跳到正文

算法·理论

树状数组与线段树(只有我看得懂版)

ny_123457约 19 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 19 分钟

版本

  1. v1当前

Part 1.树状数组精华模版

C++
int lowbit(int x){
	return x&-x;
}
void modify(int x,int y){
	for(int i=x;i<=maxxx;i+=lowbit(i))tree[i]+=y;
}
int getsum(int x){
	int sum=0;
	for(;x>0;x-=lowbit(x))sum+=tree[x];
	return sum;
}

模版想必就不用解释了。

Part 2.线段树精华模版

单点修改

C++
struct lilaoshi{
	int l,r,val;
}tree[4*maxn];
void pushup(int p){
	tree[p].val=max(tree[2*p].val,tree[2*p+1].val);
}
void build(int p,int x,int y){
	tree[p].l=x;
	tree[p].r=y;
	if(x==y){
		tree[p].val=a[x];
		return;
	}
	int mid=(x+y)/2;
	build(p<<1,x,mid);
	build(p<<1|1,mid+1,y);
	pushup(p);
}
void add(int p,int k,int d){
	if(tree[p].l==tree[p].r){
		tree[p].val+=d;
		return;
	}
	if(k<=tree[p<<1].r)add(p<<1,k,d);
	if(k>=tree[p<<1|1].l)add(p<<1|1,k,d);
	pushup(p);
}
int getval(int p,int k){
	if(tree[p].l==tree[p].r)return tree[p].val;
	if(k<=tree[p<<1].r)return getval(p<<1,k);
	if(k>=tree[p<<1|1].l)return getval(p<<1|1,k);
}
int getsum(int p,int s,int t){
	if(t<tree[p].l||s>tree[p].r)return 0;
	if(s<=tree[p].l&&tree[p].r<=t)return tree[p].val;
	return getsum(p<<1,s,t)+getsum(p<<1|1,s,t);
}
int find(int p,int l,int r){
	if(tree[p].l>=l&&tree[p].r<=r)return tree[p].val;
	int mid=(tree[p].r+tree[p].l)/2;
	if(r<=mid)return find(p*2,l,r);
	if(l>mid)return find(p*2+1,l,r);
	return max(find(p*2,l,mid),find(p*2+1,mid+1,r));
}

区间修改

C++
struct lilaoshi{
	int l,r,val,lazy;
}tree[4*maxn];
void pushup(int p){
	tree[p].val=max(tree[2*p].val,tree[2*p+1].val);
}
void build(int p,int x,int y){
	tree[p].l=x;
	tree[p].r=y;
	if(x==y){
		tree[p].val=a[x];
		return;
	}
	int mid=(x+y)/2;
	build(p<<1,x,mid);
	build(p<<1|1,mid+1,y);
	pushup(p);
}
void maketag(int p,int len,int x){
	tree[p].lazy+=x;
	tree[p].val+=x;
}
void pushdown(int p){
	int M=tree[p].r+tree[p].l>>1;
	maketag(p<<1,M-tree[p].l+1,tree[p].lazy);
	maketag(p<<1|1,tree[p].r-M,tree[p].lazy);
	tree[p].lazy=0;
}
void tag(int u){
	if(tree[u].lazy){
		int d=tree[u].lazy;
		tree[u].lazy=0;
		tree[u<<1].lazy+=d;
		tree[u<<1].val+=d;
		tree[u<<1|1].lazy+=d;
		tree[u<<1|1].val+=d;
	}
}
void add(int p,int x,int y,int d){
	//if(tree[p].l>y||tree[p].r<x)return;
	if(x<=tree[p].l && tree[p].r<=y){
		//int len=tree[p].r-tree[p].l+1;
		//maketag(p,len,d);
		tree[p].val+=d;
		tree[p].lazy+=d;
		return;
	}
	tag(p);
	int mid=tree[p].l+tree[p].r>>1;
	if(x<=mid) add(p<<1,x,y,d);
	if(y>mid) add(p<<1|1,x,y,d);
	pushup(p);
}
/*
int getval(int p,int k){
	if(tree[p].l==tree[p].r)return tree[p].val;
	if(k<=tree[p<<1].r)return getval(p<<1,k);
	if(k>=tree[p<<1|1].l)return getval(p<<1|1,k);
}
int getsum(int p,int s,int t){
	if(t<tree[p].l||s>tree[p].r)return 0;
	if(s<=tree[p].l&&tree[p].r<=t)return tree[p].val;
	return getsum(p<<1,s,t)+getsum(p<<1|1,s,t);
}*/
int find(int p,int l,int r){
	if(tree[p].l>=l && tree[p].r<=r){
		return tree[p].val;
	}
	int mid=tree[p].r+tree[p].l>>1;
	int v=-0x3f3f3f3f;
	pushdown(p);
	if(l<=mid) v=max(v,find(p<<1,l,r));
	if(r>mid) v=max(v,find(p<<1|1,l,r));
	return v;
}

