Andyqsmart ACM Learning process--zoj3872 Beauty of Array (recursive)

Source: Internet
Author: User

Description

Edward has a array A with N integers. He defines the beauty of an array as the summation of a distinct integers in the array. Now Edward wants to know the summation of the beauty of all contiguous subarray of the array A .

Input

There is multiple test cases. The first line of input contains an integer indicating the number of the T test cases. For each test case:

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

Output

For each case, print the answer on one line.

Sample Input

351 2 3 4 532 3 342 3 3 2

Sample Output

1052138

This problem directly thought of the idea of 1 a.

Suppose that f (n) represents the and of the number of sub-intervals that end with a[n]. For f (n-1), there may be two parts, part A of the value of A[n], part B that does not contain the value of a[n], assuming B is added by the P interval. Then you can get: F (N) = A + B + p*a[n] = f (n-1) + p*a[n]. So the key is to ask for P.

And since F (n-1) is the number of sub-interval reciprocal numbers ending with a[n-1], it is necessary to assume that A[i] is the nearest value of A[n] for the distance a[n], then P = n-i. So you only need to record the most recent value K's foot mark. Map is used here, the data range is not very large, you can open the array directly.

Code:

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <algorithm> #include <set> #include <map> #include <queue> #include <string> #define LL Long longusing namespace Std;int main () {    //freopen ("test.in", "R", stdin);    int T, n, V, K;    LL ans, F;    scanf ("%d", &t);    for (int times = 0; times < T; ++times)    {        scanf ("%d", &n);        Map<int, int> s;        Ans = 0;        f = 0;        for (int i = 1; I <= n; ++i)        {            scanf ("%d", &v);            f = f + (I-s[v]) *v;            S[V] = i;            Ans + = f;        }        printf ("%lld\n", ans);    }    return 0;}

Andyqsmart ACM Learning process--zoj3872 Beauty of Array (recursive)

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.