跳到正文

个人记录

模板

jun头吉吉约 41 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月18日
篇幅
约 41 分钟
获赞
1

版本

  1. v1当前

IO

快读

C++
#include<bits/stdc++.h>
#define faster
namespace in{
	#ifdef faster
	char buf[1<<21],*p1=buf,*p2=buf;
	inline int getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
	#else
	inline int getc(){return getchar();}
	#endif
	template <typename T>inline void read(T& t){
		t=0;int f=0;char ch=getc();while (!isdigit(ch)){if(ch=='-')f = 1;ch=getc();}
	    while(isdigit(ch)){t=t*10+ch-48;ch = getc();}if(f)t=-t;
	}
	template <typename T,typename... Args> inline void read(T& t, Args&... args){read(t);read(args...);}
}
namespace out{
	char buffer[1<<21];int p1=-1;const int p2 = (1<<21)-1;
	inline void flush(){fwrite(buffer,1,p1+1,stdout),p1=-1;}
	inline void putc(const char &x) {if(p1==p2)flush();buffer[++p1]=x;}
	template <typename T>void write(T x) {
		static char buf[15];static int len=-1;if(x>=0){do{buf[++len]=x%10+48,x/=10;}while (x);}else{putc('-');do {buf[++len]=-(x%10)+48,x/=10;}while(x);}
		while (len>=0)putc(buf[len]),--len;
	}
}
using namespace std;

signed main(){
	out::flush();
	return 0;
}

图论

SPFA

C++
const int maxn=10010;
const int maxm=500010;
struct spfa{
	int s,t,c;bool vis[maxn];
	int head[maxn],path[maxn];
	struct node{
	    int nxt,to,w;
	}a[maxm<<1];
	inline int relax(int u,int v,int w){
	    if(path[v]>path[u]+w){
	        path[v]=path[u]+w;
	        return 1;
	    }return 0;
	}
	inline void add(int u,int v,int w){a[c].to=v;a[c].w=w;a[c].nxt=head[u];head[u]=c++;}
	inline void init(int _s,int _t){s=_s,t=_t;memset(head,0,sizeof head);c=1;}
	void deal(){
	    int u,v,num=0;long long x=0;list<int> q;
	    memset(path,0x3f,sizeof path);
	    memset(vis,false,sizeof vis);
	    path[s]=0;vis[s]=true;q.push_back(s);num++;
	    while(!q.empty()){
	        u=q.front();q.pop_front();
	        num--;x-=path[u];
	        while(num&&path[u]>x/num){q.push_back(u);u=q.front();q.pop_front();}
	        vis[u]=false;
	        for(int i=head[u];i;i=a[i].nxt){
	            v=a[i].to;
	            if(relax(u,v,a[i].w)&&!vis[v]){
	                if(!q.empty()&&path[v]<path[q.front()])q.push_front(v);
	                else q.push_back(v);num++;x+=path[v];vis[v]=true;
	            }
	        }
	    }
	}
};

Dinic

C++
const int maxn=4010,maxe=100010*2;
struct Graph{
	struct node{
		int v,w,nxt;
	}e[maxe<<1];
	int head[maxn],cur[maxn],tot;
	int dis[maxn];
	int s,t;
	void init(int _s,int _t){s=_s,t=_t;tot=1;memset(head,0,sizeof head);}
	Graph(int _s=0,int _t=0){init(_s,_t);}
	void add(int u,int v,int w){
		//printf("%d %d %d\n",u,v,w);
		e[++tot]=(node){v,w,head[u]},head[u]=tot;
		e[++tot]=(node){u,0,head[v]},head[v]=tot;
	}
	#define v e[i].v
	inline bool bfs(){
		queue<int>q;
		memset(dis,0,sizeof dis);
		memcpy(cur,head,sizeof head);
		dis[s]=1;q.push(s);
		while(q.size()){
			int u=q.front();q.pop();
			for(int i=head[u];i;i=e[i].nxt)
				if(!dis[v]&&e[i].w){
					dis[v]=dis[u]+1,q.push(v);
					if(v==t)return true;
				}
		}
		return  false;
	}
	int dfs(int u,int flow){
		if(u==t)return flow;
		int rest=flow;
		for(int i=cur[u];i&&rest;i=e[i].nxt){
			if(dis[v]==dis[u]+1&&e[i].w){
				int tmp=dfs(v,min(rest,e[i].w));
				rest-=tmp,e[i].w-=tmp,e[i^1].w+=tmp;
			}
			cur[u]=i;
		}
		if(rest==0)dis[u]=-1;
		return flow-rest;
	}
	#undef v
	int dinic(){
		int ans=0;
		while(bfs())
			while(int sth=dfs(s,2e9))
				ans+=sth;
		return ans;
	}
}G;

