#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<map>
#define maxn1 30005
#define maxn2 100005
typedef long long LL;
using namespace std;
int l[maxn2],r[maxn2],loc[maxn1],A[maxn1],id[maxn2],n,vis[30007];
LL C[maxn1],ans[maxn2];
int Hash(int x)
{
return x%30007;
}
inline 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;
}
bool cmp(int i,int j)
{
return r[i]<r[j];
}
int main()
{
int T;
ios_base::sync_with_stdio(false);
cin>>T;
while(T--)
{
cin>>n;
memset(loc,0,sizeof(loc));
memset(C,0,sizeof(C));
//memset(vis,0,sizeof(vis));
map<int,int>q;
q.clear();
for(int i=1;i<=n;i++)
{
cin>>A[i];
update(i,A[i]);
if(!q[A[i]])
{
q[A[i]]=i;
}
}
int m;
cin>>m;
for(int i=1;i<=m;i++)
{
cin>>l[i]>>r[i];
id[i]=i;
}
sort(id+1,id+1+m,cmp);
int Right=1;
for (int i = 1; i <= m; ++i)
{
int x=id[i];
while(Right<=r[x])
{
if(q[A[Right]]!=Right){
update(q[A[Right]],-A[Right]);
q[A[Right]]=Right;
}
++Right;
}
Right=r[x];
ans[x]=Query(r[x])-Query(l[x]-1);
}
for(int i=1;i<=m;i++)printf("%I64d\n",ans[i]);
}
return 0;
}
HDU 3333 树状数组离线
最新推荐文章于 2021-01-06 21:09:28 发布