Curious Robin Hood
来源:互联网 发布:长视频软件 编辑:程序博客网 时间:2024/05/18 01:27
Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick. He keeps n sacks where he keeps this money. The sacks are numbered from 0 to n-1.
Now each time he can he can do one of the three tasks.
1) Give all the money of the ith sack to the poor, leaving the sack empty.
2) Add new amount (given in input) in the ith sack.
3) Find the total amount of money from ith sack to jth sack.
Since he is not a programmer, he seeks your help.
Input starts with an integer T (≤ 5), denoting the number of test cases.
Each case contains two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000). The next line contains n space separated integers in the range [0, 1000]. The ith integer denotes the initial amount of money in the ith sack (0 ≤ i < n).
Each of the next q lines contains a task in one of the following form:
1 i Give all the money of the ith(0 ≤ i < n) sack to the poor.
2 i v Add money v (1 ≤ v ≤ 1000) to the ith(0 ≤ i < n) sack.
3 i j Find the total amount of money from ith sack to jth sack (0 ≤ i ≤ j < n).
For each test case, print the case number first. If the query type is 1, then print the amount of money given to the poor. If the query type is 3, print the total amount from ith to jth sack.
1
5 6
3 2 1 4 5
1 4
2 3 4
3 0 3
1 2
3 0 4
1 1
Case 1:
5
14
1
13
2
树状数组的水题,需要注意的是本题的数据是从0~n-1,而不是1~n,这里需要特别注意树状数组中要从1~n!!
还有就是因为树状数组的特性,记得每次输入数据时,清空数组!
关于树状数组,详见另一篇博客!
附AC代码:
#include <stdio.h>#include <string.h>int N;int c[100005];int lowbit(int x){ return x&(-x);}void update(int x,int num){ while(x<=N) { c[x]+=num; x+=lowbit(x); }}int get_sum(int x){ int sum=0; while(x>0) { sum+=c[x]; x-=lowbit(x); } return sum;}int main(){ int T,P,a; int i,j; int l,m,n; int k=1; scanf("%d",&T); while(T--) { memset(c,0,sizeof(c)); scanf("%d%d",&N,&P); for(i=1;i<=N;i++) { scanf("%d",&a); update(i,a); } printf("Case %d:\n",k++); for(i=0;i<P;i++) { scanf("%d",&l); if(l==1) { scanf("%d",&m); m++; printf("%d\n",get_sum(m)-get_sum(m-1)); update(m,-(get_sum(m)-get_sum(m-1))); } else if(l==2) { scanf("%d%d",&m,&n); m++; update(m,n); } else if(l==3) { scanf("%d%d",&m,&n); m++; n++; printf("%d\n",get_sum(n)-get_sum(m-1)); } } } return 0;}
- Curious Robin Hood
- LightOJ 1112 Curious Robin Hood
- Curious Robin Hood(简单线段树)
- LightOJ1112——Curious Robin Hood
- Light oj 1112 - Curious Robin Hood(树状数组)
- Light oj 1112-Curious Robin hood --树状数组
- 树状数组初步 Light OJ 1112 Curious Robin Hood
- LightOj 1112 Curious Robin Hood(线段树||树状数组)
- Light oj 1112 - Curious Robin Hood【单点更新】
- lightOj 1112 Curious Robin Hood (线段树)
- Lightoj 1112 - Curious Robin Hood 【单点修改 + 单点、 区间查询】【树状数组 水题】
- LightOJ 1112 Curious Robin Hood && LightOJ 1266 Points in Rectangle(树状数组)
- Lightoj1112——Curious Robin Hood(线段树+单点更新)
- CF_671B Robin Hood(二分)
- codefores 352 D. Robin Hood
- codeoforces 671B Robin Hood
- CodeForces 671B Robin Hood
- cf 671B Robin Hood
- 你是否重视研究关键词了呢
- Tiger Concurrent Practice --日志分析并行分解设计与实现
- UVA - 11384 Help is needed for Dexter
- KMP算法
- java提高篇-----Iterator
- Curious Robin Hood
- Linux解压缩*.tar.gz命令
- 紫专妆啄滋咨孜浊棕踪滓罪组租淄姿遵祝
- opencv不同深度CvArr*(CvMat*,IplImage*)类型的显示
- 控制cellEditor是否为可编辑
- 约瑟夫环
- python的测试框架
- 计算两个日期之间天数
- 图像基本原理