Curious Robin Hood【线段树单点更新】

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

题意:
输入T组测试数据:
N个数据,Q个操作;
1,i 将编号为i的数变成0,并且输出被改变的点的数值;
2,i,v 将编号为i的数加v;
3,i,j 输出i~j之间的总和;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define maxn 100001
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

int sum[maxn << 2];

void pushUp(int rt)
{
    sum[rt] = sum[rt << 1] + sum[rt << 1|1];
}

void buildTree(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%d", &sum[rt]);
    }
    else
    {
        int m;
        m=(l + r)>>1;
        buildTree(lson);
        buildTree(rson);
        pushUp(rt);
    }
}

void update(int p,int add,int l,int r,int rt)
{
    if(l == r)
    {
        sum[rt]+=add;
    }
    else
    {
        int m = (l + r) >> 1;
        if(p <= m)
            update(p, add, lson);
        else
            update(p, add, rson);
        pushUp(rt);
    }
}

int Query(int L, int R, int l, int r, int rt)
{

    if (L <= l && r <= R)
    {
        return sum[rt];
    }
    int m = (l + r) >> 1;
    int ans = 0;
    if (L <= m)
        ans += Query(L, R, lson);
    if (R >m)
        ans += Query(L, R, rson);
    return ans;
}


void delet(int p, int l, int r, int rt)
{
    if(l == r)
    {
        printf("%d\n",sum[rt]);
        sum[rt] = 0;
    }
    else
    {
        int m=(l + r)>>1;
        if(p <= m)
            delet(p,lson);
        else
            delet(p,rson);
        pushUp(rt);
    }
}




int main()
{
    int t;
    scanf("%d", &t);
    for(int cases = 1; cases <= t; cases++ )
    {
        cout << "Case " << cases << ":" << endl;
        int n,q;
        scanf("%d%d", &n, &q);
        buildTree(1, n, 1);

        for(int i = 0; i < q; i++)
        {
            int k;
            scanf("%d",&k);
            if(k== 1)
            {
                int a;
                scanf("%d", &a);
                delet(a+1, 1, n, 1);
            }
            if(k==2)
            {
                int a,b;
                scanf("%d%d",&a,&b);
                update(a+1,b,1,n,1);

            }
            if(k==3)
            {
                int a, b;
                scanf("%d%d",&a,&b);
                printf("%d\n",Query(a+1,b+1,1,n,1));
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/li_hongcheng/article/details/79823175