Thinking ZOJ 3872 Beauty of Array

Edward has an arrayAwithNintegers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the beauty of all contiguous subarray of the arrayA.     

Input

There are multiple test cases. The first line of input contains an integerTindicating the number of test cases. For each test case:  

The first line contains an integerN(1 <=N<= 100000), which indicates the size of the array. The next line containsNpositive integers separated by spaces. Every integer is no larger than 1000000.      

<h4< dd="">Output

For each case, print the answer in one line.

<h4< dd="">Sample Input
3
5
1 2 3 4 5
3
2 3 3
4
2 3 3 2
<h4< dd="">Sample Output
105
21
38


          This is a thinking problem, very brain-burning, let the sum of different numbers of consecutive subsets in an array;
          2 3 3
          2                =2
          3                =3
          3                =3
          2  3            =5
          3  3            =3
          2 3 3          =5
          The sum is 2+3+3+5+3+5=21

This type of question must first know that there must be rules to follow
For example, the sum of the array 1 2 3 4 5 can be seen as the sum of 1 2 3 4 plus the sum of the new subset resulting from 5
which is:
1  2  3  4  5 
    2  3  4  5
        3  4  5
            4  5
                5
So what is the total value of the new subset generated by 5?
It can be observed that the value of the new subset generated by 4 is added with the number of 5.
That is: p + 5 * 5 (p is the total value of the newly added continuous subset generated by the last added element)
So what if the new subset duplicates the previous element?
Example: The new element of 1 2 3 4 is 3;
which is:
1  2  3  4  3
    2  3  4  3
        3  4  3
            4  3
                3
Because there is a repetition with the number 3 in the first 3 subsets, there is no repeated number 3 in the latter two groups
i.e. p+3*2,
Why is it 3*2? 2 How did it come?
It can be known that the current position of 3 minus the position of the previous 3 is 2;
That is to say, if the new element coincides with a previous element, the total value of the new subsequence generated by the new element is p+x*i (x is the new element, i is the current element position is the same as the previous one element difference)
What if there are multiple duplicates?
Example: The new element of 1 3 2 3 4 is 3
1  3  2  3  4  3
    3  2  3  4  3
        2  3  4  3
            3  4  3
                4  3
                    3
Obviously, just find the distance between 3 and the last closest 3;

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
long long s1[1000005],w[1000005];
intmain()
{
	long long n,a,b,c;
	cin>>a;
	while(a--){
	cin>>n;
	memset(w,0,sizeof(w));
	long long sum=0,p=0;
	for(int i=1;i<=n;i++)
	{
		cin>>s1[i];
		p=p+s1[i]*(iw[s1[i]]);//The second p is the total value of the newly added continuous subset generated by the last added element; the first p is the current new addition the value of the newly added contiguous subset produced by the element
		sum+=p;
		w[s1[i]]=i;//Bucket sorting is used here to record the position of the last occurrence of the current element
	}
	cout<<sum<<endl;
  }
}


 
  

 

     

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325935376&siteId=291194637
ZOJ
ZOJ