树状数组模板题
题单链接:树状数组模板题 -
题单 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
可以先看这篇文章学习或者复习一下:
【朝夕的ACM笔记】数据结构-树状数组
- 知乎 (zhihu.com)
#include<bits/stdc++.h> #define lowbit(x) (x&(-x)) using ll=long long; const int N=5e4+10; const int MAXN=5e5+10; const int INF=0x3f3f3f3f; const int mod=19650827; const int MAX=270007; typedef std::pair<int,int> pii; typedef std::pair<double,int> pdi; int a[MAXN]; int t[MAXN];
 
 
 
 
 
 
 
  void update(int x,int k,int n) {     while(x<=n)     {         t[x]+=k;         x+=lowbit(x);     } }
 
 
  int query(int x) {     int ans=0;     while(x)     {         ans+=t[x];         x-=lowbit(x);     }     return ans; }
  void build(int n) {     for(int i=1;i<=n;i++)     {         t[i]+=a[i];         int j=i+lowbit(i);         if(j<=n)    t[j]+=t[i];     } } void solve() {     int n,m;    std::cin>>n>>m;     for(int i=1;i<=n;i++)   std::cin>>a[i];     build(n);     for(int i=1;i<=m;i++)     {         int opt,x,y;    std::cin>>opt>>x>>y;         if(opt==1)         {             update(x,y,n);         }         if(opt==2)         {             std::cout<<query(y)-query(x-1)<<"\n";         }     } } int main(){     std::ios::sync_with_stdio(false),std::cin.tie(nullptr);     int tt=1;          while(tt--)solve();     return 0; }
   | 
 
#include<bits/stdc++.h> #define lowbit(x) (x&(-x)) using ll=long long; const int N=5e4+10; const int MAXN=1e6+10; const int INF=0x3f3f3f3f; const int mod=19650827; const int MAX=270007; typedef std::pair<int,int> pii; typedef std::pair<double,int> pdi; ll a[MAXN]; ll d[MAXN]; ll t[MAXN]; void update(int x,int k,int n) {     while(x<=n)     {         t[x]+=k;         t[x]=t[x]%2;         x+=lowbit(x);     } } ll query(int x,int n) {     ll ans=0;     while(x)     {         ans+=t[x];         x-=lowbit(x);         ans%=2;     }     return ans; } void build(int n) {     for(int i=1;i<=n;i++)     {         t[i]+=d[i];         int j=i+lowbit(i);         if(j<=n)         {             t[j]+=t[i];             t[j]=t[j]%2;         }     } } void solve() {     int n,m;    std::cin>>n>>m;     build(n);     for(int i=1;i<=m;i++)     {         int opt;    std::cin>>opt;         if(opt==1)         {             int l,r;    std::cin>>l>>r;             update(l,1,n);             update(r+1,1,n);         }         if(opt==2)         {             int x;  std::cin>>x;             std::cout<<query(x,n)<<"\n";         }     }
  } int main(){     std::ios::sync_with_stdio(false),std::cin.tie(nullptr);     int tt=1;          while(tt--)solve();     return 0; }
   | 
 
#include<bits/stdc++.h> #define lowbit(x) (x&(-x)) using ll=long long; const int N=5e4+10; const int MAXN=5e5+10; const int INF=0x3f3f3f3f; const int mod=19650827; const int MAX=270007; typedef std::pair<int,int> pii; typedef std::pair<double,int> pdi; int a[MAXN]; int d[MAXN]; int t[MAXN];
 
 
  void update(int x,int k,int n) {     while(x<=n)     {         t[x]+=k;         x+=lowbit(x);     } }
  int query(int x) {     int ans=0;     while(x)     {         ans+=t[x];         x-=lowbit(x);     }     return ans; }
  void build(int n) {     for(int i=1;i<=n;i++)     {         t[i]+=d[i];         int j=i+lowbit(i);         if(j<=n)    t[j]+=t[i];     } } void solve() {     int n,m;    std::cin>>n>>m;     for(int i=1;i<=n;i++)     {         std::cin>>a[i];         d[i]=a[i]-a[i-1];     }     build(n);     for(int i=1;i<=m;i++)     {         int opt;    std::cin>>opt;         if(opt==1)         {             int x,y,k;  std::cin>>x>>y>>k;             update(x,k,n);             update(y+1,-k,n);         }         if(opt==2)         {             int x;  std::cin>>x;             std::cout<<query(x)<<"\n";         }     } } int main(){     std::ios::sync_with_stdio(false),std::cin.tie(nullptr);     int tt=1;          while(tt--)solve();     return 0; }
   | 
 
