#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define LL __int64
using namespace std;
int n;
int hash[1000005];
LL c[50020],a[50020];
LL pp[200050];
struct node
{
int l,r,id;
};
node q[200050];
bool cmp(node a,node b)
{
if(a.r<b.r)
return true;
else
return false;
}
LL lowbit(int x)
{
return x&(-x);
}
void update(int x,int val)
{
while(x<=n)
{
c[x]+=val;
x+=lowbit(x);
}
}
LL query(int x)
{
LL sum=0;
while(x>0)
{
sum+=c[x];
x-=lowbit(x);
}
return sum;
}
int main()
{
int t,tt,i,k;
scanf("%d",&t);
while(t--)
{
memset(c,0,sizeof(c));
memset(a,0,sizeof(a));
memset(hash,0,sizeof(hash));
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%I64d",&a[i]);
scanf("%d",&tt);
for(i=0;i<tt;i++)
{
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id=i;
}
k=1;
sort(q,q+tt,cmp);
for(i=0;i<tt;i++)
{
for(;k<=q[i].r;k++)
{
if(hash[a[k]])
update(hash[a[k]],-a[k]);
hash[a[k]]=k;
update(k,a[k]);
}
pp[q[i].id]=query(q[i].r)-query(q[i].l-1);
}
for(i=0;i<tt;i++)
printf("%I64d\n",pp[i]);
}
return 0;
}
hdu 3874 树状数组
最新推荐文章于 2017-07-12 18:59:36 发布