MincostMaxflow

C++
const int maxn=100005;
const int maxe=100005;
struct Graph{
	int s,t;
	int dis[maxn],pre[maxn],lste[maxn],flow[maxn];
	bool inq[maxn];
	struct node{
		int v,w,cost,nxt;
		node(int _v=0,int _w=0,int _c=0,int _n=0){v=_v,w=_w,cost=_c,nxt=_n;}
	}e[maxe];
	int tot,head[maxn];
	void init(int _s,int _t){
		memset(head,0,sizeof head);
		tot=1;s=_s,t=_t;
	}
	inline void add(int u,int v,int w,int c){
		e[++tot]=node(v,w,c,head[u]);head[u]=tot;
		e[++tot]=node(u,0,-c,head[v]);head[v]=tot;
	}
	bool spfa(){
		queue<int>q;
		memset(dis,63,sizeof dis);
		memset(flow,63,sizeof flow);
		memset(inq,0,sizeof inq);
		dis[s]=0,pre[t]=-1;q.push(s);
		while(!q.empty()){
			int u=q.front();q.pop();inq[u]=0;
			#define v e[i].v
			for(int i=head[u];i;i=e[i].nxt){
				if(e[i].w>0&&dis[v]>dis[u]+e[i].cost){
					dis[v]=dis[u]+e[i].cost;
					pre[v]=u,lste[v]=i;//lste 表示顶点v是由哪条边过来的
					flow[v]=min(flow[u],e[i].w);
					if(!inq[v])inq[v]=1,q.push(v);
				}
			}
			#undef v
		}
		return pre[t]!=-1;
	}
	pair<int,int> mcmf(){
		int mincost=0,maxflow=0;
		while(spfa()){
			int u=t;
			maxflow+=flow[t],mincost+=flow[t]*dis[t];
			while(u!=s){
				int E=lste[u];
				e[E].w-=flow[t],e[E^1].w+=flow[t];
				u=pre[u];
			}
		}
		return make_pair(mincost,maxflow);
	}
}G;

数学

自适应辛普森法

C++
struct Simpson{
	double (*f)(double);
	double Eps;
	Simpson(double (*_f)(double),double _E){f=_f;Eps=_E;}
	inline double simpson(double l,double r){
		double mid=(l+r)/2;
		return (f(l)+4*f(mid)+f(r))*(r-l)/6;
	}
	double asr(double l,double r,double eps,double ans){
		double mid=(l+r)/2;
		double l_=simpson(l,mid),r_=simpson(mid,r);
		if(fabs(l_+r_-ans)<=15*eps)return l_+r_+(l_+r_-ans)/15;
		return asr(l,mid,eps/2,l_)+asr(mid,r,eps/2,r_);
	}
	double asr(double l,double r){
		return asr(l,r,Eps,simpson(l,r));
	}
};

凸包

C++
namespace Convex_Hull{
	#define sq(x) (x)*(x)
	struct node{
		double x,y;
	}p[1000000+1000],s[1000000+1000];
	int n=0,cnt;
	double check(node a1,node a2,node b1,node b2){
		return (a2.x-a1.x)*(b2.y-b1.y)-(a2.y-a1.y)*(b2.x-b1.x);
	}double d(node a,node b){
		return sqrt(sq(a.x-b.x)+sq(a.y-b.y));
	}bool cmp(node p1,node p2){
		double tmp=check(p1,p[1],p2,p[1]);
		if(tmp>0)return 1;
		else if(tmp==0&&d(p[0],p1)<d(p[0],p2))return 1;
		return 0;
	}
	void add(double x,double y){
		n++;
		p[n].x=x,p[n].y=y;
		if(p[n].y<p[1].y)swap(p[n],p[1]);
	}
	void deal(){
		sort(p+2,p+1+n,cmp);
		s[1]=p[1];cnt=1;
		for(int i=2;i<=n;i++){
			while(cnt>1&&check(s[cnt-1],s[cnt],s[cnt],p[i])<=0)
				cnt--;
			s[++cnt]=p[i];
		}s[cnt+1]=p[1];
	}
	double C(){
		double ans=0;
		for(int i=1;i<=cnt;i++)
			ans+=d(s[i],s[i+1]);
		return ans;
	}
};

