数据结构模板(basic)

本文深入探讨了数据结构与算法的关键概念,包括分块、权值线段树、树链剖分、堆、找树的直径、Tarjan求LCA、Splay基本操作和主席树等高级主题。通过实例和代码解析,帮助读者掌握复杂数据结构和高效算法的设计与应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、分块

void change(int l,int r,int delta){
    int i,L,R,l1=(l-1)/k+1,l2=(l-1)%k+1,r1=(r-1)/k+1,r2=(r-1)%k+1;
    L=l1,R=r1;
    if(L==R &&(l2!=1 || r2!=k)){
        fo(i,l2,r2)num[i+(L-1)*k]+=delta;
        return 0;
    }
    if(l2!=1){
    	fo(i,l2,k)num[i+(L-1)*k]+=delta;
		L++;
	}
	if(r2!=k){
		fo(i,1,r2)num[i+(R-1)*k]+=delta;
		R--;
	}
	fo(i,L,R)block[i]++;
}

2、权值线段树(查找第k小)查找部分

int findkth(int ps,int l,int r,int x){
	if(l==r)return l;
	int wz=(l+r)>>1;
	if(x>tr[ps<<1].sum)findkth((ps<<1)+1,wz+1,r,x-tr[ps<<1].sum);
	              else findkth(ps<<1,l,wz,x);
}

3、树链剖分(递归版)

void dg1(int x){
	siz[x]=1;
	for(int i=head[x];i;i=edge[i].next)
	    if(fa[x]!=edge[i].to){
	    	fa[edge[i].to]=x;
	    	dep[edge[i].to]=dep[x]+1;
	    	dg1(edge[i].to);
	    	siz[x]+=siz[edge[i].to];
	    	if(!hv[x]||siz[hv[x]]<siz[edge[i].to])hv[x]=edge[i].to;
		}
}

第一个递归:求 s i z [ x ] siz[x] siz[x]和深度,顺便找重儿子

void dg2(int x){
	dfn[x]=++T;tar[T]=x;
	if(hv[x]){
		top[hv[x]]=top[x];
		dg2(hv[x]);
	}
	for(int i=head[x];i;i=edge[i].next)
	    if(fa[x]!=edge[i].to&&hv[x]!=edge[i].to)dg2(edge[i].to);
}

第二个递归:找到每个点的重链链顶,以及基于重链的 d f n dfn dfn序列:
①有重儿子先递归重儿子。②其次递归轻儿子。

void oper(int x,int y){
	while(1){
		int tx=top[x],ty=top[y];
		if(dep[tx]<dep[ty]){
			swap(x,y);
			swap(tx,ty);
		}
		if(tx==ty)break;
		modify(dfn[tx],dfn[x]);
		x=fa[tx];
	}
	if(dfn[x]>dfn[y])swap(x,y);
	modify(dfn[x],dfn[y]);
}

区间修改/查询:先跳深度大的链顶的那条链,既能保证正确性,又能保证时间复杂度。

4、堆(手打的)

a数组记录着堆里要维护的值。
b[i]表示原数组中第i个数在堆中的位置。
c[i]表示堆中第i个元素在原数组中的位置。

void heap_swap(int x,int y){
	swap(a[x],a[y]);
	swap(b[c[x]],b[c[y]]);
	swap(c[x],c[y]); 
}
void heap_up(int ps){
	while(1){
		int fa=ps/2;
		if(a[ps]<a[fa])heap_swap(fa,ps);else break;
		ps=fa;
	}
}
void heap_down(int x){
	while(1){
		int mx,l=ps<<1,r=(ps<<1)+1;
		if(l>tot)break;
		mx=l;
		if(r<=tot && a[r]<a[mx])mx=r;
		if(a[ps]<a[mx])heap_swap(mx,ps);else break;
		ps=mx;
	}
}
void heap_push(int x,int y){
	a[++tot]=x;
	c[tot]=y;
	b[y]=tot;
	heap_up(x);
}
void heap_pop(){
	heap_swap(1,tot);
	heap_down(1);
}
void heap_del(int x){
	heap_swap(x,tot);
	heap_down(x);
	heap_up(x);
}

5、找树的直径

一个显然的结论,假设这条直径为 s → t s→t st ,那么对于任意一点u,从u搜到的最远点一定是 s s s或者 t t t中的一个。

void findD(int x,int y){
	for(int i=head[x];i;i=edge[i].next)
	    if(fa[x]!=edge[i].to){
	    	fa[edge[i].to]=0;
	    	dis[edge[i].to]=y+edge[i].val;
	    	if(dis[edge[i].to]>mx){
	    		mx=dis[edge[i].to];
	    		wz=edge[i].to;
			}
			findD(edge[i].to,mx);
		}
}
int main(){
	mx=0;
	findD(1,0);
	memset(fa,0,sizeof(fa));
	root=wz;
	findD(wz,0);
	//此时root和新的wz形成一条直径,是一条链,可以通过跳fa[]输出这条直径。 
}

6、tarjan求lca

离线做,建两个图,一个代表树,另一个代表需要查询lca的两节点。
做法:对于没有递归完的点x,设 f a [ x ] = x fa[x]=x fa[x]=x,如果x递归完了,那么 f a [ x ] = x 的 父 亲 fa[x]=x的父亲 fa[x]=x。然后对于询问(x,y),如果y被遍历过,那么 l c a = f i n d ( y ) lca=find(y) lca=find(y),即y的并查集祖先(y的没有递归完的深度最小的祖先)。
正确性:对于 ( x , y ) (x,y) (x,y)如果y已经递归完了,那么 f a [ y ] fa[y] fa[y]指回了上面,此时 l c a ( x , y ) lca(x,y) lca(x,y)是没有递归完的,所以用并查集找到的祖先一定是 l c a ( x , y ) lca(x,y) lca(x,y)

