hdu 3874 Necklace(树状数组的灵活应用)
2013-08-10 21:48
459 查看
。用map记录当前值是否出现过,并记录这个值的最后一处的位置,当遍历到某个值时,如果前面已经出现过,那么将前面那个数的值清为0,即将map记录的这个数的位置上的数减去这个数,当前位置插入这个值。离线处理询问即可。
#include<iostream> #include<map> #include<math.h> #include<cstdio> #include<string.h> #include<algorithm> using namespace std; typedef __int64 LL; #define N 50005 int a ,n,m; struct node { int id,r,l; }; node q[N*4]; map<int,int> hash; LL res[N*4],shu[N*4]; bool cmp(node x,node y) { return x.r<y.r; } int lowbit(int x) { return x&-x; } void add(int w,int k) { while(w<n+5) { shu[w]+=k; w=w+lowbit(w); } } LL sum(int w) { LL s=0; while(w>0) { s+=shu[w]; w-=lowbit(w); } return s; } int main() { int i,j,t,rr,ll; scanf("%d",&t); while(t--) { 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",&ll,&rr); q[i].l=min(ll,rr); q[i].r=max(ll,rr); q[i].id=i; } sort(q,q+m,cmp); memset(shu,0,sizeof(shu)); rr=1; hash.clear(); for(i=0;i<m;i++) { while(rr<=q[i].r) { if(hash[a[rr]]!=0) add(hash[a[rr]],-a[rr]); add(rr,a[rr]); hash[a[rr]]=rr; rr++; } res[q[i].id]=sum(q[i].r)-sum(q[i].l-1); } for(i=0;i<m;i++) printf("%I64d\n",res[i]); } return 0; }
相关文章推荐
- HDU 3874 Necklace 树状数组的应用
- hdu-3874-Necklace-(树状数组)
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
- HDU 3874 Necklace(树状数组离线处理)
- HDU 3333&&HDU-3874 Necklace 离线树状数组
- HDU 3874 Necklace 树状数组 + 离线处理
- hdu 3874 Necklace 树状数组 离线操作
- HDU 3874 Necklace(树状数组的离线操作)
- hdu 3874 Necklace (树状数组)
- HDU 3874 Necklace (树状数组)
- HDU 3874 Necklace(树状数组离线处理)
- HDU 3874 Necklace(树状数组的离线操作)
- HDU 3874 Necklace 离线+树状数组
- HDU 2642 Stars 简单二维树状数组应用
- hdu 3874 树状数组 成段不重复求和
- HDU(3874)树状数组+离线
- HDU 1541 Stars(树状数组入门应用)
- hdu 3874 Necklace 求数组任意区间和(相同元素只算一次) 树状数组+离线算法
- HDU 1556 Color the ball (树状数组简单应用)
- HDU 3874 树状数组 + 离线处理