高斯消元

C++
template<class T,const int M>
struct Gauss{
	//解n元方程组
	int n;
	T B[M][M+1];
	T ans[M];
	bool run(){
		for(int i=0;i<n;i++){
			int pivot=i;
			for(int j=i;j<n;j++)
				if(B[j][i]==B[pivot][i])
					pivot=j;
			for(int j=0;j<=n;j++)swap(B[i][j],B[pivot][j]);
			if(B[i][i]==0)return false;
			for(int j=i+1;j<=n;j++)B[i][j]/=B[i][i];
			for(int j=0;j<n;j++)
				if(i!=j)
					for(int k=i+1;k<=n;k++)
						B[j][k]-=B[j][i]*B[i][k];
		}
		for(int i=0;i<n;i++)ans[i]=B[i][n];
		return true;
	}
};
const double EPS=1e-8;
struct node{
	double x;
	node(double _=0){x=_;}
	bool operator==(node b){return fabs(x-b.x)<=EPS;}
	bool operator==(int b){return fabs(x-b)<=EPS;}
	node operator*(node b){return node(x*b.x);}
	node *operator/=(node b){x/=b.x;return this;}
	node *operator-=(node b){x-=b.x;return this;}
};
Gauss<node,120>G;

数论

快速幂

C++
template<class T>T ksm(T a,T b,T c){
	T res=1;
	while(b){
		if(b&1)res=res*a%c;
		a=a*a%c;b>>=1;
	}return res;
}

逆元

C++
template<class T>void exgcd(T a,T b,T &x,T &y){
	if(!b)x=1,y=0;
	else{
		exgcd(b,a%b,x,y);
		T t=x;x=y;
		y=t-a/b*y;
	}
}
template<class T>T inv(T a,T b){
	T x,y;exgcd(a,b,x,y);
	return (x%b+b)%b;
}

BSGS

C++
template<class T>T bsgs(T a,T b,T p){
	unordered_map<T,T>hash;
	b%=p;T t=sqrt(p)+1;
	for(T i=0;i<t;i++)
		hash[b*ksm(a,i,p)%p]=i;
	a=ksm(a,t,p);
	if(!a)return b=0?1:-1;
	for(T i=1;i<=t;i++){
		T val=ksm(a,i,p);
		int j=hash.find(val)==hash.end()?-1:hash[val];
		if(j>=0&&i*t-j>=0)return i*t-j;
	}return -1;
}

exBSGS

C++
template<class T>T ex_bsgs(T a,T b,T p){
	if(b==1||p==1)return -1;
	T na=1,k=0;
	while(T g=__gcd(a,p)){
		if(g==1)break;
		if(b%g)return -1;
		k++;b/=g;p/=g;na=na*(a/g)%p;
		if(na==b)return k;
	}
	long long f=bsgs(a,b*inv(na,p)%p,p);
	if(f==-1)return -1;
	return f+k;
}

二次剩余

C++
namespace residue{
	//求二次剩余
	int mod,I2;
	struct complex{
		long long real,imag;
		complex&operator=(long long x){real=x,imag=0;}
		complex(long long real=0,long long imag=0):real(real),imag(imag){}
		bool operator==(const complex&b)const{return real==b.real&&imag==b.imag;}
		complex operator*(const complex &b)const{return complex((real*b.real+I2*imag%mod*b.imag)%mod,(real*b.imag+imag*b.real)%mod);}
	};
	complex ksm(complex a,int b){
		complex res=1;
		while(b){
			if(b&1)res=res*a;
			a=a*a;b>>=1;
		}return res;
	}
	void init(int p){mod=p;}
	bool check(int x){return ksm(complex(x,0),(mod-1)/2).real==1;}
	pair<int,int>solve(int n){
	    if(n==0)return {0,0};
		long long a=rand()%mod;
		while(!a||check((a*a+mod-n)%mod))a=rand()%mod;
		I2=(a*a+mod-n)%mod;
		int x0=ksm(complex(a,1),(mod+1)/2).real;
		return {min(x0,mod-x0),max(x0,mod-x0)};
	}
}

