LightOJ - 1112 Curious Robin Hood [interval tree]

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

ps:

这个题刚开始傻逼的把根节点坐标设置成了0,然后各种找不到孩子。。。
然后注意一下所开数组的大小,n << 1 绝对是不够的,因为树显而易见的会比你预想的要多出来一层。
大概开个3n、4n的样子的就差不多够用了。
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

#define maxn 100001
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
//这个 rt << 1 | 1 就是rt * 2 + 1 的意思。 位运算要比乘法快很多很多
// ps: 位运算 都是对二进制数的最后一位进行运算的。
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){ // 线段树的建立
//    cout << "$$$ L: "<< l << "   R: " << r <<"   rt:" << rt  << endl;
    if(l == r){
        scanf("%d" , &sum[rt]);
//        cout << "##INPUT : sum[" << rt << "]= " << sum[rt] << "    L: "<< l << "   R: " << r << endl;
    }
    else{
        int m;
        m = (l + r) >> 1;
        buildTree(lson);
        buildTree(rson);
        pushUp(rt);
    }
}

void addon(int p, int add, int l, int r, int rt){ // 数值更新 这里的 p 的寻找原理是把这玩意看作是区间,大概就是这个意思
    if(l == r){
        sum[rt] += add;
    }
    else{
        int m = (l + r) >> 1;
        if(p <= m)
           addon(p, add, lson);
        else
           addon(p, add, rson);
        pushUp(rt);
    }
}

int Query(int L, int R, int l, int r, int rt)// L R 是查询区间
{ //
    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 argc, const char * argv[]) {
    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);
//        cout << "---------------TREE--------------------" << endl;
//        for(int i = 1; i < n*3; i++){
//            cout << sum[i] << " " << endl;
//        }
//        cout << "----------------END----------------------" <<endl;
        for(int i = 0; i < q; i++){
            int action;
            scanf("%d",&action);
            if(action == 1){
                int a;
                scanf("%d", &a);
                delet(a+1, 1, n, 1); //a+1是因为题目给的坐标范围是[0.n)的。
//                cout << "---------------TREE--------------------" << endl;
//                for(int i = 1; i < n*3; i++){
//                    cout << sum[i] << " " << endl;
//                }
//                cout << "----------------END----------------------" <<endl;
            }
            if(action == 2){
                int a,b;
                scanf("%d%d",&a,&b);
                addon(a+1, b, 1, n , 1);

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

    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值