hdu 3333 Turing Tree 成段不重复求和

貌似和以前做过的某道题一模一样,离线查询

在这里

http://www.cnblogs.com/wuyiqi/archive/2012/02/13/2349290.html

View Code
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
const int maxn = 50010;
__int64 c[maxn];
struct node{
    int l,r,id;
}p[200010];
map<int,int> Hash;
int lowbit(int x){
    return x&-x;
}
void update(int x,int d){
     for(;x<maxn;x+=lowbit(x))
         c[x]+=d;
}
__int64 sum(int x){
       __int64 ans=0;
       for(;x>0;x-=lowbit(x))
           ans+=c[x];
       return ans;
}
int cmp(node a,node b){
    return a.r<b.r;
}
int a[maxn];
__int64 ans[200010];
int main()
{
    int t,i,j,l,r,n;
    int q;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++) scanf("%d",&a[i]);
        memset(c,0,sizeof(c));
        scanf("%d",&q);
        for(i=1;i<=q;i++)
        {
            scanf("%d%d",&p[i].l,&p[i].r);
            p[i].id=i;
        }
        int pt=1;
        Hash.clear();
        sort(p+1,p+q+1,cmp);
        for(i=1;i<=q;i++)
        {
            while(pt<=p[i].r)
            {
                if(Hash[a[pt]]!=0)
                {
                    update(Hash[a[pt]],-a[pt]);
                }
                update(pt,a[pt]);
                Hash[a[pt]]=pt;
                pt++;
            }
            ans[p[i].id]=sum(p[i].r)-sum(p[i].l-1);
        }
        for(i=1;i<=q;i++)
        {
            printf("%I64d\n",ans[i]);
        }
    }
    return 0;
}
posted @ 2012-05-17 17:06  Because Of You  Views(227)  Comments(0Edit  收藏  举报