hdu 3874

来源:互联网 发布:c语言教程视频 编辑:程序博客网 时间:2024/04/29 23:15

Necklace

Problem Description
Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or more balls have the same beautiful value, we just count it once. We define the beautiful value of some interval [x,y] as F(x,y). F(x,y) is calculated as the sum of the beautiful value from the xth ball to the yth ball and the same value is ONLY COUNTED ONCE. For example, if the necklace is 1 1 1 2 3 1, we have F(1,3)=1, F(2,4)=3, F(2,6)=6.

Now Mery thinks the necklace is too long. She plans to take some continuous part of the necklace to build a new one. She wants to know each of the beautiful value of M continuous parts of the necklace. She will give you M intervals [L,R] (1<=L<=R<=N) and you must tell her F(L,R) of them.
 

Input
The first line is T(T<=10), representing the number of test cases.
  For each case, the first line is a number N,1 <=N <=50000, indicating the number of the magic balls. The second line contains N non-negative integer numbers not greater 1000000, representing the beautiful value of the N balls. The third line has a number M, 1 <=M <=200000, meaning the nunber of the queries. Each of the next M lines contains L and R, the query.
 

Output
For each query, output a line contains an integer number, representing the result of the query.
 

Sample Input
261 2 3 4 3 531 23 52 661 1 1 2 3 531 12 43 5
 

Sample Output
3714136


最近在学树状数组 搜到一个较好的题 也搜到一个极好的代码 于是就搬过来了
hash数组记录该数是否出现过 与之前出现的位置 ,若出现过就将之前位置上的数清零同时更新su[ ]数组,
#include <cstdio>#include <cstring>#include <map>#include <algorithm>#include<iostream>using namespace std;const int N = 50010;int n, m, a[N];map<int, int> hash;long long ans[N*4],su[4*N];struct node{    int i, l, r;//哎,终于懂了 i才是记录之前的顺序} q[N*4];void update(int i, int v)//主要是更新su[]数组,包括加减(减法主要是将出现过的位子上的该数清零){    while(i <= n)    {        su[i] += v;        i += i&(-i);        //cout<<"*****  "<<i<<endl;        //cout<<"++++++    "<<v<<endl;    }}long long query(int i)//树状数组 求和{    long long sum = 0;    while(i > 0)    {        sum += su[i];        i -= i&(-i);    }    return sum;}int cmp1(node a, node b){    return a.r < b.r;}int main(){    int cas, i, j, k, l, r;    scanf("%d", &cas);    while(cas--)    {        scanf("%d", &n);        for(i = 1; i <= n; i++)            scanf("%d", &a[i]);        scanf("%d", &m);        for(i = 0; i < m; i++)        {            scanf("%d%d", &q[i].l, &q[i].r);            if(q[i].l > q[i].r)                swap(q[i].l, q[i].r);            q[i].i = i;        }       sort(q, q+m, cmp1);        hash.clear();        r = 1;//跟a[]数组的第一个下标有关        memset(su,0,sizeof(su));        for(i = 0; i < m; i++)        {            while(r <= q[i].r)            {                if(hash[a[r]] != 0)                    update(hash[a[r]], -a[r]);//若                hash[a[r]] = r;                update(r, a[r]);                r++;            }            ans[q[i].i] = query(q[i].r) - query(q[i].l-1);//结构体中的i才是之前结构体数组的位置 之前的sort排序打乱了顺序        }        for(i = 0; i < m; i++)            printf("%I64d\n", ans[i]);    }    return 0;}


0 0
原创粉丝点击