数据结构

并查集

C++
const int maxn=1e4+10;
struct DSU{
	int fa[maxn];
	DSU(){for(int i=1;i<maxn;i++)fa[i]=i;}
	int getfa(int x){return x==fa[x]?x:fa[x]=getfa(fa[x]);}
	void merge(int x,int y){fa[getfa(y)]=getfa(x);}
};

ST表

C++
const int maxn=2e5+100;
const int maxj=30;
long long max(long long a,long long b){return a>b?a:b;}
long long min(long long a,long long b){return a>b?b:a;}
struct RMQ{
    long long dp[maxn][maxj];
    void init(long long *arr,long long n,long long (*cmp)(long long,long long)){
        for(long long i=1;i<=n;i++)
            dp[i][0]=arr[i];
        for(long long j=1;(1<<j)<=n;j++)
            for(long long i=1;i+(1<<j)-1<=n;i++)
                dp[i][j]=cmp(dp[i][j-1],dp[i+(1<<j-1)][j-1]);
    }
    long long query(long long st,long long ed,long long (*cmp)(long long,long long)){
        long long j=log(ed-st+1)/log(2);
        return cmp(dp[st][j],dp[ed-(1<<j)+1][j]);
    }
}R;

可持久化线段树

C++
struct node{
	int l,r,lc,rc;int len,llen,rlen,mlen;
	template<class T>node(T val){
		if(val==1)llen=rlen=mlen=1;
		else llen=rlen=mlen=0;
		len=1;
	}
	node(){llen=rlen=mlen=len=0;}
	node operator+(const node b)const{
		node c;c.len=len+b.len;
		if(r-l+1==mlen)c.llen=mlen+b.llen;
		else c.llen=llen;
		if(b.r-b.l+1==b.mlen)c.rlen=b.mlen+rlen;
		else c.rlen=b.rlen;
		c.mlen=max(rlen+b.llen,max(mlen,b.mlen));
		return c;
	}
	void cpy(node from){
		llen=from.llen;
		rlen=from.rlen;
		mlen=from.mlen;
		len=from.len;
	}
};
template<class A,class B>
struct HJT_Tree{
	A t[maxn*20];B a[maxn];
	B *operator[](const int x){return &a[x];}
	int cnt;HJT_Tree(){cnt=0;}
	int build(int l,int r){
		int x=++cnt;
		t[x].l=l,t[x].r=r;
		if(l==r){
			t[x].cpy(A(a[l]));
			return x;
		}
		int mid=l+r>>1;
		t[x].lc=build(l,mid);
		t[x].rc=build(mid+1,r);
		t[x].cpy(t[t[x].lc]+t[t[x].rc]);
		return x;
	}
	int upd(int lst,int pos,B val){
		int x=++cnt;t[x]=t[lst];
		if(t[x].l==t[x].r){t[x].cpy(A(val));return x;}
		int mid=t[x].l+t[x].r>>1;
		if(pos<=mid)t[x].lc=upd(t[lst].lc,pos,val);
		else t[x].rc=upd(t[lst].rc,pos,val);
		t[x].cpy(t[t[x].lc]+t[t[x].rc]);
		return x;
	}
	A qry(int x,int lq,int rq){
		if(lq<=t[x].l&&t[x].r<=rq)return t[x];
		int mid=t[x].l+t[x].r>>1;
		if(rq<=mid)return qry(t[x].lc,lq,rq);
		if(lq>mid)return qry(t[x].rc,lq,rq);
		return qry(t[x].lc,lq,rq)+qry(t[x].rc,lq,rq);
	}
};
HJT_Tree<node,int>Tree;

Splay

