hdu3874-Necklace 线段树+离散化
来源:互联网 发布:西北大学网络教育 编辑:程序博客网 时间:2024/05/16 23:43
Necklace
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4849 Accepted Submission(s): 1659
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题意:有一串项链,项链上的每一颗珍珠都有一定的价值,看起来一样的珍珠的价值是一样的,现在需要求从第i颗到第j颗珍珠的价值和,注意的是如果有相同的珍珠则只计算一次。解题思路:如果按照普通的思路这道题会超时,在这里我们需要用到的是线段树求区间和以及离散化缩小数据规模,离散化的具体知识可以看我的博客:点击打开链接http://blog.csdn.net/wang_heng199/article/details/75324299ac代码:#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>using namespace std;#define lson l,m,o<<1#define rson m+1,r,o<<1|1typedef __int64 ll;int a[50010];int n;ll sum[50010<<2];struct node{ int l; int r; int i;};node q[200010];int vis[1000010];ll ans[200010];void pushup(int o){ sum[o]=sum[o<<1]+sum[o<<1|1];}void build(){ memset(sum,0,sizeof(sum));}void update(int l,int r,int o,int p,int x){ if(l==r) { sum[o]+=x; return; } int m=l+r>>1; if(p<=m)update(lson,p,x); else update(rson,p,x); pushup(o);}ll query(int l,int r,int o,int L,int R){ if(L<=l&&r<=R)return sum[o]; int m=l+r>>1; ll ans=0; if(L<=m)ans+=query(lson,L,R); if(m<R)ans+=query(rson,L,R); return ans;}bool cmp(node a,node b){ return a.r<b.r;}int main(){ int t; scanf("%d",&t); while(t--) { int i; scanf("%d",&n); for(i=0;i<n;i++)scanf("%d",&a[i]); build(); int Q; scanf("%d",&Q); for(i=0;i<Q;i++){ scanf("%d%d",&q[i].l,&q[i].r); q[i].i=i; } sort(q,q+Q,cmp); int ll=0; memset(vis,-1,sizeof(vis)); for(i=0;i<Q;i++) { while(ll<=q[i].r-1) { if(vis[a[ll]]!=-1)update(0,n-1,1,vis[a[ll]],-a[ll]); vis[a[ll]]=ll; update(0,n-1,1,ll,a[ll]); ll++; } ans[q[i].i]=query(0,n-1,1,q[i].l-1,q[i].r-1); } for(i=0;i<Q;i++)printf("%I64d\n",ans[i]); } return 0;}题目链接:点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=3874推荐相似类型题目:点击打开链接http://blog.csdn.net/wang_heng199/article/details/75329999
阅读全文
0 0
- hdu3874-Necklace 线段树+离散化
- 线段树 hdu3874 Necklace
- hdu3874 Necklace 线段树端点更新
- hdu3874 Necklace 线段树/树状数组
- [树状数组&线段树]HDU3874 Necklace 离线思想
- HDU3874:Necklace
- hdu3874(离线思想+线段树)
- hdu3874(线段树点更新)
- 【题解】[hdu3874]Necklace
- HDU3874 Necklace[树状数组]
- hdu3874/hdu3333 线段树区间求和
- hdu3874 线段树单点更新+离线处理
- HDU3874 Necklace(树状数组)
- 线段树+离散化
- 离散化 + 线段树
- 线段树离散化
- 线段树离散化
- POJ_2528 线段树+离散化
- 配置(7) Tensorflow OpKernel ('op: "BestSplits" device_type: "CPU"') for unknown op: BestSplits
- 如何解决Sublime Text 3不能正确显示中文的问题
- ORA-30036 的错误解决
- mongo可视化工具
- 喷水装置(一)
- hdu3874-Necklace 线段树+离散化
- hibernate的list映射和map映射
- 解决A master URL must be set in your configuration错误
- grep详解
- HTML标签
- JAVA之多线程(一)
- springmvc项目创建
- 不同系统下微信 onMenuShareTimeline 调用
- Android下的USB Host介绍和开发