知识点✨树状数组✨
lowbit(x)
int lowbit(int x){
return (x&(-x));
}lowbit(x)是x在二进制下,末尾找到的第一个1和后面所有0的数
示例:
10=(1010)₂
lowbit(10)=(10)₂=2
14=(1110)₂
lowbit(14)=(10)₂=2
- c[x]表示的区间长度是lowbit(x)
- c[x]表示a[x-lowbit(x)+1]到a[x];
- 规律:进位碰巧和区间翻倍一样(进位也是倍增)
可实现两大类型
1. 区间查询
查询的是[1:x]的区间和
本质:拆二进制
代码模板
int lowbit(int x){
return (x&(-x));
}
int query(int x){
int ans=0;
while(x>0){
ans+=c[x];
x-=lowbit(x);
}
return ans;
}2.单点修改
a[x]+=v
本质是修改和a[x]相关的所有c[x]
第一个被影响的c[x]
下一个受到影响的是c[x+lowbid(x)]
代码模板
int lowbit(int x){
return (x&(-x));
}
void modify(int x,int v){
while(x<=n){
c[x]+=v;
x+=lowbit(x);
}
}结论
- 修改一个点的时间
- 查询一段区间的时间
- 空间复杂度
U438237 ١١(❛ᴗ❛)【树状数组+线段树+分块】-模板(单点修改+区间查询)
使用模板
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+50;
int n,q,c[N];
int lowbit(int x){
return (x&(-x));
}
int query(int x){
int ans=0;
while(x>0){
ans+=c[x];
x-=lowbit(x);
}
return ans;
}
void modify(int x,int v){
while(x<=n){
c[x]+=v;
x+=lowbit(x);
}
return ;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>q;
for(int i=1,x;i<=n;i++){
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;
}U438249 清点人数
细节:下车就是增加了人
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+50;
int n,q,c[N];
int lowbit(int x){
return (x&(-x));
}
int query(int x){
int ans=0;
while(x>0){
ans+=c[x];
x-=lowbit(x);
}
return ans;
}
void modify(int x,int v){
while(x<=n){
c[x]+=v;
x+=lowbit(x);
}
return ;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>q;
while(q--){
char op;
cin>>op;
if(op=='B'){
int x,y;
cin>>x>>y;
modify(x,y);
}else if(op=='A'){
int x;
cin>>x;
cout<<query(x)<<"\n";
}else{
int x,y;
cin>>x>>y;
modify(x,-y);
}
}
return 0;
}P3368 【模板】树状数组 2
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+50;
int n,q,a[N];
int diff[N];//差分数组
struct Bit_tree{//以防要定义多个树状数组
int c[N];
int lowbit(int x){
return (x&(-x));
}
int query(int x){
int ans=0;
while(x>0){
ans+=c[x];
x-=lowbit(x);
}
return ans;
}
void modify(int x,int v){
while(x<=n){
c[x]+=v;
x+=lowbit(x);
}
return ;
}
}tr1;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
diff[i]=a[i]-a[i-1];
tr1.modify(i,diff[i]);//修改的也是差分数组的值
}
while(q--){
int op;
cin>>op;
if(op==1){
int x,y,k;
cin>>x>>y>>k;
tr1.modify(x,k);
tr1.modify(y+1,-k);
}else{
int x;
cin>>x;
cout<<tr1.query(x)<<"\n";
}
}
return 0;
}P5057 [CQOI2006] 简单题
F1
奇数次为1
偶数次为0
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+50;
int n,q;
struct Bit_tree{//以防要定义多个树状数组
int c[N];
int lowbit(int x){
return (x&(-x));
}
int query(int x){
int ans=0;
while(x>0){
ans+=c[x];
x-=lowbit(x);
}
return ans;
}
void modify(int x,int v){
while(x<=n){
c[x]+=v;
x+=lowbit(x);
}
return ;
}
}tr1;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>q;
while(q--){
int op;
cin>>op;
if(op==1){
int x,y;
cin>>x>>y;
tr1.modify(x,1);
tr1.modify(y+1,-1);
}else{
int x;
cin>>x;
cout<<tr1.query(x)%2<<"\n";
}
}
return 0;
}F2
利用异或的特性,把 modify 和 query 中的加号用异或代替
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+50;
int n,q;
struct Bit_tree{//以防要定义多个树状数组
int c[N];
int lowbit(int x){
return (x&(-x));
}
int query(int x){
int ans=0;
while(x>0){
ans^=c[x];
x-=lowbit(x);
}
return ans;
}
void modify(int x,int v){
while(x<=n){
c[x]^=v;
x+=lowbit(x);
}
return ;
}
}tr1;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>q;
while(q--){
int op;
cin>>op;
if(op==1){
int x,y;
cin>>x>>y;
tr1.modify(x,1);
tr1.modify(y+1,1);
}else{
int x;
cin>>x;
cout<<tr1.query(x)<<"\n";
}
}
return 0;
}

