HDU3874 (树状数组+离线操作)
来源:互联网 发布:nginx跳转保持url不变 编辑:程序博客网 时间:2024/06/07 02:32
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 intervalsL,R
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 Sample Output
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
(1<=L<=R<=N) and you must tell her F(L,R) of them.
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.
261 2 3 4 3 531 23 52 661 1 1 2 3 531 12 43 5
3714136
题意:给一组数据,每次查询一个区间内的和,相同的数据智能计算一次。
分析:查询一个区间和线段树或者树状数组。一个数据只能计算一次,有点麻烦。这个时候应该用离线操作。
先把要查询的数据按照区间右端点,从小到大排序。用一个last数组记录每个数据第一次出现的位置。
每次查询前先遍历一次这个区间,对于不是第一次出现的数据。先减去第一次出现这个数据的位置的值。
然后将这个位置标记为第一次出现。由于区间已排好序,从左至右依次。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int last[1000200],n,m,val[200050];long long ans[200050],seg[50050];struct node{ int b,e,index;}a[200050];bool cmp(node aa,node bb){ return aa.e<bb.e;}void add(int i,int v){ while(i<=n) { seg[i]+=v; i+=(i&-i); }}long long sum(int i){ long long anss=0; while(i>0) { anss+=seg[i]; i-=(i&-i); } return anss;}int main(){ int t; scanf("%d",&t); while(t--) { memset(last,0,sizeof(last)); memset(seg,0,sizeof(seg)); scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&val[i]); add(i,val[i]); if(!last[val[i]])last[val[i]]=i; } scanf("%d",&m); for(int i=1;i<=m;i++) { scanf("%d%d",&a[i].b,&a[i].e); a[i].index=i; } sort(a+1,a+m+1,cmp); int rg=1; for(int i=1;i<=m;i++) { for(int j=rg;j<=a[i].e;j++) { if(last[val[j]]!=j) { add(last[val[j]],-val[j]); last[val[j]]=j; } } rg=a[i].e; ans[a[i].index]=sum(a[i].e)-sum(a[i].b-1); } for(int i=1;i<=m;i++) printf("%lld\n",ans[i]); } return 0;}
阅读全文
0 0
- HDU3874 (树状数组+离线操作)
- hdu3874 离线树状数组
- hdu3874之离线树状数组
- 简单树状数组+离线 HDU3874
- HDU3874 Necklace(树状数组)
- [树状数组&线段树]HDU3874 Necklace 离线思想
- HDU3874 Necklace[树状数组]
- HDU3874树状数组的应用
- 1006(离线操作+树状数组)
- HDU3874 树状数组的灵活应用
- hdu3874 Necklace 线段树/树状数组
- HDU4638——Group(树状数组+离线操作)
- HDU 3874 Necklace(树状数组 && 离线操作)
- HDU 3874 Necklace (树状数组+离线操作)
- BZOJ 1878-HH的项链(离线操作+树状数组)
- 1004(树状数组+离线操作+离散化)
- crf 的数数 (树状数组 离线操作)
- hdu-4638-Group-(树状数组,离线操作)
- memcpy使用
- notepad++更改选中行背景色和字体
- coremark测试
- 理解Python的With语句
- java 值类型和引用类型
- HDU3874 (树状数组+离线操作)
- python zip 变成元组数组之后如何解压?
- arm-linux之为开发板写上电自启动程序之方法
- IE7下浮动元素margin-bottom失效
- linux 修改mysql端口
- LoadMenu使用方法
- Base64编码原理分析
- stm32 printf 死机
- xcode模拟器默认弹出默认键盘