C++
#define clear(x) {f[x]=key[x]=size[x]=recy[x]=son[x][0]=son[x][1]=0;}
#define get(x) (son[f[x]][1]==x)
#define connect(x,y,p) {if(x)f[x]=y;if(y)son[y][p]=x;}
#define update(x) {size[x]=recy[x];size[x]+=size[son[x][0]]+size[son[x][1]];}
template<const int N>
struct Splay{
	int num,root,f[N],key[N],size[N],recy[N],son[N][2];
	void rotate(int x){
		int fa=f[x],ffa=f[fa],p1=get(x),p2=get(fa);
		connect(son[x][p1^1],fa,p1);
		connect(fa,x,p1^1);
		connect(x,ffa,p2);
		update(fa);update(x);
	}
	void splay(int x){
		for(int fa;fa=f[x];rotate(x))
		if(f[fa])rotate(get(x)==get(fa)?fa:x);
		root=x;
	}
	void insert(int x){
		if(!root){
			root=++num;
			key[root]=x;size[root]=recy[root]=1;
			son[root][0]=son[root][1]=0;
			return;
		}
		int now=root,fa=0;
		while(1){
			if(key[now]==x){
				++recy[now];
				update(now);update(fa);
				splay(now);return;
			}
			fa=now,now=son[now][x>key[now]];
			if(!now){
				key[++num]=x;
				size[num]=recy[num]=1;
				f[num]=fa;son[fa][x>key[fa]]=num;
				update(fa);splay(num);
				return;
			}
		}
	}
	int find(int x){
		int now=root,ans=0;
		while(1){
			if(x<key[now]){
				now=son[now][0];
				continue;
			}
			ans+=size[son[now][0]];
			if(x==key[now]){splay(now);return ans+1;}
			ans+=recy[now];now=son[now][1];
		}
	}
	inline int kth(int x){
		int now=root;
		while(1){
		if(son[now][0]&&x<=size[son[now][0]]){
			now=son[now][0];
			continue;
		}
		if(son[now][0])x-=size[son[now][0]];
		if(x<=recy[now])return key[now];
		x-=recy[now];now=son[now][1];
		}
	}
	int pre(){
		int now=son[root][0];
		while(son[now][1])now=son[now][1];
		return now;
	}
	int nxt(){
		int now=son[root][1];
		while(son[now][0])now=son[now][0];
		return now;
	}
	void del(int x){
		find(x);
		if(recy[root]>1){
			recy[root]--;
			update(root);
			return;
		}
		if(!son[root][0]&&!son[root][1]){
			clear(root);root=0;
			return;
		}
		int tmp=root;
		if(!son[root][0]){
			f[root=son[root][1]]=0;
			clear(tmp);return;
		}
		if(!son[root][1]){
			f[root=son[root][0]]=0;
			clear(tmp);return;
		}
		int left=pre();
		splay(left);connect(son[tmp][1],root,1);
		clear(tmp);update(root);
	}
};

Splay维护序列

