HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
数组 处理 HDU 离线 线段 树状
2023-09-27 14:22:23 时间
Necklace
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2083 Accepted Submission(s): 747
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
lcy
- /*
- 题意为查找区间去重后的和
- 用树状数组离线处理
- 将所有查询以右端点从小到大排序
- 按此顺序边去重边查询
- 前面的去重就不会影响到后面的结果了
- */
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=51000; const int M=210000; struct node{ int l,r; int id; }q[M]; int n,m,val[N],pre[N],loc[1100000]; long long arr[N],res[M]; int lowbit(int x){ return x&(-x); } void update(int i,int x){ while(i<=n){ arr[i]+=x; i+=lowbit(i); } } long long Sum(int i){ long long ans=0; while(i>0){ ans+=arr[i]; i-=lowbit(i); } return ans; } bool cmp(node a,node b){ return a.r<b.r; } int main(){ //freopen("input.txt","r",stdin); int t; scanf("%d",&t); while(t--){ memset(arr,0,sizeof(arr)); memset(loc,-1,sizeof(loc)); scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&val[i]); pre[i]=loc[val[i]]; loc[val[i]]=i; update(i,val[i]); } scanf("%d",&m); for(int i=1;i<=m;i++){ scanf("%d%d",&q[i].l,&q[i].r); q[i].id=i; } sort(q+1,q+1+m,cmp); int r=0; for(int i=1;i<=m;i++){ for(int j=r+1;j<=q[i].r;j++) if(pre[j]!=-1) update(pre[j],-val[j]); r=q[i].r; res[q[i].id]=Sum(q[i].r)-Sum(q[i].l-1); } for(int i=1;i<=m;i++) printf("%I64d\n",res[i]); } return 0; }
线段树:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> using namespace std; const int N=51000; //#define L(rt) (rt<<1) //#define R(rt) (rt<<1|1) #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 struct Tree{ int l,r; int id; }q[N<<2]; map<int,int> mp; int n,m,a[N]; long long sum[N<<2],res[N<<2]; void PushUp(int rt){ sum[rt]=sum[rt<<1]+sum[rt<<1|1]; } void update(int id,int val,int l,int r,int rt){ if(l==r){ sum[rt]+=val; return ; } int mid=(l+r)>>1; if(id<=mid) update(id,val,lson); else update(id,val,rson); PushUp(rt); } long long query(int L,int R,int l,int r,int rt){ if(L<=l && R>=r) return sum[rt]; int mid=(l+r)>>1; long long ans=0; if(L<=mid) ans+=query(L,R,lson); if(R>mid) ans+=query(L,R,rson); return ans; } int cmp(Tree a,Tree b){ return a.r<b.r; } int main(){ //freopen("input.txt","r",stdin); int t; scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); scanf("%d",&m); for(int i=1;i<=m;i++){ scanf("%d%d",&q[i].l,&q[i].r); q[i].id=i; } sort(q+1,q+1+m,cmp); mp.clear(); memset(sum,0,sizeof(sum)); int r=0; for(int i=1;i<=m;i++){ for(int j=r+1;j<=q[i].r;j++){ if(mp[a[j]]) update(mp[a[j]],-a[j],1,n,1); update(j,a[j],1,n,1); mp[a[j]]=j; r=q[i].r; } res[q[i].id]=query(q[i].l,q[i].r,1,n,1); } for(int i=1;i<=m;i++) printf("%I64d\n",res[i]); } return 0; }
相关文章
- JS合并两个数组的方法
- Matlab中数组下标是logical,如何处理?
- Matlab找二维数组最大值
- 0长度数组
- ***PHP implode() 函数,将数组合并为字符串;explode() 函数,把字符串打散为数组
- php处理xml成数组
- jQuery.each(object, [callback])方法,用于处理json数组
- 算法积累之处理数组数据
- js对ajax返回数组的处理
- 【求数组中第二大的数字】(Java实现,附带分析)
- 剑指 Offer II 006. 排序数组中两个数字之和
- Java中文件与字节数组转换
- Python 获取二维数组中每列第二个最小值的方法及示例代码
- 【编程】二维数组按照某行或某列排序(c++实现)
- C语言柔性数组
- 二维数组一维表示
- PHP数组操作函数
- Jquery遍历筛选数组的几种方法和遍历解析json对象|Map()方法详解
- leetcode 153 寻找选择排序数组中的最小值