hdu 3874 Necklace
2013-08-14 20:42
316 查看
Necklace
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2004 Accepted Submission(s): 718
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each query, output a line contains an integer number, representing the result of the query.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
3
7
14
1
3
6
方法很难想,离线处理,先对右区间从小到大排序,对于每一个区间,把前面的重复的删掉,留下离右区间最近的一个,还有就是注意sum的值超过int的范围要用__int64
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct node { int l; int r; int id; }p[800000]; __int64 ans[200005]; bool cmp(node a,node b) { return a.r<b.r; } __int64 c[50005]; int a[50005]; int flag[1000005],n; int lowbit(int x) { return x&(-x); } void add(int x,int k) { while(x<=n) { c[x]+=k; x+=lowbit(x); } } __int64 Sum(int x) { __int64 sum=0; while(x>0) { sum+=c[x]; x-=lowbit(x); } return sum; } int main() { int m,t,i; // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&a[i]); flag[a[i]]=0; c[i]=0; } scanf("%d",&m); for(i=1;i<=m;i++) { scanf("%d %d",&p[i].l,&p[i].r); p[i].id=i; } sort(p+1,p+m+1,cmp); int j;i=1; for(j=1;j<=m;j++) { while(i<=p[j].r) { if(flag[a[i]]) add(flag[a[i]],-a[i]); flag[a[i]]=i; add(i,a[i]); i++; } ans[p[j].id]=Sum(p[j].r)-Sum(p[j].l-1); } for(i=1;i<=m;i++) printf("%I64d\n",ans[i]); } return 0; }
相关文章推荐
- hdu 3874 Necklace (树状数组)
- HDU 3874 Necklace(树状数组离线处理)
- HDU 3874 Necklace&&HDU 3333 Turing Tree(求一些区间l~r中的数去重后的和)
- hdu 3874 Necklace 线段树单点更新区间求和
- hdu 3874 Necklace(bit树+事先对查询区间右端点排序)
- hdu 3874 Necklace
- HDU 3874 Necklace 2011 Multi-University Training Contest 4 - Host by SDU 树状数组+离散化
- HDU 3874 Necklace(线段树啊 单点更新 区间求和)
- HDU 3874 Necklace
- hdu 3874 Necklace【树状数组简单应用】
- hdu 3874 Necklace 树状数组 离线操作
- hdu 3874【Necklace】
- HDU 3874 Necklace(树状数组的离线操作)
- hdu-3874-Necklace-(树状数组)
- HDU - 3874Necklace(线段树离线查询)
- HDU 3874 Necklace
- HDU 3874 Necklace(树状数组的离线操作)
- hdu 3874 Necklace
- HDU 3874 Necklace (数状数组)
- hdu 3874 Necklace (树状数组+离线操作)