LightOj 1112


Curious Robin Hood
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit Status Practice LightOJ 1112



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
<span style="color:#6600cc;">/*****************************************************

       author   :   Grant Yuan
       time     :   2014.7.27
       algorithm;   线段树
       
*****************************************************/

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define MAX 100005

using namespace std;
struct node{
int l;
int r;
int sum;
};
node tree[3*MAX];
int b[MAX];
int t,n,q;
long long ans;
int ct=1;

void build(int left,int right,int root)
{
    tree[root].l=left;
    tree[root].r=right;
    if(left==right){
        tree[root].sum=b[left];
        return;
    }
    int mid=(left+right)>>1;
    build(left,mid,root*2);
    build(mid+1,right,root*2+1);
    tree[root].sum=tree[root*2].sum+tree[root*2+1].sum;
}

void Add(int point,int root,int num)
{
   if(tree[root].l==tree[root].r)
        {
            tree[root].sum+=num;
            return;
        }
   int mid=(tree[root].l+tree[root].r)>>1;
   if(mid>=point)
     Add(point,root*2,num);
   else if(point>mid)
     Add(point,root*2+1,num);
     tree[root].sum=tree[root*2].sum+tree[root*2+1].sum;
}

void cle(int point,int root)
{
   if(tree[root].l==tree[root].r)
        {   ans= tree[root].sum;
            tree[root].sum=0;
            return;
        }
   int mid=(tree[root].l+tree[root].r)>>1;
   if(mid>=point)
     cle(point,root*2);
   else if(point>mid)
     cle(point,root*2+1);
    tree[root].sum=tree[root*2].sum+tree[root*2+1].sum;
}

void qiuhe(int left,int right,int root)
{
  if(tree[root].l>right||tree[root].r<left)
        return;
  if(left==tree[root].l&&right==tree[root].r)
        {ans+=tree[root].sum;
        return;}
  int mid=(tree[root].l+tree[root].r)>>1;
  if(mid<left) qiuhe(left,right,root*2+1);
  else if(mid>=right) qiuhe(left,right,root*2);
  else{
     qiuhe(left,mid,root*2);
     qiuhe(mid+1,right,root*2+1);
  }

}

int main()
{  int aa,bb,s;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&q);
        for(int i=0;i<n;i++)
            scanf("%d",&b[i]);
        build(0,n-1,1);
        printf("Case %d:
",ct++);
            for(int i=0;i<q;i++)
              {scanf("%d",&s);
              if(s==1){
                    ans=0;
                    scanf("%d",&aa);
                  cle(aa,1);
                  printf("%lld
",ans);
           }
              else if(s==2){
                scanf("%d%d",&aa,&bb);
                Add(aa,1,bb);
              }
              else if(s==3){
                ans=0;
                scanf("%d%d",&aa,&bb);
                qiuhe(aa,bb,1);
                printf("%lld
",ans);

              }}
    }
    return 0;
}
</span>


原文地址:https://www.cnblogs.com/codeyuan/p/4254478.html