树状数组测试总结

· · 个人记录

1.U438237 ١١(❛ᴗ❛)【树状数组+线段树】-模板(单点修改+区间查询)

纯模版,直接 AC 了

Ac Code

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+7;
int n,q;
int c[N];
int lowbit(int x){
    return x&(-x);
}
void modify(int x,int v){
    while(x<=n){
        c[x]+=v;
        x+=lowbit(x);
    }
    return;
}
int query(int x){
    int sum=0;
    while(x){
        sum+=c[x];
        x-=lowbit(x);
    }
    return sum;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
//  freopen("T1.in","r",stdin);
//  freopen("T1.out","w",stdout);
    cin>>n>>q;
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        modify(i,x);
    }
    while(q--){
        int op,x,y;
        cin>>op>>x>>y;
        if(op==1){
            modify(x,y);
        }
        else{
            cout<<query(y)-query(x-1)<<'\n';    
        }
    }
    return 0;
} 

2.U438249 ١١(❛ᴗ❛)【树状数组】-清点人数

在模版的基础上改下输入就行了,也 AC 了

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+7;
int n,k;
int c[N];
int lowbit(int x){
    return x&(-x);
}
void modify(int x,int v){
    while(x<=n){
        c[x]+=v;
        x+=lowbit(x);
    }
    return;
}
int query(int x){
    int sum=0;
    while(x){
        sum+=c[x];
        x-=lowbit(x);
    }
    return sum;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
//  freopen("T2.in","r",stdin);
//  freopen("T2.out","w",stdout);
    cin>>n>>k;
    while(k--){
        char op;
        cin>>op;
        if(op=='A'){
            int m;
            cin>>m;
            cout<<query(m)<<'\n';
        }
        else if(op=='B'){
            int m,p;
            cin>>m>>p;
            modify(m,p);
        }
        else{
            int m,p;
            cin>>m>>p;
            modify(m,-p);
        }
    }
    return 0;
}

3.U584613 树状数组2

区间修改,单点查询,运用到差分 + 树状数组

输入时忘差分,0 分

Ac Code

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+7;
int n,q;
int a[N],c[N];
int lowbit(int x){
    return x&(-x);
}
void modify(int x,int v){
    while(x<=n){
        c[x]+=v;
        x+=lowbit(x);
    }
    return;
}
int query(int x){
    int sum=0;
    while(x){
        sum+=c[x];
        x-=lowbit(x);
    }
    return sum;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
//  freopen("T3.in","r",stdin);
//  freopen("T3.out","w",stdout);
    cin>>n>>q;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        modify(i,a[i]-a[i-1]);//差分
    }
    while(q--){
        int op;
        cin>>op;
        if(op==1){
            //树状数组操作跟前缀和,差分同理
            int l,r,x;
            cin>>l>>r>>x;
            modify(l,x);
            modify(r+1,-x);
        }
        else{
            int i;
            cin>>i;
            cout<<query(i)<<'\n';//输出
        }
    }
    return 0;
}

4.U438285 ١١(❛ᴗ❛)【树状数组】-数星星 Stars

上课讲过,AC 了

Ac Code

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+7;
int n,q;
int c[N],ans[N];
int lowbit(int x){
    return x&(-x);
}
void modify(int x,int v){
    while(x<=40000){
        c[x]+=v;
        x+=lowbit(x);
    }
    return;
}
int query(int x){
    int sum=0;
    while(x){
        sum+=c[x];
        x-=lowbit(x);
    }
    return sum;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //freopen("T4.in","r",stdin);
    //freopen("T4.out","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++){
        int x,y;
        cin>>x>>y;
        int sum=0;
        x++;
        sum=query(x);
        ans[sum]++;
        modify(x,1);
    }
    for(int i=0;i<n;i++){
        cout<<ans[i]<<'\n';
    }
    return 0;
}

5.U438255 ١١(❛ᴗ❛)【树状数组】-校门外的树

上课讲过,即用两个树状数组,一个存左括号,一个存右括号,AC 了

Ac Code

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e4+7;
int n,m;
int c[N][2];
int ans[N];
int lowbit(int x){
    return x&(-x);
}
void modify(int x,int v,int cnt){
    while(x<=n){
        c[x][cnt]+=v;
        x+=lowbit(x);
    }
    return;
}
int query(int x,int cnt){
    int sum=0;
    while(x){
        sum+=c[x][cnt];
        x-=lowbit(x);
    }
    return sum;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
//  freopen("T5.in","r",stdin);
//  freopen("T5.out","w",stdout);
    cin>>n>>m;
    while(m--){
        int op,x,y;
        cin>>op>>x>>y;
        if(op==1){
            modify(x,1,0);
            modify(y,1,1);
        }
        else{
            cout<<query(y,0)-query(x-1,1)<<'\n';
        }
    }
    return 0;
}