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.
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
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
- HDOJ 题目3874 Necklace(线段树+离线求区间去重和)
- Necklace (线段树单点更新+区间查询+离线操作)
- HDU 3874 Necklace (线段树单点更新+区间查询+离线操作)
- HDOJ 题目3954 Level up(线段树去见面更新区间查询)
- HDOJ 题目1754 I Hate It(线段树单点更新,求区间最大值)
- HDOJ 题目3074 Multiply game(线段树单点更新,区间求乘积)
- HDU - 3874 Necklace (线段树 + 离线处理)
- HDOJ 1698 Just a Hook (线段树区间更新求区间和)
- HDOJ 1166 敌兵布阵(线段树单点更新求区间和)
- HDOJ 题目3308 LCIS(线段树,区间查询,区间合并)
- HDOJ 题目3911 Black And White(线段树区间异或区间合并)
- hdu 3874 Necklace(离线操作+树状数组或线段树)
- HDU 3874 Necklace(线段树啊 单点更新 区间求和)
- HDOJ 3874 Necklace 线段树 单点更新 成段查询
- POJ 题目3667 Hotel(线段树,区间更新查询,求连续区间)
- hdu 3333 Turing Tree(线段树求区间内不同值之和+离线处理)
- HDOJ 题目1698 Just a Hook(线段树区间更新)
- HDOJ 题目3397 Sequence operation(线段树区间覆盖异或合并)
- Java_jdbc 基础笔记之一 数据库连接
- android开发常用网址
- 每天一个Linux命令-24(file)
- (五)在python中创建一个函数
- HDU 3001 【三进制状压DP】
- HDOJ 题目3874 Necklace(线段树+离线求区间去重和)
- kafka部署一
- redis与memcache区别
- (六)python共享代码步骤
- POJ 2337 Catenyms(欧拉路径)
- 断舍离
- 准确率(Accuracy), 精确率(Precision), 召回率(Recall)和F1-Measure,confusion matrix
- python编程初学者入门案例-猜数游戏和天气查询
- Java类的多态机制