#include<bits/stdc++.h> #define lowbit(x) (x&(-x)) using ll=long long; const int N=5e4+10; const int MAXN=1e5+10; const int INF=0x3f3f3f3f; const int mod=19650827; const int MAX=270007; typedef std::pair<int,int> pii; typedef std::pair<double,int> pdi; ll d[MAXN]; ll t[MAXN]; void update(int x,ll k,int n) {     while(x<=n)     {         t[x]+=k;         x+=lowbit(x);     } } ll query(ll x) {     ll ans=0;     while(x)     {         ans+=t[x];         x-=lowbit(x);     }     return ans; } void build(int n) {     for(int i=1;i<=n;i++)     {         t[i]+=d[i];         int j=i+lowbit(i);         if(j<=n)         {             t[j]+=t[i];         }     } } void solve() {     int n,w;    std::cin>>n>>w;     build(n);     for(int i=1;i<=w;i++)     {         char opt;   std::cin>>opt;         if(opt=='x')         {             int a,b;  std::cin>>a>>b;             update(a,b,n);         }         if(opt=='y')         {             int a,b;  std::cin>>a>>b;             std::cout<<query(b)-query(a-1)<<"\n";         }     } } int main(){     std::ios::sync_with_stdio(false),std::cin.tie(nullptr);     int tt=1;          while(tt--)solve();     return 0; }
   | 
 
#include<bits/stdc++.h> #define lowbit(x) (x&(-x)) using ll=long long; const int N=5e4+10; const int MAXN=1e7+10; const int INF=0x3f3f3f3f; const int mod=19650827; const int MAX=270007; typedef std::pair<int,int> pii; typedef std::pair<double,int> pdi; ll t[MAXN]; void update(int x,ll k,int n) {     while(x<=n)     {         t[x]+=k;         x+=lowbit(x);     } } ll query(int x) {     ll ans=0;     while(x)     {         ans+=t[x];         x-=lowbit(x);     }     return ans; } void solve() {     int n,m;    std::cin>>n>>m;     while(m--)     {         int opt;    std::cin>>opt;         if(opt==0)         {             int a,b;    std::cin>>a>>b;             update(a,1,n);             update(b+1,-1,n);         }         if(opt==1)         {             int a;  std::cin>>a;             std::cout<<query(a)<<"\n";         }     }
  } int main(){     std::ios::sync_with_stdio(false),std::cin.tie(nullptr);     int tt=1;          while(tt--)solve();     return 0; }
   | 
 
#include<bits/stdc++.h> #define lowbit(x) (x&(-x)) using ll=long long; const int N=5e4+10; const int MAXN=1e5+10; const int INF=0x3f3f3f3f; const int mod=19650827; const int MAX=270007; typedef std::pair<int,int> pii; typedef std::pair<double,int> pdi; ll a[MAXN]; ll t1[MAXN]; ll t2[MAXN];
 
 
 
 
 
  void update(int x,ll k,int n) {     while(x<=n)     {         t1[x]=std::max(t1[x],k);         t2[x]=std::min(t2[x],k);         x+=lowbit(x);     } } ll query(int l,int r) {     ll maxn=0;     ll minn=0x3f3f3f3f;     while(l<=r)     {         while(r-lowbit(r)>=l)         {             maxn=std::max(maxn,t1[r]);             minn=std::min(minn,t2[r]);             r-=lowbit(r);         }         maxn=std::max(maxn,a[r]);         minn=std::min(minn,a[r]);         r--;     }     return maxn-minn; } void build(int n) {     for(int i=1;i<=n;i++)     {         update(i,a[i],n);     } } void solve() {     memset(t2,0x3f,sizeof(t2));     int n,q;    std::cin>>n>>q;     for(int i=1;i<=n;i++)     {         std::cin>>a[i];     }     build(n);     while(q--)     {         int x,y;    std::cin>>x>>y;         std::cout<<query(x,y)<<"\n";     } } int main(){     std::ios::sync_with_stdio(false),std::cin.tie(nullptr);     int tt=1;          while(tt--)solve();     return 0; }
   | 
 
#include<bits/stdc++.h> #define lowbit(x) (x&(-x)) using ll=long long; const int N=5e4+10; const int MAXN=1e7+10; const int INF=0x3f3f3f3f; const int mod=19650827; const int MAX=270007; typedef std::pair<int,int> pii; typedef std::pair<double,int> pdi; ll a[MAXN];
 
 
 
 
 
 
 
  struct BIT {     ll t[MAXN];     void update(int x,ll k,int n)     {         while(x<=n)         {             t[x]^=k;             x+=lowbit(x);         }     }     ll query(int x)     {         ll ans=0;         while(x)         {             ans^=t[x];             x-=lowbit(x);         }         return ans;     } }; BIT tree[2]; void solve() {     int n,q;    std::cin>>n>>q;     for(int i=1;i<=n;i++)     {         std::cin>>a[i];         tree[i&1].update(i,a[i],n);     }     while(q--)     {         int opt;    std::cin>>opt;         if(opt==1)         {             int i,j;  std::cin>>i>>j;             tree[i&1].update(i,a[i]^j,n),    a[i]=j;         }         if(opt==2)         {             int l,r;    std::cin>>l>>r;             if((l+r)&1)     std::cout<<0<<"\n";             else    std::cout<<(tree[l&1].query(r)^tree[l&1].query(l-1))<<"\n";         }     } } int main(){     std::ios::sync_with_stdio(false),std::cin.tie(nullptr);     int tt=1;          while(tt--)solve();     return 0; }
   | 
 
