树状数组测试

· · 个人记录

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

  1. 原题,不解释。
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=5e6+7;
int a[maxn],c[maxn],n,q;
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);
    cin>>n>>q;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        modify(i,a[i]);
    }
    for(int i=1;i<=q;i++){
        int w,l,r;
        cin>>w>>l>>r;
        if(w==1){
            modify(l,r); 
        }else{
            cout<<query(r)-query(l-1)<<"\n";
        }
    }
    return 0;
} 

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

  1. 为"B"时加上y,为"c"时减y。
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=5e5+7;
int a[maxn],c[maxn],n,q;
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);
    cin>>n>>q;
    for(int i=1;i<=q;i++){
        char w;
        int l,r;
        cin>>w;
        if(w=='A'){
            cin>>l;
            cout<<query(l)<<"\n";
        }else if(w=='B'){
            cin>>l>>r;
            modify(l,r);
        }else{
            cin>>l>>r;
            modify(l,-r);
        }
    }
    return 0;
} 

[U584613 树状数组2]()

  1. 将c数组维护成差分数组。
    #include<bits/stdc++.h>
    using namespace std;
    #define int long long
    const int maxn=5e6+7;
    int a[maxn],c[maxn],n,q;
    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);
     cin>>n>>q;
     for(int i=1;i<=n;i++){
         cin>>a[i];
         modify(i,a[i]);
         modify(i+1,-a[i]);
     }
     for(int i=1;i<=q;i++){
         int w,l,r,x;
         cin>>w;
         if(w==1){
             cin>>l>>r>>x;
             modify(l,x);
             modify(r+1,-x);
         }else{
             cin>>l;
             cout<<query(l)<<"\n";
         }
     }
     return 0;
    } 

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

  2. 原题,不解释。
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e5+7;
int c[maxn],sum[maxn],n,q;
struct ww{
    int x,y;
}a[maxn];
int lowbit(int x){
    return (x&(-x));
}
void modify(int x,int v){
    while(x<=maxn){
        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);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].x>>a[i].y;
        a[i].x++;
        sum[query(a[i].x)]++;
        modify(a[i].x,1);
    }
    for(int i=0;i<=n-1;i++){
        cout<<sum[i]<<"\n";
    }
    return 0;
} 

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

  1. 原题,不解释。
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e5+7;
int a[maxn],c[maxn][2],n,q;
int lowbit(int x){
    return (x&(-x));
}
void modify(int x,int v,int i){
    while(x<=maxn){
        c[x][i]+=v;
        x+=lowbit(x);
    }
    return ;
}
int query(int x,int i){
    int sum=0;
    while(x){
        sum+=c[x][i];
        x-=lowbit(x);
    }
    return sum;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>q;
    for(int i=1;i<=q;i++){
        int w,l,r;
        cin>>w;
        if(w==1){
            cin>>l>>r;
            modify(l,1,1);
            modify(r,1,2);
        }else{
            cin>>l>>r;
            cout<<query(r,1)-query(l-1,2)<<"\n";
        }
    }
    return 0;
} 

P8165 [eJOI 2021] AddK

  1. 使用差分数组,和一个公式。
  2. 公式推导过程
#include <bits/stdc++.h>
using namespace std;
#define l1 long long
const int maxn=5e6+7;
l1 a[maxn];
int id[maxn];
l1 A[maxn][2];
inline int lowbit(int x){
    return x&(-x);
}
l1 query(int x,int type){
    l1 sum=0;
    while(x>0){
        sum+=A[x][type];
        x=x-lowbit(x);
    }
    return sum;
}

void modify(int idx,l1 math,int k,int type){
    while (idx <= k){
        A[idx][type]+=math;
        idx+=lowbit(idx);
    }
}
l1 cal(int x){
    if(x<0){
        return 0;
    }
    return x*query(x,0)-query(x,1);
}
int main(){
    int n,k;
    cin >>n>>k;
    for (int i=1;i<=n;i++){
        cin>>a[i];
        modify(i,a[i],n,0);
        modify(i,(l1)(i-1)*a[i],n,1);
    }
    int t;
    cin>>t;
    while(t--){
        int op;
        cin>>op;
        if(op==1){
            for(int i=1;i<=k;i++){
                cin>>id[i];
            }
            int tmp=a[id[1]];
            for(int i=1;i<k;i++){
                modify(id[i],a[id[i+1]]-a[id[i]],n,0);
                modify(id[i],(l1)(a[id[i+1]]-a[id[i]])*(id[i]-1),n,1);
                a[id[i]]=a[id[i+1]];
            }
            modify(id[k],tmp-a[id[k]],n,0);
            modify(id[k],(l1)(tmp-a[id[k]])*(id[k]-1),n,1);
            a[id[k]]=tmp;
        }else{
            int l,r,m;
            cin>>l>>r>>m;
            cout<<cal(r)-cal(l+m-2)-(cal(r-m)-cal(l-2))<<"\n";
        }
    }
    return 0;
}

P7629 [COCI 2011/2012 #1] SORT

  1. 题目给我们的是不规则的数列,让我们更改逆序对的个数确定最后的答案。
  2. 来张具体解释,
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int Max=1e6+10;
int n,m;
int a[Max];
struct bt{
    int c[Max];
    int low(int x){
        return (x&(-x));
    }
    int query(int x){
        int sum=0;
        while(x){
            sum+=c[x];
            x-=low(x);
        }
        return sum;
    }
    void modify(int x,int v){
        while(x<=n){
            c[x]+=v;
            x+=low(x);
        }
        return ;
    }
}T;
int ans=0;
void tf(int l,int r){
    while(l<=r){
        swap(a[l],a[r]);
        ++l,--r;
    }
    ans++;
    return ;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    int las=1;
    for(int i=2;i<=n;i++){
        if(a[i]>a[i-1]){
            tf(las,i-1);
            las=i;
        }
    }
    tf(las,n);
    for(int i=n;i>=1;--i){
        ans+=T.query(a[i]);
        T.modify(a[i],1);
    }
    cout<<ans<<"\n";
    return 0;
}