int find(int x){
	return fa[x]==x?x:fa[x]=get(fa[x]);
}
void tarjan(int x){
	fa[x]=x;
	vis[x]=1;
	for(int i=head[x];i;i=edge[i].next)
	    if(!vis[edge[i].to]){
	     	tarjan(edge[i].to);
	     	fa[edge[i].to]=x;
		}
	for(int i=qhead[x];i;i=qedge[i].next)
	    if(!vis[qedge[i].to]){
	    	qedge[i].lca=find(qedge[i].to);
	    	qedge[i^1].lca=qedge[i].lca;
		}
}
int main(){
	scanf("%d%d",&n,&q);
	tot=1;
	fo(i,1,n){
		scanf("%d%d",&x,&y);
		lb(x,y);lb(y,x);//连边过程自行脑补
	}
	fo(i,1,q){
		scanf("%d%d",&x,&y);
		lbq(x,y);lbq(y,x);//连边过程自行脑补
	}
	fo(i,1,q)printf("%d\n",qedge[i*2].lca);
}

7.Splay基本操作

尤其是选择,看清楚具体的节点的个数。以及初始的splay该怎么建,所有该初始化的变量都不要漏掉。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define N 100010
#define Null 100005
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
using namespace std;
struct note{
	int siz,a,f,sum;
	int c[2];
};note tr[N];
int a[N];
int i,j,k,l,r,n,m,ans;
int root;
int dir(int x){
	return tr[tr[x].f].c[1]==x;
}
void update(int x){
	tr[x].siz=tr[tr[x].c[0]].siz+tr[tr[x].c[1]].siz+1;
	tr[x].sum=tr[tr[x].c[0]].sum+tr[tr[x].c[1]].sum+tr[x].a;
}
void sc(int x,int y,int z){
	tr[x].c[z]=y;
	if(y!=Null)tr[y].f=x;
}
void zig(int x){
	int y=tr[x].f,z=dir(x);
	sc(y,tr[x].c[z^1],z);
	if(y==root)tr[x].f=Null,root=x;
	else sc(tr[y].f,x,dir(y));
	sc(x,y,z^1);
	update(y);update(x);
}
void splay(int x,int rt){
	while(tr[x].f!=rt){
		int y=tr[x].f;
		if(tr[y].f!=rt)
		if(dir(x)==dir(y))zig(y);else zig(x);
		zig(x);
	}
	update(x);
	if(rt==Null)root=x;
}
void select(int k,int rt){
	int x=root;
	while(tr[tr[x].c[0]].siz!=k){
		if(tr[tr[x].c[0]].siz>k)x=tr[x].c[0];
		else{
			k-=tr[tr[x].c[0]].siz+1;
			x=tr[x].c[1];
		}
	}
	splay(x,rt);
}
int main(){
	scanf("%d",&n);
	fo(i,1,n)scanf("%d",&a[i]);
	fo(i,0,N-1)tr[i].c[0]=tr[i].c[1]=tr[i].f=Null;
	fo(i,1,n+2){
		if(i<n+2)tr[i].c[1]=i+1;
		tr[i].siz=n+3-i;
		if(i>1)tr[i].f=i-1;
	}
	fd(i,n+1,1){
		tr[i].sum=tr[i+1].sum+a[i-1];
		tr[i].a=a[i-1];
	}
	root=1;
	splay(n+2,Null);
	scanf("%d",&m);
	while(m--){
		scanf("%d%d",&l,&r);
		select(l-1,Null);
		select(r+1,root);
		ans=tr[tr[tr[root].c[1]].c[0]].sum;
		printf("%d\n",ans);
	}
	return 0;
}

8.主席树

用来维护一个区间的信息。其功能主要是查询一个区间的一个范围的数的个数。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define N 100010
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
struct note{
	int sum,ls,rs;
};note tr[N*20];
int a[N],o[N];
int i,j,k,l,r,n,m,ans;
int temp;
int cnt,gs;
int rt[N];
int query(int px,int py,int l,int r,int x){
	if(l==r)return o[l];
	int wz=(l+r)>>1;
	temp=tr[tr[px].ls].sum-tr[tr[py].ls].sum;
	if(temp>=x)return query(tr[px].ls,tr[py].ls,l,wz,x);
	      else return query(tr[px].rs,tr[py].rs,wz+1,r,x-temp);
}
void change(int &px,int py,int l,int r,int x){
	px=++gs;
	tr[px]=tr[py];
	tr[px].sum++;
	if(l==r)return;
	int wz=(l+r)>>1;
	if(x<=wz)change(tr[px].ls,tr[py].ls,l,wz,x);
	    else change(tr[px].rs,tr[py].rs,wz+1,r,x);
}
int main(){
	freopen("zxs.in","r",stdin);
	scanf("%d%d",&n,&m);
	fo(i,1,n)scanf("%d",&a[i]),o[i]=a[i];
	sort(o+1,o+n+1);
	cnt=unique(o+1,o+n+1)-o-1;
	fo(i,1,n)a[i]=lower_bound(o+1,o+cnt+1,a[i])-o;
	fo(i,1,n)
	    change(rt[i],rt[i-1],1,n,a[i]);
	while(m--){
		scanf("%d%d%d",&l,&r,&k);
		ans=query(rt[r],rt[l-1],1,n,k);
		printf("%d\n",ans);
	} 
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值