树状数组

· · 算法·理论

1 前缀和树状数组(单点更新区间查找)

例题P3374【模板】树状数组 1

#include<bits/stdc++.h>
using namespace std;
#define intc const int
#define intl long long
#define Cios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define lowbit(x) x&(-x)
intc N=5e5+5;
int n,m,a[N],s[N];
void update(int x,int d){
    while(x<=n){
        s[x]+=d;
        x+=lowbit(x);
    }
}
int check(int x){
    int ans=0;
    while(x){
        ans+=s[x];
        x-=lowbit(x);
    }
    return ans;
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) update(i,a[i]);
    while(m--){
        int q,x,y;
        cin>>q>>x>>y;
        if(q==1) update(x,y);
        if(q==2) cout<<check(y)-check(x-1)<<endl;
    }
    return 0;
}

2 差分树状数组(区间更新单点查找)

例题P3368 【模板】树状数组 2

#include<bits/stdc++.h>
using namespace std;
#define intc const int
#define intl long long
#define Cios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) // 优化输入输出
#define lowbit(x) x&(-x) // 获取x的二进制表示中最低位的1对应的值

intc N = 5e5 + 5;    // 定义数组最大长度
int n, m, a[N], s[N]; // a为原始数组,s为树状数组(维护差分数组)

// 树状数组更新操作:在位置x添加增量d
void update(int x, int d) {
    while (x <= n) {
        s[x] += d;       // 更新当前节点
        x += lowbit(x);   // 跳转到父节点,向上更新
    }
}

// 查询前缀和:计算位置x的前缀和(即差分数组的累积效应)
int check(int x) {
    int ans = 0;
    while (x) {
        ans += s[x];      // 累加路径上的值
        x -= lowbit(x);   // 跳转到前一个区间,向左查询
    }
    return ans;
}

int main() {
    Cios; // 优化IO
    cin >> n >> m;
    for (int i = 1; i <= n; i++) 
        cin >> a[i]; // 读取初始数组

    while (m--) {
        int q, x, y, k;
        cin >> q;
        if (q == 1) { // 区间更新操作:[x,y]区间增加k
            cin >> x >> y >> k;
            update(x, k);     // 差分数组x处+k,影响后续所有包含x的区间
            update(y + 1, -k);// y+1处-k,确保更新只作用在[x,y]区间
        } 
        else if (q == 2) { // 单点查询操作
            cin >> x;
            // 当前值 = 原始值 + 差分数组前缀和(累计的修改量)
            cout << a[x] + check(x) << endl;
        }
    }
    return 0;
}

3 仿线段树树状数组(区间修改区间查询)

例题P3372 【模板】线段树 1

#include<bits/stdc++.h>
using namespace std;
#define intc const int
#define intl signed
#define int long long
#define Cios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define lowbit(x) x&(-x)
intc N=1e5+5;
int n,m,a[N],s1[N],s2[N];
void update1(int x,int d){
    while(x<=n){
        s1[x]+=d;
        x+=lowbit(x);
    }
}
void update2(int x,int d){
    while(x<=n){
        s2[x]+=d;
        x+=lowbit(x);
    }
}
int check1(int x){
    int ans=0;
    while(x){
        ans+=s1[x];
        x-=lowbit(x);
    }
    return ans;
}
int check2(int x){
    int ans=0;
    while(x){
        ans+=s2[x];
        x-=lowbit(x);
    }
    return ans;
}
intl main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        update1(i,a[i]-a[i-1]);
        update2(i,(i-1)*(a[i]-a[i-1]));
    }
    while(m--){
        int q,x,y,k;
        cin>>q>>x>>y;
        if(q==1){
            cin>>k;
            update1(x,k);
            update1(y+1,-k);
            update2(x,k*(x-1));
            update2(y+1,-k*y);
        }
        if(q==2){
            cout<<(check1(y)*y-check2(y))-(check1(x-1)*(x-1)-check2(x-1))<<endl;
        }
    }
    return 0;
}