Curious Robin Hood(简单线段树)
来源:互联网 发布:十天学会单片机 编辑:程序博客网 时间:2024/05/22 15:19
题目来源:[NWPU][2014][SRM][4] A 题
http://vjudge.net/contest/view.action?cid=51335#problem/A
作者:npufz
题目:
Description
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
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).
Output
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.
Sample Input
1
5 6
3 2 1 4 5
1 4
2 3 4
3 0 3
1 2
3 0 4
1 1
Sample Output
Case 1:
5
14
1
13
2
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>using namespace std;long long sum1;typedef struct rootpoint{ int le,ri; int sum; int mid() { return (le+ri)/2; }} rootpoi;rootpoi tree[400050];int built (int root,int l,int r ){ if(l==r) { tree[root].le=l; tree[root].ri=r; tree[root].sum=0; return 0; } if(l!=r) { tree[root].le=l; tree[root].ri=r; tree[root].sum=0; built(root*2,l,(l+r)/2); built(root*2+1,(l+r)/2+1,r); }return 0;}int insert1 (int num,int i,int root){ if(tree[root].le==tree[root].ri) { tree[root].sum=num; return 0; } if(tree[root].mid()<i) { insert1(num,i,root*2+1); } else { insert1(num,i,root*2); } if(tree[root].le!=tree[root].ri) tree[root].sum=tree[root*2].sum+tree[root*2+1].sum;return 0;}int chaxun(int l,int r,int root){ if(tree[root].le==l&&tree[root].ri==r) { sum1=sum1+tree[root].sum; return 0; } if(l>tree[root].mid()) { chaxun(l,r,root*2+1); } else if(r<=tree[root].mid()) { chaxun(l,r,root*2); } else { chaxun(l,tree[root].mid(),root*2); chaxun(tree[root].mid()+1,r,root*2+1); } return 0;}int add (int root,int i,int num){ if(tree[root].le==tree[root].ri) { tree[root].sum+=num; return 0; } if(tree[root].mid()<i) { add(root*2+1,i,num); } else { add(root*2,i,num); } if(tree[root].le!=tree[root].ri) tree[root].sum=tree[root*2].sum+tree[root*2+1].sum;return 0;}int sub(int root ,int i){ if(tree[root].le==tree[root].ri) { printf("%d\n",tree[root].sum); tree[root].sum=0; return 0; } if(tree[root].mid()<i) { sub(root*2+1,i); } else { sub(root*2,i); } if(tree[root].le!=tree[root].ri) tree[root].sum=tree[root*2].sum+tree[root*2+1].sum;return 0;}int main(){ int n,i,qianshu,left1,right1,bianhao,t,cnt,j,q; scanf("%d",&t); for(cnt=1;cnt<=t;cnt++) { scanf("%d%d",&n,&q); built (1,1,n); for(i=1;i<=n;i++) { scanf("%d",&qianshu); insert1(qianshu,i,1); } int s1; printf("Case %d:\n",cnt); for(j=0;j<q;j++) { scanf("%d",&s1); if(s1==3) { scanf("%d%d",&left1,&right1); sum1=0; chaxun(left1+1,right1+1,1); printf("%lld\n",sum1);//printf("%I64d\n",sum1);会WA掉 } else if(s1==2) { scanf("%d%d",&bianhao,&qianshu); add(1,bianhao+1,qianshu); } else { scanf("%d",&bianhao); sub(1,bianhao+1); } } } return 0;}
反思:
看懂题目,看准输入输出要求很重要,一开始I64D 就WA了,改成LLD就A了
- Curious Robin Hood(简单线段树)
- LightOj 1112 Curious Robin Hood(线段树||树状数组)
- lightOj 1112 Curious Robin Hood (线段树)
- Curious Robin Hood
- Lightoj1112——Curious Robin Hood(线段树+单点更新)
- LightOJ 1112 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
- Light oj 1112 - Curious Robin Hood【单点更新】
- Lightoj 1112 - Curious Robin Hood 【单点修改 + 单点、 区间查询】【树状数组 水题】
- LightOJ 1112 Curious Robin Hood && LightOJ 1266 Points in Rectangle(树状数组)
- CF_671B Robin Hood(二分)
- codefores 352 D. Robin Hood
- codeoforces 671B Robin Hood
- CodeForces 671B Robin Hood
- cf 671B Robin Hood
- 利用GOOGLE CACHE 缓存token
- Java 访问权限的表,总结的比较透彻。
- 仿刮刮乐刮奖效果
- ORACLE 日期函数 MONTHS_BETWEEN
- Android最新的Platform Codenames, Versions, API Levels,NDK Releases
- Curious Robin Hood(简单线段树)
- 使用Java进行SNMP编程-SNMP4J-SNMPv1/v2-代码实例
- sql之left join、right join、inner join的区别
- 如何把sqlserver数据库中表的数据导入到excel中
- 一道机试字符串替换题
- 吝啬的国度--无向图,广度优先遍历,内存爆掉了
- 对回调函数的理解
- 史上最全oracle练习题
- 微信-连接一切的力量