C++
template<const int N,class T>
struct Splay{
    int root,cnt;
    int f[N],ch[N][2];T v[N];
    int sum[N],sz[N],mx[N],lx[N],rx[N];
    bool tag[N],rev[N];
    queue<int>q;
    #define max3(a,b,c) max(a,max(b,c))
    #define inf 0x3f3f3f3f
    void pushup(int x){
        int l=ch[x][0],r=ch[x][1];
        sz[x]=sz[l]+1+sz[r];
        sum[x]=sum[l]+v[x]+sum[r];
        mx[x]=max3(mx[l],mx[r],rx[l]+v[x]+lx[r]);
        lx[x]=max(lx[l],sum[l]+v[x]+lx[r]);
        rx[x]=max(rx[r],sum[r]+v[x]+rx[l]);
    }
    void pushdown(int x){
        int l=ch[x][0],r=ch[x][1];
        if(tag[x]){//被修改成相同值
            rev[x]=tag[x]=0;
            if(l)tag[l]=1,v[l]=v[x],sum[l]=sz[l]*v[x];
            if(r)tag[r]=1,v[r]=v[x],sum[r]=sz[r]*v[x];
            if(v[x]>0){
                if(l)lx[l]=rx[l]=mx[l]=sum[l];
                if(r)lx[r]=rx[r]=mx[r]=sum[r];
            }else{
                if(l)lx[l]=rx[l]=0,mx[l]=v[x];
                if(r)lx[r]=rx[r]=0,mx[r]=v[x];
            }
        }
        if(rev[x]){//要翻转
            rev[x]=0;
            rev[l]^=1,rev[r]^=1;
            swap(lx[l],rx[l]);
            swap(lx[r],rx[r]);
            swap(ch[l][0],ch[l][1]);
            swap(ch[r][0],ch[r][1]);
        }
    }
    int newnode(T val){
        //新建叶子结点
        int now;
        if(q.size())now=q.front(),q.pop();
        else now=++cnt;
        v[now]=val;return now;
    }
    void init(int now){
        mx[now]=sum[now]=v[now];
        lx[now]=rx[now]=max(v[now],0);
        sz[now]=1;
    }
    #define get(x) (x==ch[f[x]][1])
    #define connect(x,y,p) {if(x)f[x]=y;if(y)ch[y][p]=x;}
    void rotate(int x){
        int fa=f[x],ffa=f[fa],p1=get(x),p2=get(fa);
        connect(ch[x][p1^1],fa,p1);
        connect(fa,x,p1^1);
        connect(x,ffa,p2);
        pushup(fa);pushup(x);
    }
    inline void splay(int x,int to){
        for(int fa;(fa=f[x])!=to;rotate(x))
            if(f[fa]!=to)rotate(get(x)==get(fa)?fa:x);
        if(!to)root=x;
    }
    int find(int x,int rk){
        pushdown(x);
        int l=ch[x][0],r=ch[x][1];
        if(sz[l]+1==rk)return x;
        if(sz[l]>=rk)return find(l,rk);
        else return find(r,rk-1-sz[l]);
    }
    void recycle(int x){
        int l=ch[x][0],r=ch[x][1];
        if(l)recycle(l);
        if(r)recycle(r);
        q.push(x);
        f[x]=ch[x][0]=ch[x][1]=tag[x]=rev[x]=0;
    }
    int split(int k,int tot){
        int x=find(root,k),y=find(root,k+tot+1);
        splay(x,0);splay(y,x);
        return ch[y][0];
    }
    void qry(int k,int tot){
        int x=split(k,tot);
        out::write(sum[x]);
        out::putc('\n');
    }
    void modify(int k,int tot,int val){
        int x=split(k,tot),y=f[x];
        v[x]=val;tag[x]=1;sum[x]=sz[x]*val;
        if(val>=0)lx[x]=rx[x]=mx[x]=sum[x];
        else lx[x]=rx[x]=0,mx[x]=val;
        pushup(y);pushup(f[y]);
    }
    void rever(int k,int tot){
        int x=split(k,tot),y=f[x];
        if(!tag[x]){
            rev[x]^=1;
            swap(ch[x][0],ch[x][1]);
            swap(lx[x],rx[x]);
            pushup(y);pushup(f[y]);
        }
    }
    void erase(int k,int tot){
        int x=split(k,tot),y=f[x];
        recycle(x);ch[y][0]=0;
        pushup(y);pushup(f[y]);
    }
    #define mid (l+r>>1)
    int build(T *a,int l,int r){
        int now=newnode(a[mid]);
        if(l==r){
            init(now);
            return now;
        }
        if(l<mid)ch[now][0]=build(a,l,mid-1),f[ch[now][0]]=now;
        if(mid<r)ch[now][1]=build(a,mid+1,r),f[ch[now][1]]=now;
        pushup(now);return now;
    }
    void insert(int k,T *a,int tot){
        int z=build(a,1,tot);;
        int x=find(root,k+1),y=find(root,k+2);
        splay(x,0);splay(y,x);
        f[z]=y,ch[y][0]=z;
        pushup(y);pushup(x);
    }
};

LCT

