HDOJ 题目3874 Necklace(线段树+离线求区间去重和)

来源:互联网 发布:mac字体库里灰色字体 编辑:程序博客网 时间:2024/05/21 17:04

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.
 

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.
 

Output
For each query, output a line contains an integer number, representing the result of the query.
 

Sample Input
261 2 3 4 3 531 23 52 661 1 1 2 3 531 12 43 5
 

Sample Output
3714136
 

Source
2011 Multi-University Training Contest 4 - Host by SDU
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  3878 3875 3870 3877 3876 
HDOJ3333也是求区间去重之后的和,而且比这个题数据还小,这个代码水过
 先把查询的左右边界记下来,然后开始从左开始往扫,以前出现过,就消去,知道这个查询的右边界,然后查询,
ac代码
#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<map>using namespace std;#define N 100100#define LL __int64LL 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);}elseupdate(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]);}}}


0 0
原创粉丝点击