lightoj 1112
来源:互联网 发布:生命起源知乎 编辑:程序博客网 时间:2024/04/30 23:53
Submit Status
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
Submit Status
代码:
#include <set>#include <map>#include <list>#include <cmath>#include <ctime>#include <deque>#include <queue>#include <stack>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <cassert>#include <cstdlib>#include <cstring>#include <sstream>#include <iostream>#include <algorithm>#define pi acos(-1.0)#define maxn (100000 + 50)#define Lowbit(x) (x & (-x))using namespace std;typedef long long int LLI;int a[maxn];int c[maxn];void Update(int x,int change,int n) { while(x <= n) { c[x] = c[x] + change; x = x + Lowbit(x); }}int Query(int l,int r) { int suml = 0,sumr = 0,nl = l - 1,nr = r; while(nr > 0) { sumr = sumr + c[nr]; nr = nr - Lowbit(nr); } while(nl > 0) { suml = suml + c[nl]; nl = nl - Lowbit(nl); } return sumr - suml;}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout); int t; scanf("%d",&t); for(int Case = 1; Case <= t; Case ++) { memset(a,0,sizeof(a)); memset(c,0,sizeof(c)); printf("Case %d:\n",Case); int n,q,sum = 0,temp; scanf("%d%d",&n,&q); for(int i = 1; i <= n; i ++) { scanf("%d",&temp); sum = sum + temp; a[i] = sum; c[i] = a[i] - a[i - Lowbit(i)]; } for(int i = n; i >= 1; i --) { a[i] = a[i] - a[i - 1]; } for(int i = 1; i <= q; i ++) { int Order,ii,jj; scanf("%d",&Order); if(Order == 1) { scanf("%d",&ii); Update(ii + 1,-a[ii + 1],n); printf("%d\n",a[ii + 1]); a[ii + 1] = 0; } else if(Order == 2) { scanf("%d%d",&ii,&jj); a[ii + 1] = a[ii + 1] + jj; Update(ii + 1,jj,n); } else { scanf("%d%d",&ii,&jj); printf("%d\n",Query(ii + 1,jj + 1)); } } } return 0;}
- LightOj 1112
- lightoj 1112
- LightOJ 1112
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- [LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- 【数据结构与算法07】哈希表
- 【Android】如何写一个JsBridge
- 写两点心得
- Android BottomNavigationBar的使用
- ps铅笔素描效果教程
- lightoj 1112
- eerer
- java中常用的包
- 21. 投票(poll)
- 对deep learning的第一周调研
- cmd 小技巧
- Hadoop学习九:hdfs源代码调试
- 【红宝书笔记】 创建对象的几种方法
- cmd中添加中文字体