C++
#define lc c[x][0]
#define rc c[x][1]
struct Link_Cut_Tree{
	int f[N],c[N][2],v[N],s[N];
	bool r[N];
	bool nroot(int x){
		return c[f[x]][0]==x||c[f[x]][1]==x;
	}
	void pushup(int x){
		s[x]=max(v[x],max(s[lc],s[rc]));
	}
	void pushr(int x){
		swap(lc,rc);
		r[x]^=1;
	}
	void pushdown(int x){
		if(r[x]){
			if(lc)pushr(lc);
			if(rc)pushr(rc);
			r[x]=0;
		}
	}
	#define get(x) (c[f[x]][1]==x)
	void rotate(int x){
		int fa=f[x],ffa=f[fa],k=get(x),w=c[x][!k];
		if(nroot(fa))c[ffa][get(fa)]=x;c[x][!k]=fa;c[fa][k]=w;
		if(w)f[w]=fa;f[fa]=x;f[x]=ffa;pushup(fa);
	}
	void splay(int x){
		int y=x;stack<int>s;
		s.push(y);
		while(nroot(y))s.push(y=f[y]);
		while(s.size())pushdown(s.top()),s.pop();
		while(nroot(x)){
			int fa=f[x],ffa=f[fa];
			if(nroot(fa))
				rotate((!get(x))^(!get(fa))?x:fa);
			rotate(x);
		}pushup(x);
	}
	void access(int x){
		for(int y=0;x;x=f[y=x])
			splay(x),rc=y,pushup(x);
	}
	void makeroot(int x){
		access(x);splay(x);
		pushr(x);
	}
	int findroot(int x){
		access(x);splay(x);
		while(lc)pushdown(x),x=lc;
		splay(x);return x;
	}
	void split(int x,int y){
		makeroot(x);
		access(y);splay(y);
	}
	void change(int x,int num){
		splay(x);
		v[x]=num;
	}
	int query(int x,int y){
		makeroot(x);
		if(findroot(y)!=x)return -1;
		split(x,y);
		return s[y];
	}
	bool link(int x,int y){
		makeroot(x);
		if(findroot(y)!=x){f[x]=y;return 1;}
		return 0;
	}
	bool cut(int x,int y){
		makeroot(x);
		if(findroot(y)==x&&f[y]==x&&!c[y][0]){
			f[y]=c[x][1]=0;
			pushup(x);return 1;
		}return 0;
	}
}

可持久化平衡树

C++
const int N=5e5+100;
namespace tree{
	int n,rt[N],cnt;
	struct node{
		int ch[2];
		int rnd,sz,v;
		//rnd随机值,sz子节点的个数
		//cnt节点的个数
	}t[N*50];
	int cpynode(int x){
		t[++cnt]=t[x];
		return cnt;
	}
	int newnode(int x){
		t[++cnt].v=x;t[cnt].sz=1;
		t[cnt].rnd=rand();return cnt;
	}
	#define ls(x) t[(x)].ch[0]
	#define rs(x) t[(x)].ch[1]
	void upd(int k){
		if(k)
			t[k].sz=t[ls(k)].sz+t[rs(k)].sz+1;
	}
	inline void split(int now,int k,int &x,int &y){
		if(!now){x=0,y=0;return;}
		if(t[now].v<=k){
			x=cpynode(now);
			split(rs(x),k,rs(x),y);
		}else{
			y=cpynode(now);
			split(ls(y),k,x,ls(y));
		}upd(x);upd(y);
	}
	inline int merge(int x,int y){
		if(!x||!y)return x+y;//返回不为0的值
		if(t[x].rnd<t[y].rnd){
			int z=cpynode(x);
			rs(z)=merge(rs(z),y);
			upd(z);return z;
		} else{
			int z=cpynode(y);
			ls(z)=merge(x,ls(z));
			upd(z);return z;
		}
	}
	inline void insert(int now,int k){
		int x=0,y=0,z=0;
		split(rt[now],k,x,y);
		z=newnode(k);
		rt[now]=merge(merge(x,z),y);
	}
	inline void del(int now,int k){
		int x=0,y=0,z=0;
		split(rt[now],k,x,y);//先把大于k的结点分离
		split(x,k-1,x,z);//保证z中只有值为k的结点
		z=merge(ls(z),rs(z));//若值为k的结点有多个,则合并后少一	个,如果原本只有一个或者没有,则返回0,相当于删除了值为k的点
		rt[now]=merge(merge(x,z),y);//合并复原
	}
	inline int rnk(int now,int k){
	    int x=0,y=0;
	    split(rt[now],k-1,x,y);//以x为根的树中包含了所有值小于k的结点。
	    return t[x].sz+1;//结点数+1就是k的排名
	}
	inline int kth(int x,int k){
		while(1){
			if(t[ls(x)].sz+1==k)return t[x].v;
			else if(t[ls(x)].sz>=k)x=ls(x);
			else k-=t[ls(x)].sz+1,x=rs(x);
		}
	}
	inline int pre(int now,int k){
		int x=0,y=0,z=0;
		split(rt[now],k-1,x,y);
		if(!x)return -2147483647;
		return kth(x,t[x].sz);
	}
	inline int suf(int now,int k){
		int x=0,y=0,z=0;
		split(rt[now],k,x,y);
		if(!y)return 2147483647;
		return kth(y,1);
	}
}
原文