hdu3874之离线树状数组
2014-03-13 18:57
288 查看
Necklace
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2467 Accepted Submission(s): 873
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
/*分析:对于所有的询问先离线储存好然后排序, 然后对输入的数组进行扫描,对于任何数都只保存最后出现的位置,把原来的位置赋值为0 这样在扫描的过程中进行求和并且保存最后的位置使得后面包含该数的区间要不只包含一次该数或者不包含 */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <queue> #include <algorithm> #include <map> #include <iomanip> #define INF 99999999 typedef __int64 LL; using namespace std; const int MAX=50000+10; int n,q,size; LL c[MAX],a[MAX],sum[MAX*4]; int hash[MAX],mp[MAX]; struct Node{ int l,r,id; bool operator<(const Node &temp)const{ return r<temp.r; } }s[MAX*4]; int lowbit(int x){ return x&(-x); } void Update(int x,int d){ while(x<=n){ c[x]+=d; x+=lowbit(x); } } LL Query(int x){ LL sum=0; while(x>0){ sum+=c[x]; x-=lowbit(x); } return sum; } int search(int x){ int left=1,right=size,mid; while(left<=right){//>=x的第一个数 mid=left+right>>1; if(hash[mid]<x)left=mid+1; else right=mid-1; } return right+1; } int main(){ int t,x; scanf("%d",&t); while(t--){ memset(mp,0,sizeof mp); memset(c,0,sizeof c); scanf("%d",&n); for(int i=1;i<=n;++i)scanf("%d",&a[i]),hash[i]=a[i]; sort(hash+1,hash+1+n); size=1; for(int i=2;i<=n;++i)if(hash[i] != hash[size])hash[++size]=hash[i]; scanf("%d",&q); for(int i=1;i<=q;++i){ scanf("%d%d",&s[i].l,&s[i].r); s[i].id=i; } sort(s+1,s+1+q); int i=1,j=1; while(i<=n && j<=q){ int k=search(a[i]); if(mp[k])Update(mp[k],-a[i]); Update(i,a[i]); mp[k]=i; if(s[j].r == i){ LL ans=Query(i); while(s[j].r == i){ LL temp=Query(s[j].l-1); sum[s[j].id]=ans-temp; ++j; } } ++i; } for(int i=1;i<=q;++i)printf("%I64d\n",sum[i]); } return 0; }
相关文章推荐
- hdu3874 离线树状数组
- [SPOJ DQUERY] D-query(树状数组,离线)
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
- 1018(离线树状数组)
- HDU 4777 Rabbit Kingdom [离线+树状数组]
- hdu 4417 离线树状数组 胡乱搞搞
- HDU 3874 Necklace(树状数组的离线操作)
- SPOJ DQUERY(树状数组离线处理 or 主席树 区间不同数个数)
- hdu 4777 Rabbit Kingdom (离线树状数组)
- SCU2016-04 H题 质因数处理 + 离线区间树状数组
- Codeforces Gym 100114 H. Milestones 离线树状数组
- [离线+树状数组 || 主席树]BZOJ1878: [SDOI2009]HH的项链
- HDU3333 Turing Tree 离线树状数组
- Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化
- hdu-4638-Group-(树状数组,离线操作)
- POJ-3416 Crossing (树状数组 离线处理)
- BZOJ 1878: [SDOI2009]HH的项链 树状数组 离线维护
- AtCoder Regular Contest 068 E - Snuke Line 离线+树状数组
- HDU 3874 Necklace(树状数组的离线操作)
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线操作,树状数组,last[value],异或和