跳到正文

个人记录

树状数组

htjiajia约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年10月25日
收录
2026年10月25日
更新
2026年11月14日
篇幅
约 6 分钟

版本

  1. v1当前

lowbit

一个二进制数最低位的 11 右边所有的 00 ,lowbit(x)=x&−x\text{lowbit}(x)=x\&-x,因为负数是补码,xx 和 −x-x 只有最右侧的 11 位置同为 11,按位与就取出这个最低位 11,即 lowbit(x)=x&−x\text{lowbit}(x)=x\&-x。

树状数组的定义

树状数组是利用 lowbitlowbit 划分区间、高效支持单点修改、区间前缀求和的数组结构,时间复杂度均为 O(log⁡n)O(\log n)。

P3347(模板题)

C++
#include <bits/stdc++.h>
using namespace std;
int n,m,a[500005],c[500005];
int lowbit(int x){
    return x&-x;
}
//单点前缀查询,求[1,x]的区间和
int query(int x){
    int res=0;
    while(x>0){
        res+=c[x];
        x-=lowbit(x);
    }
    return res;
}
//单点更新,下表x加上val
void update(int x,int val){
    while(x<=n){
        c[x]+=val;
        x+=lowbit(x);
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        update(i,a[i]);//初始化树状数组
    }
    for(int i=1;i<=m;i++){
        int op,x,y;
        cin>>op>>x>>y;
        if(op==1){
            update(x,y);
        }else{
            cout<<query(y)-query(x-1)<<'\n';
        }
    }
    return 0;
}

P3368(模板题)

变成了区间修改。

C++
#include <bits/stdc++.h>
using namespace std;
int n,m,a[500005],c[500005];
int lowbit(int x){
    return x&-x;
}
//维护差分数组
int query(int x){
    int res=0;
    while(x>0){
        res+=c[x];
        x-=lowbit(x);
    }
    return res;
}
//单点修改
void update(int x,int val){
    while(x<=n){
        c[x]+=val;
        x+=lowbit(x);
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        update(i,a[i]-a[i-1]);
    }
    for(int i=1;i<=m;i++){
        int op,x,y,k;
        cin>>op;
        if(op==1){
            cin>>x>>y>>k;
            //区间[x,y]加 k
            update(x,k);
            update(y+1,-k);
        }else{
            cin>>x;
            //查询a[x]
            cout<<query(x)<<'\n';
        }
    }
    return 0;
}

离散化的定义

离散化是将大的值域区间映射到小的值域区间的技巧。

B3694(模板题)

C++
#include <bits/stdc++.h>
using namespace std;
int t,n,a[100005],b[100005];
int main(){
    cin>>t;
    while(t--){
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            b[i]=a[i];
        }
        sort(a+1,a+n+1);
        int len=unique(a+1,a+n+1)-(a+1);//去重
        for(int i=1;i<=n;i++){
            //在去重数组里二分找b[i]的下标,得到排名
            b[i]=lower_bound(a+1,a+len+1,b[i])-a;
        }
        for(int i=1;i<=n;i++){
            cout<<b[i]<<' ';
        }
        cout<<'\n';
    }
    return 0;
}

P5149

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
map<string,int>mp;
int n,a[100005],c[100005],ans;
string s;
int lowbit(int x){
    return x&-x;
}
int query(int x){
	int res=0;
	while(x>0){
        res+=c[x];
        x-=lowbit(x);
    }
	return res;
}
void update(int x){
    while(x<=n){
        c[x]++;
        x+=lowbit(x);
    }
}
signed main(){
	cin>>n;
  //分配原来位置i
	for(int i=1;i<=n;i++){
		cin>>s;
        mp[s]=i;
	}
  //转换成原位置编号数组a
	for(int i=1;i<=n;i++){
		cin>>s;
        a[i]=mp[s];
	}
  //从后往前扫,query(a[i]-1) 表示已经放进树状数组里,编号比a[i]小的元素个数
	for(int i=n;i>=1;i--){
		ans+=query(a[i]-1);
		update(a[i]);
	}
	cout<<ans;
    return 0;
}
原文