Part 3.动态开点线段树精华模版

tottot 一定要初始化为 11!

C++
void add(int p,int l,int r,int x,int y,int d){
	if(x<=l&&r<=y){
		lazy[p]=max(lazy[p],d);
		return;
	}
	int mid=(l+r)>>1;
	if(x<=mid){
		if(ls[p]==0)ls[p]=++tot;
		add(ls[p],l,mid,x,y,d);
	}
	if(mid<y){
		if(rs[p]==0)rs[p]=++tot;
		add(rs[p],mid+1,r,x,y,d);
	}
}
void putdown(int p){
	if(ls[p])lazy[ls[p]]=max(lazy[ls[p]],lazy[p]);
	if(rs[p])lazy[rs[p]]=max(lazy[rs[p]],lazy[p]);
}
int getans(int p,int l,int r){
	putdown(p);
	if(l==r)return lazy[p];
	int sum=0;
	int mid=(l+r)>>1;
	if(ls[p])sum+=getans(ls[p],l,mid);
	else sum+=lazy[p]*(mid-l+1);
	if(rs[p])sum+=getans(rs[p],mid+1,r);
	else sum+=lazy[p]*(r-mid);
	return sum;
}

part 4.逆天恶心题

P题:IOI 代表队 难度:提高+/省选- 知识点:树状数组 正解:可以从 nn 到 11 枚举开头。不断更新靠前的位置。定义数组 treetree 表示种类数,当有一个新元素加入时,现将上一个位置上的种类数 −1-1,然后再让新加入的位置种类数 +1+1。(一开始的位置可以设置成 n+1n+1 )查询:先前位置的前一个元素的种类数即为答案。 代码:

C++
#include<bits/stdc++.h>
#define int long long
const int N=500010;
using namespace std;
int n,b[N],a[N],c[N],ans;
int lowbit(int x){
	return x&-x;
}
void modify(int x,int y){
	for(;x<=n;x+=lowbit(x))c[x]+=y;
}
int getsum(int x){
	int ans=0;
	for(;x;x-=lowbit(x))ans+=c[x];
	return ans;
}
signed main(){
	scanf("%lld",&n);
	for(int i=1;i<=n;i++){
		scanf("%lld",&b[i]);
		a[i]=n+1;
	}
	for(int i=n;i>=1;i--){
		ans+=getsum(a[b[i]]-1);
		modify(a[b[i]],-1);
		modify(i,1);
		a[b[i]]=i;
	}
	printf("%lld",ans);
	return 0;
}

Z题:打鼹鼠 难度未知,知识点《二维树状数组》。 二维模拟暴枚就行了,计算公式在代码里面。 代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1025;
int n,m,k,ans,tree[maxn][maxn],a,b,c,d;
void modify(int x,int y,int z){
	for(int i=x;i<=n;i+=i&-i)
		for(int j=y;j<=n;j+=j&-j)
			tree[i][j]+=z;
}
int getsum(int x,int y){
	int res=0;
	for(int i=x;i>0;i-=i&-i)
		for(int j=y;j>0;j-=j&-j)
			res+=tree[i][j];
	return res;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n;
	while(1){
		cin>>m;
		if(m==3)return 0;
		if(m==1){
			cin>>a>>b>>c;
			a++,b++;
			modify(a,b,c);
		}
		if(m==2){
			cin>>a>>b>>c>>d;
			a++,b++,c++,d++;
			cout<<getsum(c,d)-getsum(c,b-1)-getsum(a-1,d)+getsum(a-1,b-1)<<endl;
		}
	}
	return 0;
}

小c:火车线路 提高+/线段树 正解:每个预定是按原顺序定的,不能更改,既不能贪心(被坑过没过样例)。然后就是线段树,将一笔订单视为区间修改就是了。(到这笔订单的终点站时减回去) 代码:

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m,op,x,y,k,q;
int a[500005],tree[500005],flag[500005];
void pushup(int x){
	tree[x]=max(tree[x*2],tree[x*2+1]);
}
void pushdown(int u,int l,int r){
	int mid=l+r>>1;
	tree[u*2]+=flag[u];
	tree[u*2+1]+=flag[u];
	flag[u*2]+=flag[u];
	flag[u*2+1]+=flag[u];
	flag[u]=0;
}
void update(int u,int l,int r,int s,int t,int x){
	if(l<=s&&r>=t){
		flag[u]+=x;
		tree[u]+=x;
		return;
	}
	if(flag[u])pushdown(u,s,t);
	int mid=(s+t)>>1;
	if(l<=mid)update(u*2,l,r,s,mid,x);
	if(r>mid)update(u*2+1,l,r,mid+1,t,x);
	pushup(u);
}
int query(int u,int l,int r,int s,int t){
	if(l<=s && r>=t)return tree[u];
	if(flag[u])pushdown(u,s,t);
	int mid=s+t>>1,sum=0;
	if(l<=mid)sum=max(sum,query(u<<1,l,r,s,mid));
	if(r>mid)sum=max(sum,query(u<<1|1,l,r,mid+1,t));
	return sum;
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n>>m>>q;
	while(q--){
		int l,r,x;
		cin>>l>>r>>x;
		update(1,l,r-1,1,n,x);
		if(query(1,l,r-1,1,n)>m){
			update(1,l,r-1,1,n,-x);
			cout<<"N"<<endl;
		}
		else cout<<"T"<<endl;
	}
	return 0;
}

