【模板】树状数组2

此题为洛谷P3368

https://www.luogu.org/problemnew/show/P3368

有些一些是转载于此:

https://www.cnblogs.com/hsd-/p/6139376.html

顺带总结一下最近学的树状数组(笑)

#include<bits/stdc++.h>
using namespace std;
int n,m;
long long a[50000005];
int lowbit(int x){
    return x&(-x);//-t 代表t的负数 计算机中负数使用对应的正数的补码来表示
}//印象中是树状数组最主要的结构!!!
//lowbit(x) 其实就是取出x的最低位1 
void add(long long p,long long x){
    while(p<=n)
    {
        a[p]+=x;
        p+=lowbit(p);
    }//可以发现 更新过程是查询过程的逆过程
    //由叶子结点向上更新C[]数组
}
int find(long long p){
    long long ans=0;
    while(p>0)
    {
        ans+=a[p];
        p-=lowbit(p);
    }//利用C[i]数组,求A数组中前i项的和
    return ans;
}
int main(){
    scanf("%d%d",&n,&m);
    long long last = 0, now;
    for (int i = 1; i <= n; i++) {
        scanf("%lld", &now);
        add(i, now - last);
        last = now;
    }
    int f;
    while (m--) {
        scanf("%d", &f);
        if (f == 1) {
            int x, y;
            long long k;
            scanf("%d%d%lld", &x, &y, &k);
            add(x, k);
            add(y + 1, -k);
        } else if (f==2) {
            int t;
            scanf("%d", &t);
            printf("%lld\n", find(t));
        }
    }
    return 0;
}

作为小juluo的我竟然自己总结(假的)

希望对每个和我一样的juluo又一点想法,嘻嘻!!!

猜你喜欢

转载自www.cnblogs.com/U58223-luogu/p/9462331.html