Necklace
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4095 Accepted Submission(s): 1367
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.
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.
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
Source
Recommend
HDOJ3333也是求区间去重之后的和,而且比这个题数据还小,这个代码水过
先把查询的左右边界记下来,然后开始从左开始往扫,以前出现过,就消去,知道这个查询的右边界,然后查询,
ac代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<map>
using namespace std;
#define N 100100
#define LL __int64
LL node[N<<2];
LL ans[N<<2],a[N];
struct s
{
int l,r,id;
}q[N<<1];
int cmp(s a,s b)
{
return a.r<b.r;
}
void pushup(int tr)
{
node[tr]=node[tr<<1]+node[tr<<1|1];
}
void update(int pos,int l,int r,int tr,int val)
{
if(l==r)
{
node[tr]+=val;
return;
}
int mid=(l+r)>>1;
if(pos<=mid)
{
update(pos,l,mid,tr<<1,val);
}
else
update(pos,mid+1,r,tr<<1|1,val);
pushup(tr);
}
LL query(int L,int R,int l,int r,int tr)
{
if(L<=l&&r<=R)
{
return node[tr];
}
int mid=(l+r)>>1;
LL ans1,ans2;
ans1=ans2=0;
if(L<=mid)
ans1=query(L,R,l,mid,tr<<1);
if(R>mid)
ans2=query(L,R,mid+1,r,tr<<1|1);
return ans1+ans2;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
memset(node,0,sizeof(node));
int i;
for(i=1;i<=n;i++)
{
scanf("%I64d",&a[i]);
}
int m;
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id=i;
}
sort(q+1,q+m+1,cmp);
int r=1;
map<LL ,int>pre;
for(i=1;i<=m;i++)
{
for(;r<=q[i].r;r++)
{
if(pre[a[r]])
update(pre[a[r]],1,n,1,-a[r]);
update(r,1,n,1,a[r]);
pre[a[r]]=r;
}
ans[q[i].id]=query(q[i].l,q[i].r,1,n,1);
}
for(i=1;i<=m;i++)
{
printf("%I64d\n",ans[i]);
}
}
}