ZOJ 3872 Beauty of Array (Mathematics)

Source: Internet
Author: User

ZOJ 3872 Beauty of Array (Mathematics)

 

Beauty of Array Time Limit: 2 Seconds Memory Limit: 65536 KB

 

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.

Output

For each case, print the answer in one line.

Sample Input
351 2 3 4 532 3 342 3 3 2
Sample Output
1052138

Define the beauty value of a sequence as the sum of different numbers in the sequence, and calculate the sum of the beauty values of all subsequences in a sequence.

 

Idea: in the Code, the key is to understand that C (x, 2) indicates that the obtained range is left open and right closed.

 

 

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         # Define L (x) (x <1) # define R (x) (x <1 | 1) # define MID (x, y) (x + y)> 1) # define eps 1e-8typedef long ll; # define fre (I, a, B) for (I = a; I
          
            = A; I --) # define mem (t, v) memset (t), v, sizeof (t) # define ssf (n) scanf ("% s ", n) # define sf (n) scanf ("% d", & n) # define sff (a, B) scanf ("% d", &, & B) # define sfff (a, B, c) scanf ("% d", & a, & B, & c) # define pf printf # define bug pf ("Hi \ n") using namespace std; # define INF 0x3f3f3f3f # define N 1000005int ha [N], hato [N]; int a [N], B [N]; vector
           
             G [N]; int n; int k; void solve () {int I, j; ll ans = 0; ll temp; fre (I, 0, k) {ans + = (long) (n) * (n + 1)/2 * hato [I]; // set it to temp = 0; // For example, the position of 3 in a range is (the actual position is used here, not from 0) // 3 .. 5... 8 suppose there is a 3 // In the 0 position, so there is a 3 position 0 3 5 8 // because 0 is added, so the sequence has a total of n + 1 numbers, assume that the number of n + 1 is 3 //. If we take two numbers s and e is used as the // valid range of [s + 1, e], a 3 is added, in this case, the above step // now, because the numbers between two three are not all 3 //, we need to go to the heavy. Assume that the number of the numbers between two three is set to x, then we should use x ++, // because the following C (x, 2) indicates that the valid value of the obtained range is open, right, and closed. // The number of removed values should be C (x, 2 ), of course, the subtraction should be 3 (here 3 represents the same number of original sequences) fre (j, 0, g [I]. size () {ll tt = g [I] [j] + 1-temp; ans-= (tt) * (TT-1)/2 * hato [I]; temp = g [I] [j] + 1;} ll tt = n + 1-temp; ans-= (tt) * (TT-1)/2 * hato [I];} pf ("% lld \ n", ans);} int main () {int I, j, t; sf (t); while (t --) {sf (n); fre (I, 0, n) {sf (a [I]); B [I] = a [I];} sort (B, B + n); k = unique (B, B + n)-B; // worry about the data size, hash Preprocessing for (I = 0; I
            
             

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.