• Curious Robin Hood(树状数组+线段树)


    1112 - Curious Robin Hood
    Time Limit: 1 second(s) Memory Limit: 64 MB

    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

    Notes

    Dataset is huge, use faster I/O methods.

    题解:简单的树状数组;线段树也很简单;

    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).

    树状数组代码:

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    using namespace std;
    const int INF=0x3f3f3f3f;
    #define mem(x,y) memset(x,y,sizeof(x))
    #define SI(x) scanf("%d",&x)
    #define PI(x) printf("%d",x)
    typedef long long LL;
    const int MAXN=1e5+100;
    int tree[MAXN];
    int lowbit(int x){return x&(-x);}
    void add(int x,int v){
        while(x<MAXN){
            tree[x]+=v;
            x+=lowbit(x);
        }
    }
    int SUM(int x){
        int sum=0;
        while(x>0){
            sum+=tree[x];
            x-=lowbit(x);
        }
        return sum;
    }
    int main(){
        int T,kase=0;
        int N,M;
        SI(T);
        while(T--){
            SI(N);SI(M);
            mem(tree,0);
            for(int i=1;i<=N;i++){
                int temp;
                SI(temp);
                add(i,temp);
            }
            printf("Case %d:
    ",++kase);
            while(M--){
                int q,a,b,ans;
                SI(q);
                if(q==1){
                    SI(a);a++;
                    ans=SUM(a)-SUM(a-1);
                    add(a,-ans);
                    printf("%d
    ",ans);
                }
                else if(q==2){
                    SI(a);SI(b);
                    a++;
                    add(a,b);
                }
                else{
                    SI(a);SI(b);a++;b++;
                    printf("%d
    ",SUM(b)-SUM(a-1));
                }
            }
        }
        return 0;
    }

     线段树:

    /*
    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    using namespace std;
    const int INF=0x3f3f3f3f;
    #define mem(x,y) memset(x,y,sizeof(x))
    #define SI(x) scanf("%d",&x)
    #define PI(x) printf("%d",x)
    typedef long long LL;
    const int MAXN=1e5+100;
    int tree[MAXN];
    int lowbit(int x){return x&(-x);}
    void add(int x,int v){
        while(x<MAXN){
            tree[x]+=v;
            x+=lowbit(x);
        }
    }
    int SUM(int x){
        int sum=0;
        while(x>0){
            sum+=tree[x];
            x-=lowbit(x);
        }
        return sum;
    }
    int main(){
        int T,kase=0;
        int N,M;
        SI(T);
        while(T--){
            SI(N);SI(M);
            mem(tree,0);
            for(int i=1;i<=N;i++){
                int temp;
                SI(temp);
                add(i,temp);
            }
            printf("Case %d:
    ",++kase);
            while(M--){
                int q,a,b,ans;
                SI(q);
                if(q==1){
                    SI(a);a++;
                    ans=SUM(a)-SUM(a-1);
                    add(a,-ans);
                    printf("%d
    ",ans);
                }
                else if(q==2){
                    SI(a);SI(b);
                    a++;
                    add(a,b);
                }
                else{
                    SI(a);SI(b);a++;b++;
                    printf("%d
    ",SUM(b)-SUM(a-1));
                }
            }
        }
        return 0;
    }
    */
    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    using namespace std;
    const int INF=0x3f3f3f3f;
    #define mem(x,y) memset(x,y,sizeof(x))
    #define SI(x) scanf("%d",&x)
    #define PI(x) printf("%d",x)
    typedef long long LL;
    const int MAXN=1e5+100;
    #define ll root<<1
    #define rr root<<1|1
    #define lson ll,l,mid
    #define rson rr,mid+1,r
    int tree[MAXN<<2];
    int ans;
    void pushup(int root){
        tree[root]=tree[ll]+tree[rr];
    }
    void build(int root,int l,int r){
        int mid=(l+r)>>1;
        if(l==r){
            SI(tree[root]);
            return;
        }
        build(lson);build(rson);
        pushup(root);
    }
    void update(int root,int l,int r,int A,int B){
        if(l==A&&r==A){
            tree[root]+=B;
            return;
        }
        int mid=(l+r)>>1;
        if(mid>=A)update(lson,A,B);
        else update(rson,A,B);
        pushup(root);
    }
    void query(int root,int l,int r,int A,int B){
        if(l>=A&&r<=B){
            ans+=tree[root];
            return ;
        }
        int mid=(l+r)>>1;
        if(mid>=A)query(lson,A,B);
        if(mid<B)query(rson,A,B);
    }
    int main(){
        int T;
        int N,M,kase=0;
        SI(T);
        while(T--){
            SI(N);SI(M);
            build(1,1,N);
            printf("Case %d:
    ",++kase);
            while(M--){
                int q,a,b;
                SI(q);
                if(q==1){
                    SI(a);a++;
                    ans=0;
                    query(1,1,N,a,a);
                    printf("%d
    ",ans);
                    update(1,1,N,a,-ans);
                }
                else if(q==2){
                    SI(a);SI(b);
                    a++;
                    update(1,1,N,a,b);
                }
                else{
                    SI(a);SI(b);a++;b++;
                    ans=0;
                    query(1,1,N,a,b);
                    printf("%d
    ",ans);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    通过kinaba对es进行crud
    安装启动kibana
    es安装启动
    IP网络层
    Java 高级文本处理
    django17
    邮件问题
    django16
    django15
    django14
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5236697.html
Copyright © 2020-2023  润新知