Lightoj1112——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
Output for 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
Case 1:
5
14
1
13
2

羅賓漢有n個袋子,他只有三種行爲,把一個袋子裏的財物都分給窮人;把一個袋子的財物加上一定的數目;求一個連續區間的袋子的財物總數
還是很容易看出是線段樹的,然而第二次嘗試純記憶手寫還是失敗。。。

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <math.h>
#include <algorithm>
#include <queue>
#include <iomanip>
#include <map>
#define INF 0x3f3f3f3f
#define MAXN 100005
#define Mod 20007
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
long long sum[MAXN<<2];
void pushup(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%lld",&sum[rt]);
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int p,int num,int l,int r,int rt)
{
    if(l==r)
    {
        if(num<0)
        {
            printf("%lld\n",sum[rt]);
            sum[rt]=0;
        }
        else
            sum[rt]+=num;
        return;
    }
    int m=(l+r)>>1;
    if(p<=m)
        update(p,num,lson);
    else
        update(p,num,rson);
    pushup(rt);
}
long long query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        return sum[rt];
    }
    int m=(l+r)>>1;
    long long ret=0;
    if(L<=m)
        ret+=query(L,R,lson);
    if(R>m)
        ret+=query(L,R,rson);
    pushup(rt);
    return ret;
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int cas=1; cas<=t; ++cas)
    {
        int n,q;
        scanf("%d%d",&n,&q);
        build(1,n,1);
        printf("Case %d:\n",cas);
        while(q--)
        {
            int k,i,j;
            scanf("%d",&k);
            if(k==1)
            {
                scanf("%d",&i);
                update(i+1,-1,1,n,1);
            }
            else if(k==2)
            {
                scanf("%d%d",&i,&j);
                update(i+1,j,1,n,1);
            }
            else
            {
                scanf("%d%d",&i,&j);
                printf("%lld\n",query(i+1,j+1,1,n,1));
            }
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章