#include<bits/stdc++.h> #define lowbit(x) (x&(-x)) using ll=long long; const int N=5e4+10; const int MAXN=5e5+10; const int INF=0x3f3f3f3f; const int mod=19650827; const int MAX=270007; typedef std::pair<int,int> pii; typedef std::pair<double,int> pdi; struct node {     ll val;     int id; }a[MAXN]; bool cmp(node a,node b) {     if(a.val!=b.val)    return a.val<b.val;     return a.id<b.id; } ll b[MAXN]; ll t[MAXN];
 
 
 
 
  void update(ll x,int k,int n) {     while(x<=n)     {         t[x]+=k;         x+=lowbit(x);     } } ll query(ll x) {     ll ans=0;     while(x)     {         ans+=t[x];         x-=lowbit(x);     }     return ans; } void build(int n) {     for(int i=1;i<=n;i++)   update(b[i],1,n);    } void solve() {     int n;  std::cin>>n;     for(int i=1;i<=n;i++)   std::cin>>a[i].val, a[i].id=i;     std::sort(a+1,a+1+n,cmp);     for(int i=1;i<=n;i++)     {         b[a[i].id]=i;     }     ll ans=0;     for(int i=1;i<=n;i++)     {         update(b[i],1,n);         ans+=i-query(b[i]);     }     std::cout<<ans; } int main(){     std::ios::sync_with_stdio(false),std::cin.tie(nullptr);     int tt=1;          while(tt--)solve();     return 0; }
   | 
 
#include<bits/stdc++.h> #define lowbit(x) (x&(-x)) using ll=long long; const int N=5e4+10; const int MAXN=5e5+10; const int INF=0x3f3f3f3f; const int mod=19650827; const int MAX=270007; typedef std::pair<int,int> pii; typedef std::pair<double,int> pdi; struct node {     ll val;     int id; }a[MAXN]; bool cmp(node a,node b) {     if(a.val!=b.val)    return a.val<b.val;     return a.id<b.id; } ll b[MAXN]; ll t[MAXN];
 
 
 
 
  void update(ll x,int k,int n) {     while(x<=n)     {         t[x]+=k;         x+=lowbit(x);     } } ll query(ll x) {     ll ans=0;     while(x)     {         ans+=t[x];         x-=lowbit(x);     }     return ans; } void build(int n) {     for(int i=1;i<=n;i++)   update(b[i],1,n);    } void solve() {     int n;  std::cin>>n;     for(int i=1;i<=n;i++)   std::cin>>a[i].val, a[i].id=i;     std::sort(a+1,a+1+n,cmp);     for(int i=1;i<=n;i++)     {         b[a[i].id]=i;     }     ll ans=0;     for(int i=1;i<=n;i++)     {         update(b[i],1,n);         ans+=i-query(b[i]);     }     std::cout<<ans; } int main(){     std::ios::sync_with_stdio(false),std::cin.tie(nullptr);     int tt=1;          while(tt--)solve();     return 0; }
   | 
 
#include<bits/stdc++.h> #define lowbit(x) (x&(-x)) using ll=long long; const int N=5e4+10; const int MAXN=1e5+10; const int INF=0x3f3f3f3f; const int mod=19650827; const int MAX=270007; typedef std::pair<int,int> pii; typedef std::pair<double,int> pdi; ll t[MAXN];
 
 
 
 
 
  struct node {     ll val;     int id;     static bool cmp(node a,node b)     {         if(a.val==b.val)  return a.id<b.id;         return a.val<b.val;     } }a[MAXN]; ll b[MAXN]; void update(ll x,ll k,int n) {     while(x<=n)     {         t[x]+=k;         x+=lowbit(x);     } } ll query(ll x,int n) {     ll ans=0;     while(x)     {         ans+=t[x];         x-=lowbit(x);     }     return ans; } void build(int n) {     for(int i=1;i<=n;i++)     {         update(b[i],1,n);     } } void solve() {     int n;  std::cin>>n;     for(int i=1;i<=n;i++)   std::cin>>a[i].val, a[i].id=i;     std::sort(a+1,a+1+n,node::cmp);     for(int i=1;i<=n;i++)     {         b[a[i].id]=i;     }     ll ans=0;     for(int i=1;i<=n;i++)     {         update(b[i],1,n);         ans=std::max(ans,i-query(b[i],n));     }     std::cout<<ans+1<<"\n"; } int main(){     std::ios::sync_with_stdio(false),std::cin.tie(nullptr);     int tt=1;          while(tt--)solve();     return 0; }
   |