HDU 3874 树状数组 + 离线处理

Necklace

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1529    Accepted Submission(s): 572

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
2
6
1 2 3 4 3 5
3
1 2
3 5
2 6
6
1 1 1 2 3 5
3
1 1
2 4
3 5
 
Sample Output
3
7
14
1
3
6
View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #define MAX 50010
 6 
 7 using namespace std;
 8 
 9 struct node
10 {
11     int left,right,index;
12 };
13 
14 node query[MAX*4];
15 int val[MAX];
16 __int64 ans[MAX*4],sum[MAX];
17 int pre[MAX*20];
18 int N,Q;
19 
20 bool cmp(node a,node b)
21 {
22     return a.right < b.right;
23 }
24 
25 int lowbit(int x)
26 {
27     return x & (-x);
28 }
29 
30 __int64 cal(int x)
31 {
32     __int64 ret = 0;
33     while(x > 0)
34     {
35         ret += sum[x];
36         x -= lowbit(x);
37     }
38     return ret;
39 }
40 
41 void add(int x,int v)
42 {
43     while(x <= N)
44     {
45         sum[x] += v;
46         x += lowbit(x);
47     }
48 }
49 
50 int main()
51 {
52     int Case;
53     scanf("%d",&Case);
54     while(Case -- )
55     {
56         scanf("%d",&N);
57         for(int i=1; i<=N; i++)
58             scanf("%d",&val[i]);
59         scanf("%d",&Q);
60         for(int i=1; i<=Q; i++)
61         {
62             scanf("%d%d",&query[i].left,&query[i].right);
63             query[i].index = i;
64         }
65         sort(query+1,query+Q+1,cmp);
66         memset(pre,0,sizeof(pre));
67         memset(sum,0,sizeof(sum));
68         int pt = 1;
69         for(int i=1; i<=Q; i++)
70         {
71             while(pt <= query[i].right)
72             {
73                 if(pre[val[pt]])
74                     add(pre[val[pt]],-val[pt]);
75                 pre[val[pt]] = pt;
76                 add(pt,val[pt]);
77                 pt ++;
78             }
79             ans[query[i].index] = cal(query[i].right)-cal(query[i].left-1);
80         }
81         for(int i=1; i<=Q; i++)
82             printf("%I64d\n",ans[i]);
83     }
84     return 0;
85 }

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值