树状数组 区间修改区间查询模板

POJ - 3468

http://poj.org/problem?id=3468

#include<cstdio>
using namespace std;
#define int long long
using namespace std;
const int maxn = 1e5 + 10;
int tr[2][maxn], sum[maxn];
int lowbit(int x){
    return x & (-x);
}
void add(int x, int t, int k){
    while(x <= maxn){
        tr[k][x] += t;
        x += lowbit(x);
    }
}
int ask(int x, int k){
    int sum = 0;
    while(x > 0){
        sum += tr[k][x];
        x -= lowbit(x);
    }
    return sum;
}
signed main(){
    int n, m;
    scanf("%lld%lld",&n,&m);
    for(int i = 1; i <= n; i++){
        scanf("%lld",&sum[i]);
        sum[i] += sum[i - 1];
    }
    while(m--){
        char c[2];
        int l, r;
        scanf("%s%lld%lld",c,&l,&r);
        if(c[0] == 'C'){
            int a;
            scanf("%lld",&a);
            add(l, a, 0);
            add(r + 1, -a, 0);
            add(l, l * a, 1);
            add(r + 1, -(r + 1) * a, 1);
        }
        else {
            int ans = sum[r] + (r + 1) * ask(r, 0) - ask(r, 1);
            ans -= sum[l - 1] + l * ask(l - 1, 0) - ask(l - 1, 1);
            printf("%lld\n",ans);
        }
    }
    return 0;
}

 

posted @ 2020-11-07 10:25  国服第一混子  阅读(42)  评论(0)    收藏  举报