小k:站军姿 传奇提高+/动态开点线段树 正解:两个儿子分开记,用 cntcnt 数组记录每个儿子的值。数组开 n+mn+m 构建节点 min⁡(r,n)−min⁡(l−1,n)\min(r,n)-\min(l-1,n),其他就是模板了。 代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxm=4e6+5;
const int maxn=1e8+5;
int n,m,k,tot=1,cnt[maxm*2];
int ans[maxm],ls[maxm],rs[maxm];
void lson(int p,int l,int r){
	ls[p]=++tot;
	cnt[ls[p]]=min(r,n)-min(l-1,n);
}
void rson(int p,int l,int r){
	rs[p]=++tot;
	cnt[rs[p]]=min(r,n)-min(l-1,n);
}
void add(int p,int l,int r,int num,int k){
	cnt[p]+=k;
	if(l==r)return;
	int mid=(l+r)>>1;
	if(!ls[p])lson(p,l,mid);
	if(!rs[p])rson(p,mid+1,r);
	if(mid>=num)add(ls[p],l,mid,num,k);
	else add(rs[p],mid+1,r,num,k);
}
int ask(int p,int l,int r,int num){
	int mid=(l+r)>>1;
	if(l==r)return l;
	if(!ls[p])lson(p,l,mid);
	if(!rs[p])rson(p,mid+1,r);
	if(cnt[ls[p]]>=num)return ask(ls[p],l,mid,num);
	else return ask(rs[p],mid+1,r,num-cnt[ls[p]]);
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	cnt[1]=n;
	for(int i=1;i<=m;i++){
		cin>>k;
		int po=ask(1,1,n+m,k);
		add(1,1,n+m,po,-1);
		add(1,1,n+m,n+i,1);
		ans[i]=po;
		if(po>n)ans[i]=ans[po-n];
		cout<<ans[i]<<" ";
	}
	return 0;
}

小e:小白逛公园 提高+/动态开点/朴素线段树 单点修改没啥好说的,然后就是线段树求最大子段和,详见代码。 代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=5e5+5;
int n,m,op,l,r;
struct lilaoshi{
	int val,ans,lmax,rmax;
}tree[4*maxn];
int a[maxn];
void push_up(int x){
	tree[x].val=tree[x*2].val+tree[x*2+1].val;
	tree[x].ans=max(tree[x*2].ans,max(tree[x*2+1].ans,tree[x*2].rmax+tree[x*2+1].lmax));
	tree[x].lmax=max(tree[x*2].lmax,tree[x*2].val+tree[x*2+1].lmax);
	tree[x].rmax=max(tree[x*2+1].rmax,tree[x*2+1].val+tree[x*2].rmax);
}
void build(int l,int r,int u){
	if(l==r){
		tree[u].val=tree[u].lmax=tree[u].rmax=tree[u].ans=a[l];
		return;
	}
	int mid=(l+r)/2;
	build(l,mid,u*2);
	build(mid+1,r,u*2+1);
	push_up(u);
}
void update(int l,int s,int t,int u,int k){
	if(l==s&&s==t){
		tree[u].val=tree[u].lmax=tree[u].rmax=tree[u].ans=k;
		return;
	}
	int mid=(s+t)/2;
	if(l<=mid)update(l,s,mid,u*2,k);
	else update(l,mid+1,t,u*2+1,k);
	push_up(u);
}
lilaoshi query(int l,int r,int s,int t,int u){
	if(l<=s&&t<=r)return tree[u];
	int mid=(s+t)/2,ans=-2e9;
    lilaoshi x,y,sum;
	if(r<=mid)return query(l,r,s,mid,u*2);
	if(l>mid)return query(l,r,mid+1,t,u*2+1);
	x=query(l,r,s,mid,u*2),y=query(l,r,mid+1,t,u*2+1);
    sum.val=x.val+y.val;
	sum.ans=max(x.ans,max(y.ans,x.rmax+y.lmax));
	sum.lmax=max(x.lmax,x.val+y.lmax);
	sum.rmax=max(y.rmax,y.val+x.rmax);
    return sum;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>a[i];
	build(1,n,1);
	for(int i=1;i<=m;i++){
		cin>>op;
		if(op==1){
			cin>>l>>r;
			if(l>r)swap(l,r);
			cout<<query(l,r,1,n,1).ans<<endl;
		}
		else{
			cin>>l>>r;
			update(l,1,n,1,r);
		}
	}
	return 0;
}
原文