12th Zhejiang Provincial competition question B Beauty of Array, 12th Zhejiang Province

Source: Internet
Author: User

12th Zhejiang Provincial competition question B Beauty of Array, 12th Zhejiang Province

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 question: Self-translation. What I was thinking at the time was that the tree-like number student thought about the number of line segments (I didn't even think about how to repeat them). My teammates used the dynamic second to pass the train of thought: 2 3 3 3 3 3 3 3 2 2 2 2 2 before each Subtraction
#include<bits/stdc++.h>using namespace std;long long sum[1000010];long long a[100010];long long b[100010];int main(){    int t;    cin>>t;    while(t--)    {        memset(sum,0,sizeof(sum));        long long n;        scanf("%lld",&n);        b[0]=0;        a[0]=0;        for(long long i=1;i<=n;i++)        {            long long shu;            scanf("%lld",&shu);            b[i]=b[i-1]+(i-sum[shu])*shu;            a[i]=a[i-1]+b[i];            sum[shu]=i;        }        cout<<a[n]<<endl;    }}


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.