DescriptionRobin 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. InputInput 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). OutputFor 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 Input1 5 6 3 2 1 4 5 1 4 2 3 4 3 0 3 1 2 3 0 4 1 1 Sample OutputCase 1: 5 14 1 13 2 |
代码:
#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;
}