跳到正文

个人记录

板子

yinianxingkong约 58 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 58 分钟
获赞
1

版本

  1. v1当前

想了想还是放博客比较好,有索引。

全体 namespacenamespace 方便 copycopy。

Edge with DFN

C++
namespace Edge{
		int h[N],etot,dfn[N],dcnt,fa[N],dep[N],sz[N];
		struct edge{int to,nxt;}e[N<<1];
		il vd lj(int u,int v){e[++etot]=(edge){v,h[u]},h[u]=etot;}
		vd dfs1(int u){
			dfn[u]=++dcnt,sz[u]=1;
			for(int i=h[u],v=e[i].to;i;v=e[i=e[i].nxt].to) if(v!=fa[u]) dep[v]=dep[fa[v]=u]+1,dfs1(v),sz[u]+=sz[v];
		}
	}using namespace Edge;

Edge with LCA

C++
namespace Edge{
		#define CMP(a,b) (dfn[a]<dfn[b]?(a):(b))
		int h[N],etot,dfn[N],dcnt,st[N][21],fa[N],dep[N],sz[N];
		struct edge{int to,nxt;}e[N<<1];
		il vd lj(int u,int v){e[++etot]=(edge){v,h[u]},h[u]=etot;}
		vd dfs1(int u){
			st[dfn[u]=++dcnt][0]=fa[u],sz[u]=1;
			for(int i=h[u],v=e[i].to;i;v=e[i=e[i].nxt].to) if(v!=fa[u]) dep[v]=dep[fa[v]=u]+1,dfs1(v),sz[u]+=sz[v];
		}il vd init_RMQ(){for(int j=1;(1<<j)<=n;j++) for(int i=1;i+(1<<j)-1<=n;i++) st[i][j]=CMP(st[i][j-1],st[i+(1<<j-1)][j-1]);}
		il int lca(int u,int v){
			if(u==v) return u;u=dfn[u],v=dfn[v];
			if(u>v) swap(u,v);int d=__lg(v-u++);
			return CMP(st[u][d],st[v-(1<<d)+1][d]);
		}
	}using namespace Edge;

Edge with Tree List Section

C++
namespace Edge{
	int h[N],etot,dfn[N],dcnt,DFS[N],fa[N],dep[N],sz[N],dson[N],top[N];
	struct edge{int to,nxt;}e[N<<1];
	struct list{int l,r;}b[N];int bcnt;
	il vd lj(int u,int v){e[++etot]=(edge){v,h[u]},h[u]=etot;}
	vd dfs1(int u){
		sz[u]=1;
		for(int i=h[u];i;i=e[i].nxt){
			int v=e[i].to;
			if(v==fa[u]) continue;
			fa[v]=u,dep[v]=dep[u]+1,dfs1(v),sz[u]+=sz[v];
			if(sz[dson[u]]<sz[v]) dson[u]=v;
		}
	}
	vd dfs2(int u,int tp){
		DFS[dfn[u]=++dcnt]=u,top[u]=tp;
		if(dson[u]) dfs2(dson[u],tp);
		for(int i=h[u];i;i=e[i].nxt){
			int v=e[i].to;
			if(v==fa[u]||v==dson[u]) continue;
			dfs2(v,v);
		}
	}
	il vd sublist(int u,int v){
		bcnt=0;
		while(top[u]!=top[v]){
			if(dep[top[u]]<dep[top[v]]) swap(u,v);
			b[++bcnt]=(list){dfn[top[u]],dfn[u]};
			u=fa[top[u]];
		}
		if(dep[u]>dep[v]) swap(u,v);
		b[++bcnt]=(list){dfn[u],dfn[v]};
	}
	il int lca(int u,int v){
		while(top[u]!=top[v]){
			if(dep[top[u]]<dep[top[v]]) swap(u,v);
			u=fa[top[u]];
		}
		if(dep[u]>dep[v]) swap(u,v);
		return u;
	}
}using namespace Edge;

DSU on Tree

C++
namespace Edge{
	int h[N],etot,fa[N],sz[N],dson[N],dfn[N],dcnt,DFS[N];
	struct edge{int to,nxt;}e[N<<1];
	il vd lj(int u,int v){e[++etot]=(edge){v,h[u]},h[u]=etot;}
	vd dfs1(int u){
		sz[u]=1;dfn[u]=++dcnt;DFS[dcnt]=u;
		for(int i=h[u];i;i=e[i].nxt){
			int v=e[i].to;
			if(v==fa[u]) continue;
			fa[v]=u;dfs1(v);sz[u]+=sz[v];
			if(sz[v]>sz[dson[u]]) dson[u]=v;
		}
	}
	il vd add(int x){
		;
	}
	il vd del(int x){
		;
	}
	vd dfs2(int u,bl flag=1){
		for(int i=h[u];i;i=e[i].nxt){
			int v=e[i].to;
			if(v==fa[u]||v==dson[u]) continue;
			dfs2(v);
		}
		if(dson[u]) dfs2(dson[u],0);
		for(int i=h[u];i;i=e[i].nxt){
			int v=e[i].to;
			if(v==fa[u]||v==dson[u]) continue;
			for(int j=dfn[v];j<=dfn[v]+sz[v]-1;j++) add(s[DFS[j]]);
		}
		add(s[u]);
		ans[u]=now;
		if(flag) for(int i=dfn[u];i<=dfn[u]+sz[u]-1;i++) del(s[DFS[i]]);
	}
}using namespace Edge;

Segment Tree

C++
namespace Segment_Tree{
	#define Z k<<1
	#define Y k<<1|1
	#define ls Z,l,mid
	#define rs Y,mid+1,r
	struct node{int v,lz;}t[N<<2];
	il node merge(node a,node b){return (node){a.v+b.v,0};}
	il vd pushup(int k){t[k]=merge(t[Z],t[Y]);}
	il vd adder(int k,int l,int r,int v){
		t[k].v+=(r-l+1)*v;
		t[k].lz+=v;
	}
	il vd pushdown(int k,int l,int r){
		if(!t[k].lz||l==r) return;
		int mid=l+r>>1;
		adder(ls,t[k].lz),adder(rs,t[k].lz);
		t[k].lz=0;
	}
	vd build(int k,int l,int r){
		if(l==r) return void(t[k].v=s[l]);
		int mid=l+r>>1;
		build(ls),build(rs);
		pushup(k);
	}
	vd update(int k,int l,int r,int L,int R,int v){
		if(L<=l&&r<=R) return adder(k,l,r,v);
		pushdown(k,l,r);
		int mid=l+r>>1;
		if(L<=mid) update(ls,L,R,v);
		if(R>mid) update(rs,L,R,v);
		pushup(k);
	}
	node query(int k,int l,int r,int L,int R){
		if(L<=l&&r<=R) return t[k];
		pushdown(k,l,r);
		int mid=l+r>>1;
		if(L<=mid&&R>mid) return merge(query(ls,L,R),query(rs,L,R));
		if(L<=mid) return query(ls,L,R);
		return query(rs,L,R);
	}
}using namespace Segment_Tree;

Merge Segment Tree

C++
namespace Segment_Tree{
	#define Z t[k].son[0]
	#define Y t[k].son[1]
	#define ls Z,l,mid
	#define rs Y,mid+1,r
	int Node;
	struct node{int son[2],v;}t[N*60];
	il int neww(){return ++Node;}
	il vd pushup(int k){t[k].v=t[Z].v+t[Y].v;}
	vd update(int &k,int l,int r,int pos,int v){
		if(!k) k=neww();
		if(l==r) return void(t[k].v+=v);
		int mid=l+r>>1;
		if(pos<=mid) update(ls,pos,v);
		else update(rs,pos,v);
		pushup(k);
	}
	int merge(int a,int b,int l,int r){
		if(!a||!b) return a|b;
		if(l==r){t[a].v+=t[b].v;return a;}
		int mid=l+r>>1;
		t[a].son[0]=merge(t[a].son[0],t[b].son[0],l,mid);
		t[a].son[1]=merge(t[a].son[1],t[b].son[1],mid+1,r);
		pushup(a);return a;
	}
}using namespace Segment_Tree;

Chairman_Tree

C++
namespace Chairman_Tree{
	#define Z t[k].son[0]
	#define Y t[k].son[1]
	#define ls Z,l,mid
	#define rs Y,mid+1,r
	int Node;
	struct node{int son[2],v;}t[N*60];
	il int neww(int k=0){t[++Node]=t[k];return Node;}
	il vd pushup(int k){t[k].v=min(t[Z].v,t[Y].v);}
	int build(int l,int r){
		int k=neww();t[k].v=INF;
		if(l==r) return k;
		int mid=l+r>>1;
		Z=build(l,mid),Y=build(mid+1,r);
		return k;
	}
	int update(int k,int l,int r,int pos,int v,bl New=1){
		if(New) k=neww(k);
		t[k].v=min(t[k].v,v);
		if(l==r) return k;
		int mid=l+r>>1;
		if(pos<=mid) Z=update(ls,pos,v);
		else Y=update(rs,pos,v);
		return k;
	}
	int query(int k,int l,int r,int L,int R){
		if(!k) return INF;
		if(L<=l&&r<=R) return t[k].v;
		int mid=l+r>>1;
		if(L<=mid&&R>mid) return min(query(ls,L,R),query(rs,L,R));
		else if(L<=mid) return query(ls,L,R);
		return query(rs,L,R);
	}
}using namespace Chairman_Tree;

Merge 01-Trie

C++
namespace Trie{
	int Node,st[N*7],top;
	const int Base=17;
	struct node{int son[2];ll v;}t[N*7];
	il int neww(){
		int k=top?st[top--]:++Node;
		t[k].son[0]=t[k].son[1]=t[k].v=0;
		return k;
	}
	il vd pushup(int k){t[k].v=t[t[k].son[0]].v+t[t[k].son[1]].v;}
	vd ins(int &k,int pos,int v,int lim=Base){
		if(!k) k=neww();
		if(!~lim) return void(t[k].v+=v);
		bl to=(pos>>lim)&1;
		ins(t[k].son[to],pos,v,lim-1),pushup(k);
	}
	vd split(int k,int &l,int &r,int pos,int lim=Base){
		if(!k) return void(l=r=0);
		if(!~lim) return l=k,r=0,void(0);
		if((pos>>lim)&1) l=k,r=neww(),split(t[k].son[1],t[l].son[1],t[r].son[1],pos,lim-1);
		else r=k,l=neww(),split(t[k].son[0],t[l].son[0],t[r].son[0],pos,lim-1);
		pushup(l),pushup(r);
	}
	int merge(int a,int b,int lim=Base){
		if(!a||!b) return a|b;
		if(!~lim){t[a].v+=t[b].v,st[++top]=b;return a;}
		t[a].son[0]=merge(t[a].son[0],t[b].son[0],lim-1);
		t[a].son[1]=merge(t[a].son[1],t[b].son[1],lim-1);
		pushup(a);st[++top]=b;return a;
	}
	int query(int k,int v,int lim=Base){
		if(!~lim) return 0;
		if(t[t[k].son[0]].v>=v) return query(t[k].son[0],v,lim-1);
		return query(t[k].son[1],v-t[t[k].son[0]].v,lim-1)|(1<<lim);
	}
}using namespace Trie;

Merge Heap

C++
namespace Heap{
	struct node{int v,son[2],fa,dis;}t[N];
	int find(int x){return (x==t[x].fa)?(x):(t[x].fa=find(t[x].fa));}
	int merge(int a,int b){
		if(!a||!b) return a|b;
		if(t[a].v>t[b].v||t[a].v==t[b].v&&a>b) swap(a,b);
		t[a].son[1]=merge(t[a].son[1],b);
		if(t[t[a].son[0]].dis<t[t[a].son[1]].dis) swap(t[a].son[0],t[a].son[1]);
		t[t[a].son[0]].fa=t[t[a].son[1]].fa=t[a].fa=a;
		t[a].dis=t[t[a].son[1]].dis+1;
		return a;
	}
	il vd pop(int a){
		t[a].v=-1;
		t[t[a].son[0]].fa=t[a].son[0];
		t[t[a].son[1]].fa=t[a].son[1];
		t[a].fa=merge(t[a].son[0],t[a].son[1]);
	}
}using namespace Heap;

Fhq_Treep

C++
namespace Fhq_Treap{
	#define VAL 0
	#define RNK 1
	int Node,st[N],top,tl,tm,tr;
	struct node{int son[2],sz,v;ll rd;}t[N];
	il int neww(int v=0){
		int k;
		if(top){
			k=st[top--];
			if(t[k].son[0]) st[++top]=t[k].son[0];
			if(t[k].son[1]) st[++top]=t[k].son[1];
		}else k=++Node;
		t[k]=(node){0,0,1,v,rand()};
		return k;
	}
	il vd pushup(int k){t[k].sz=t[t[k].son[0]].sz+t[t[k].son[1]].sz+1;}
	vd split(int k,int &l,int &r,int v,bl type){
		if(!k) return void(l=r=0);
		if(type){
			if(t[t[k].son[0]].sz>=v) r=k,split(t[k].son[0],l,t[r].son[0],v,type);
			else l=k,split(t[k].son[1],t[l].son[1],r,v-t[t[k].son[0]].sz-1,type);
		}else{
			if(t[k].v<=v) l=k,split(t[l].son[1],t[l].son[1],r,v,type);
			else r=k,split(t[r].son[0],l,t[r].son[0],v,type);
		}
		pushup(k);
	}
	int merge(int l,int r){
		if(!l||!r) return l|r;
		if(t[l].rd<=t[r].rd){
			t[l].son[1]=merge(t[l].son[1],r);
			pushup(l);return l;
		}
		t[r].son[0]=merge(l,t[r].son[0]);
		pushup(r);return r;
	}
	il vd ins(int &rt,int v){
		split(rt,tl,tr,v,VAL);
		rt=merge(merge(tl,neww(v)),tr);
	}
	il vd del(int &rt,int v){
		split(rt,tl,tr,v,VAL);
		split(tl,tl,tm,t[tl].sz-1,RNK);
		rt=merge(tl,tr),st[++top]=tm;
	}
	il vd link(int &rt,int k){
		if(!k) return;
		ins(rt,t[k].v);
		link(rt,t[k].son[0]);
		link(rt,t[k].son[1]);
	}
	il int rnk(int &rt,int v){
		split(rt,tl,tr,v-1,VAL);
		int tmp=t[tl].sz+1;
		rt=merge(tl,tr);
		return tmp;
	}
	il int kth(int &rt,int v){
		split(rt,tl,tr,v-1,RNK);
		split(tr,tm,tr,1,RNK);
		int tmp=t[tm].v;
		rt=merge(merge(tl,tm),tr);
		return tmp;
	}
	il int pre(int &rt,int v){
		split(rt,tl,tr,v-1,VAL);
		split(tl,tl,tm,t[tl].sz-1,RNK);
		int tmp=t[tm].v;
		rt=merge(merge(tl,tm),tr);
		return tmp;
	}
	il int suf(int &rt,int v){
		split(rt,tl,tr,v,VAL);
		split(tr,tm,tr,1,RNK);
		int tmp=t[tm].v;
		rt=merge(merge(tl,tm),tr);
		return tmp;
	}
}using namespace Fhq_Treap;

Scape Goat Tree

C++
namespace Scape_Goat_Tree{
	const double A=0.75;
	int Node,arr[N],tail;
	struct node{int son[2],v,sz,point,cnt;}t[N];
	il int neww(){
		int k=++Node;
		t[k].son[0]=t[k].son[1]=t[k].v=t[k].sz=t[k].point=t[k].cnt=0;
		return k;
	}
	il vd pushup(int k){t[k].sz=t[t[k].son[0]].sz+t[t[k].son[1]].sz+t[k].cnt,t[k].point=t[t[k].son[0]].point+t[t[k].son[1]].point+1;}
	vd Re_arr(int k){
		if(t[k].son[0]) Re_arr(t[k].son[0]);
		if(t[k].cnt) arr[++tail]=k;
		if(t[k].son[1]) Re_arr(t[k].son[1]);
	}
	vd Re_tree(int &k,int l,int r){
		if(l==r){
			k=arr[l];
			t[k].son[0]=t[k].son[1]=0;t[k].point=1;
			t[k].sz=t[k].cnt;
			return;
		}
		int mid=l+r>>1;
		k=arr[mid];
		t[k].son[0]=t[k].son[1]=0;
		if(l<mid) Re_tree(t[k].son[0],l,mid-1);
		if(r>mid) Re_tree(t[k].son[1],mid+1,r);
		pushup(k);
	}
	il vd Re_build(int &k){
		tail=0;
		Re_arr(k);
		Re_tree(k,1,tail);
	}
	il bl Can_build(int k){return t[k].cnt&&A*t[k].sz<=max(1.0*t[t[k].son[0]].sz,1.0*t[t[k].son[1]].sz)||A*t[k].sz>=1.0*t[k].point;}
	vd ins(int &k,int v){
		if(!k){
			k=neww();
			t[k].v=v,t[k].cnt=t[k].sz=1;
			return;
		}
		if(v==t[k].v) ++t[k].cnt;
		else if(v<t[k].v) ins(t[k].son[0],v);
		else ins(t[k].son[1],v);
		pushup(k);
		if(Can_build(k)) Re_build(k);
	}
	vd del(int &k,int v){
		if(!k) return;
		if(v==t[k].v) --t[k].cnt;
		else if(v<t[k].v) del(t[k].son[0],v);
		else del(t[k].son[1],v);
		pushup(k);
		if(Can_build(k)) Re_build(k);
	}
	il int rnk(int k,int v){
		int sum=1;
		while(k){
			if(v<=t[k].v) k=t[k].son[0];
			else sum+=t[t[k].son[0]].sz+t[k].cnt,k=t[k].son[1];
		}
		return sum;
	}
	il int kth(int k,int v){
		while(k){
			if(v<=t[t[k].son[0]].sz) k=t[k].son[0];
			else if(v<=t[t[k].son[0]].sz+t[k].cnt&&t[t[k].son[0]].sz+1<=v) return t[k].v;
			else v-=t[t[k].son[0]].sz+t[k].cnt,k=t[k].son[1];
		}
		return 0;
	}
	il int pre(int v){return kth(rt,rnk(rt,v)-1);}
	il int suf(int v){return kth(rt,rnk(rt,v+1));}
}using namespace Scape_Goat_Tree;

LCT

C++
namespace LCT{
	struct node{int fa,son[2],v,sum;bl reved;}t[N];
	#define isroot(x) (t[t[x].fa].son[0]!=x&&t[t[x].fa].son[1]!=x)
	#define get(x) (t[t[x].fa].son[1]==x)
	#define connect(x,y,w) t[x].son[w]=y,t[y].fa=x
	il vd pushup(int x){t[x].sum=t[t[x].son[0]].sum^t[x].v^t[t[x].son[1]].sum;}
	il vd rever(int x){if(x) swap(t[x].son[0],t[x].son[1]),t[x].reved^=1;}
	il vd pushdown(int x){if(t[x].reved) rever(t[x].son[0]),rever(t[x].son[1]),t[x].reved=0;}
	il vd update(int x){if(!isroot(x)) update(t[x].fa);pushdown(x);}
	il vd rotate(int x){
		int y=t[x].fa,z=t[y].fa;bl w=get(x);
		if(!isroot(y)) t[z].son[get(y)]=x;
		t[x].fa=z,connect(y,t[x].son[w^1],w),connect(x,y,w^1);
		pushup(y),pushup(x);
	}
	il vd splay(int x){
		for(update(x);!isroot(x);rotate(x))
		if(!isroot(t[x].fa)) rotate(get(t[x].fa)!=get(x)?x:t[x].fa);
	}
	il vd access(int x){for(int y=0;x;y=x,x=t[x].fa) splay(x),t[x].son[1]=y,pushup(x);}
	il vd mkroot(int x){access(x),splay(x),rever(x);}
	il int fdroot(int x){
		access(x),splay(x);
		while(t[x].son[0]) x=t[x].son[0];
		return splay(x),x;
	}
	il vd split(int x,int y){mkroot(x),access(y),splay(y);}
	il vd link(int x,int y){if(mkroot(x),fdroot(y)!=x) t[x].fa=y;}
	il vd cut(int x,int y){if(mkroot(x),fdroot(y)==x&&t[y].fa==x&&!t[y].son[0]) t[y].fa=t[x].son[1]=0;}
}using namespace LCT;

VAL WBLT

C++
namespace WBLT{
	int Node,rt,st[N],top;
	struct node{int son[2],v,sz;}t[N<<1];
	#define isleaf(x) (!t[x].son[0]||!t[x].son[1])
	il int neww(int v){int k=top?st[top--]:++Node;return t[k]=(node){0,0,v,1},k;}
	il vd pushup(int x){if(!isleaf(x)) t[x].sz=t[t[x].son[0]].sz+t[t[x].son[1]].sz,t[x].v=t[t[x].son[1]].v;}
	il vd rotate(int x,bl w){
		int y=t[x].son[w];
		t[x].son[w]=t[t[x].son[w]].son[w];
		t[y].son[w]=t[y].son[w^1];
		t[y].son[w^1]=t[x].son[w^1];
		t[x].son[w^1]=y;
		pushup(y),pushup(x);
	}
	il vd maintain(int x){
		if(t[t[x].son[0]].sz>t[t[x].son[1]].sz*3){
			if(t[t[t[x].son[0]].son[1]].sz>t[t[t[x].son[0]].son[0]].sz*2) rotate(t[x].son[0],1);rotate(x,0);
		}else if(t[t[x].son[1]].sz>t[t[x].son[0]].sz*3){
			if(t[t[t[x].son[1]].son[0]].sz>t[t[t[x].son[1]].son[1]].sz*2) rotate(t[x].son[1],0);rotate(x,1);
		}
	}
	vd ins(int x,int v){
		if(isleaf(x)){
			if(v>t[x].v) swap(t[x].v,v);
			t[x].son[0]=neww(v),t[x].son[1]=neww(t[x].v);
			return pushup(x);
		}
		if(v<=t[t[x].son[0]].v) ins(t[x].son[0],v);
		else ins(t[x].son[1],v);
		pushup(x),maintain(x);
	}
	vd del(int x,int v,int lst=0){
		if(isleaf(x)) return t[lst]=t[t[lst].son[x==t[lst].son[0]]],void(st[++top]=x);
		if(v<=t[t[x].son[0]].v) del(t[x].son[0],v,x);
		else del(t[x].son[1],v,x);
		pushup(x),maintain(x);
	}
	int fdrnk(int x,int v){
		if(isleaf(x)) return t[x].v<=v;
		if(t[t[x].son[0]].v>v) return fdrnk(t[x].son[0],v);
		return fdrnk(t[x].son[1],v)+t[t[x].son[0]].sz;
	}
	int fdkth(int x,int v){
		if(t[x].sz==v) return t[x].v;
		if(t[t[x].son[0]].sz>=v) return fdkth(t[x].son[0],v);
		return fdkth(t[x].son[1],v-t[t[x].son[0]].sz);
	}
	il int fdpre(int x,int v){return fdkth(x,fdrnk(x,v-1));}
	il int fdsuf(int x,int v){return fdkth(x,fdrnk(x,v)+1);}
	int merge(int x,int y){
		if(!x||!y) return x|y;
		if(4*min(t[x].sz,t[y].sz)>=t[x].sz+t[y].sz){
			int z=neww(0);t[z].son[0]=x,t[z].son[1]=y;
			return pushup(z),z;
		}
		if(t[x].sz>=t[y].sz){
			if(4*t[t[x].son[0]].sz>=t[x].sz+t[y].sz) return merge(t[x].son[0],merge(t[x].son[1],y));
			return merge(merge(t[x].son[0],t[t[x].son[1]].son[0]),merge(t[t[x].son[1]].son[1],y));
		}else{
			if(4*t[t[y].son[1]].sz>=t[x].sz+t[y].sz) return merge(merge(x,t[y].son[0]),t[y].son[1]);
			return merge(merge(x,t[t[y].son[0]].son[0]),merge(t[t[y].son[0]].son[1],t[y].son[1]));
		}
	}
	vd split(int x,int v,int &l,int &r){
		if(isleaf(x)){
			if(v) l=x,r=0;
			else l=0,r=x;
			return;
		}
		if(t[t[x].son[0]].sz>=v) split(t[x].son[0],v,l,r),r=merge(r,t[x].son[1]);
		else split(t[x].son[1],v-t[t[x].son[0]].sz,l,r),l=merge(t[x].son[0],l);
		st[++top]=x;
	}
}using namespace WBLT;

KTH WBLT

C++
namespace WBLT{
	int Node,rt,st[N*30],top,tl,tm,tr;
	struct node{int son[2],v,sz;bl reved;}t[N*30];
	#define isleaf(x) (!t[x].son[0]||!t[x].son[1])
	il int neww(int v=0){int k=top?st[top--]:++Node;return t[k]=(node){0,0,v,1,0},k;}
	il vd pushup(int x){if(!isleaf(x)) t[x].sz=t[t[x].son[0]].sz+t[t[x].son[1]].sz,t[x].v=t[t[x].son[1]].v;}
	il vd rever(int x){if(x) swap(t[x].son[0],t[x].son[1]),t[x].reved^=1;}
	il vd pushdown(int x){if(t[x].reved) rever(t[x].son[0]),rever(t[x].son[1]),t[x].reved=0;}
	il int connect(int x,int y){int z=neww();t[z].son[0]=x,t[z].son[1]=y;return pushup(z),z;}
	il vd rotate(int x,bl w){
		int y=t[x].son[w];
		t[x].son[w]=t[t[x].son[w]].son[w];
		t[y].son[w]=t[y].son[w^1];
		t[y].son[w^1]=t[x].son[w^1];
		t[x].son[w^1]=y;
		pushup(y),pushup(x);
	}
	il vd maintain(int x){
		if(t[t[x].son[0]].sz>t[t[x].son[1]].sz*3){
			if(t[t[t[x].son[0]].son[1]].sz>t[t[t[x].son[0]].son[0]].sz*2) rotate(t[x].son[0],1);rotate(x,0);
		}else if(t[t[x].son[1]].sz>t[t[x].son[0]].sz*3){
			if(t[t[t[x].son[1]].son[0]].sz>t[t[t[x].son[1]].son[1]].sz*2) rotate(t[x].son[1],0);rotate(x,1);
		}
	}
	int build(int l,int r){
		if(l==r) return neww(l);
		int mid=l+r>>1;
		return connect(build(l,mid),build(mid+1,r));
	}
	int merge(int x,int y){
		if(!x||!y) return x|y;
		if(4*min(t[x].sz,t[y].sz)>=t[x].sz+t[y].sz) return connect(x,y);
		if(t[x].sz>=t[y].sz){
			pushdown(x);
			if(4*t[t[x].son[0]].sz>=t[x].sz+t[y].sz) return merge(t[x].son[0],merge(t[x].son[1],y));
			return pushdown(t[x].son[1]),merge(merge(t[x].son[0],t[t[x].son[1]].son[0]),merge(t[t[x].son[1]].son[1],y));
		}else{
			pushdown(y);
			if(4*t[t[y].son[1]].sz>=t[x].sz+t[y].sz) return merge(merge(x,t[y].son[0]),t[y].son[1]);
			return pushdown(t[y].son[0]),merge(merge(x,t[t[y].son[0]].son[0]),merge(t[t[y].son[0]].son[1],t[y].son[1]));
		}
	}
	vd split(int x,int v,int &l,int &r){
		if(isleaf(x)){
			if(v) l=x,r=0;
			else l=0,r=x;
			return;
		}
		pushdown(x);
		if(t[t[x].son[0]].sz>=v) split(t[x].son[0],v,l,r),r=merge(r,t[x].son[1]);
		else split(t[x].son[1],v-t[t[x].son[0]].sz,l,r),l=merge(t[x].son[0],l);
		st[++top]=x;
	}
}using namespace WBLT;

计算几何

C++
namespace naive{
		const db eps=1e-8;
		const db Pi=3.141592653589793;
		il int sgn(db x){if(fabs(x)<eps)return 0;return x<0?-1:1;}
		struct Point{db x,y;Point(db x=0.0,db y=0.0):x(x),y(y){}};
		typedef Point Vector;
		Vector operator+(const Vector&a,const Vector&b){return Vector(a.x+b.x,a.y+b.y);}
		Vector operator-(const Vector&a,const Vector&b){return Vector(a.x-b.x,a.y-b.y);}
		Vector operator*(const Vector&a,const db&p){return Vector(a.x*p,a.y*p);}
		Vector operator/(const Vector&a,const db&p){return Vector(a.x/p,a.y/p);}
		bl operator==(const Point&a,const Point&b){return !sgn(a.x-b.x)&&!sgn(a.y-b.y);}
		il db dottle(const Vector&a,const Vector&b){return a.x*b.x+a.y*b.y;}
		il db cross(const Vector&a,const Vector&b){return a.x*b.y-a.y*b.x;}
		il db angle(const Vector&a){return atan2(a.y,a.x);}
		il db modul(const Vector&a){return sqrt(a.x*a.x+a.y*a.y);}
		il db dist(const Point&a,const Point&b){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}
		il int CmpPointHor(const Point&a,const Point&b){return a.x<b.x||!sgn(a.x-b.x)&&a.y<b.y;}
		il int CmpVectorPol(const Vector&a,const Vector&b){return angle(a)<angle(b);}
		struct Line{Point p;Vector k;Line(Point a=Point(),Vector b=Vector()):p(a),k(b){}};
		il int OnRight(const Line&a,const Vector&b){return sgn(cross(a.k,b-a.p));}
		il int CmpLinePol(const Line&a,const Line&b){return angle(a.k)<angle(b.k);}
		il Point ITS(const Line&a,const Line&b){return a.p+a.k*(cross(b.k,a.p-b.p)/cross(a.k,b.k));}
	}using namespace naive;
	namespace Polygons{
		il vd Andrew(Point*p,int n,Point*Poly,int&m){
			sort(p+1,p+n+1,CmpPointHor),m=0;
			for(int i=1;i<=n;Poly[++m]=p[i++])while(m>1&&sgn(cross(Poly[m]-Poly[m-1],p[i]-Poly[m-1]))<=0)--m;
			for(int i=n-1,k=m;i;Poly[++m]=p[i--])while(m>k&&sgn(cross(Poly[m]-Poly[m-1],p[i]-Poly[m-1]))<=0)--m;
			m-=n>1;
		}
		il vd LCS(Point*p,int n,Point*Poly,int&m){sort(p+1,p+n+1,CmpPointHor),m=0;for(int i=1;i<=n;Poly[++m]=p[i++])while(m>1&&sgn(cross(Poly[m]-Poly[m-1],p[i]-Poly[m-1]))<=0)--m;}
		il vd UCS(Point*p,int n,Point*Poly,int&m){sort(p+1,p+n+1,CmpPointHor),m=0;for(int i=n;i;Poly[++m]=p[i--])while(m>1&&sgn(cross(Poly[m]-Poly[m-1],p[i]-Poly[m-1]))<=0)--m;}
		il db PolyPer(Point*p,int n){db res=0;for(int i=1;i<n;i++)res+=dist(p[i],p[i+1]);return res+dist(p[n],p[1]);}
		il db PolyArea(Point*p,int n){db res=0;for(int i=1;i<n;i++)res+=cross(p[i],p[i+1]);return (res+cross(p[n],p[1]))/2;}
	}using namespace Polygons;
	namespace HalfPlane{
		Line que[N];int head,tail;
		il bl HPI(Point*p,Line*l,int n){
			sort(l+1,l+1+n,CmpLinePol);
			head=tail=0,que[0]=l[1];
			for(int i=2;i<=n;i++){
				while(head<tail&&OnRight(l[i],p[tail])<=0)--tail;
				while(head<tail&&OnRight(l[i],p[head+1])<=0)++head;
				que[++tail]=l[i];
				if(sgn(cross(que[tail].k,que[tail-1].k))==0){
					if(OnRight(que[tail],que[tail-1].p)<=0&&sgn(dottle(que[tail].k,que[tail-1].k))<=0)return 0;
					--tail;if(OnRight(que[tail],l[i].p)==1)que[tail]=l[i];
				}
				if(head<tail)p[tail]=ITS(que[tail],que[tail-1]);
			}
			while(head<tail&&OnRight(que[head],p[tail])<=0)--tail;
			return tail-head<=1?0:p[head]=ITS(que[head],que[tail]),1;
		}
	}using namespace HalfPlane;
	namespace DynamicPolygon{
		struct CMPPointHor{bl operator()(const Point&a,const Point&b)const{return a.x<b.x||!sgn(a.x-b.x)&&a.y<b.y;}};
		typedef set<Point,CMPPointHor> DynPoly;
		il vd DPInit(DynPoly&DPoly){DynPoly().swap(DPoly),DPoly.insert(Point(-INF,-IINF)),DPoly.insert(Point(INF,-IINF));}
		il vd DPUpdate(Point a,Point b,bl type,db&Cir,db&Mea){if(sgn(a.x-INF)&&sgn(a.x+INF)&&sgn(b.x-INF)&&sgn(b.x+INF))Cir+=type?dist(a,b):-dist(a,b),Mea+=type?-cross(a,b):cross(a,b);}
		il vd DPLink(Point a,Point b,Point c,db&Cir,db&Mea){DPUpdate(a,c,0,Cir,Mea),DPUpdate(a,b,1,Cir,Mea),DPUpdate(b,c,1,Cir,Mea);}
		il vd DPCut(Point a,Point b,Point c,db&Cir,db&Mea){DPUpdate(a,c,1,Cir,Mea),DPUpdate(a,b,0,Cir,Mea),DPUpdate(b,c,0,Cir,Mea);}
		il Point DPHead(DynPoly&DPoly){return *(++DPoly.begin());}
		il Point DPTail(DynPoly&DPoly){return *(----DPoly.end());}
		il vd DPInsert(DynPoly&DPoly,Point p,db&Cir,db&Mea){
			if(DPoly.find(p)!=DPoly.end())return;
			DynPoly::iterator it=DPoly.insert(p).fi;
			DynPoly::iterator pre=prev(it),suf=next(it);
			if(sgn(cross(*suf-p,p-*pre))<=0)return DPoly.erase(it),void(0);
			DPLink(*pre,p,*suf,Cir,Mea);
			while(pre!=DPoly.begin()){
				DynPoly::iterator ppre=prev(pre);
				if(sgn(cross(p-*pre,*pre-*ppre))>0)break;
				DPCut(*ppre,*pre,p,Cir,Mea),DPoly.erase(pre),pre=ppre;
			}while(suf!=--DPoly.end()){
				DynPoly::iterator ssuf=next(suf);
				if(sgn(cross(*ssuf-*suf,*suf-p))>0)break;
				DPCut(p,*suf,*ssuf,Cir,Mea),DPoly.erase(suf),suf=ssuf;
			}
		}
	}using namespace DynamicPolygon;

Slope Trick

C++
namespace Slope_Trick_Tool{
		namespace Leftist_Tree_Tool{
			struct node{int v,son[2],fa,sz,sum,d;}t[N<<2];int Node,st[N<<2],top;
			il int neww(int v=0){int k=top?st[top--]:++Node;return t[k]=(node){v,0,0,k,1,v,0},k;}
			int find(int x){return x==t[x].fa?x:t[x].fa=find(t[x].fa);}
			int merge(int a,int b){
				if(!a||!b)return a|b;
				if(t[a].v<t[b].v)swap(a,b);
				t[a].son[1]=merge(t[a].son[1],b);
				if(t[t[a].son[0]].d<t[t[a].son[1]].d)swap(t[a].son[0],t[a].son[1]);
				return t[a].sz=t[t[a].son[0]].sz+1+t[t[a].son[1]].sz,t[a].sum=t[t[a].son[0]].sum+t[a].v+t[t[a].son[1]].sum,t[a].d=t[t[a].son[1]].d+1,t[t[a].son[0]].fa=t[t[a].son[1]].fa=t[a].fa=a;
			}
		}using namespace Leftist_Tree_Tool;
		namespace Line_Tool{
			struct Line{int k,b;Line(int a1=0,int a2=0){k=a1,b=a2;}vd operator+=(Line&b){this->k+=b.k,this->b+=b.b,b.k=b.b=0;}int operator[](const int&x){return k*x+b;}};
			Line operator+(const Line&a,const Line&b){return Line(a.k+b.k,a.b+b.b);}
		}using namespace Line_Tool;
		struct Slope_Trick{
			Line U;
			struct Leftist_Tree{
				int rt;
				Leftist_Tree(){rt=0;}
				il vd push(int v){rt=merge(rt,neww(v));}
				il vd pop(){st[++Leftist_Tree_Tool::top]=rt,t[t[rt].son[0]].fa=t[rt].son[0],t[t[rt].son[1]].fa=t[rt].son[1],rt=merge(t[rt].son[0],t[rt].son[1]);}
				il int top(){return t[rt].v;}
				il int size(){return t[rt].sz;}
				il int sum(){return t[rt].sum;}
				il bl empty(){return!rt;}
				il vd clear(){rt=0;}
				vd operator+=(Leftist_Tree&b){this->rt=merge(this->rt,b.rt),b.rt=0;}
			}L,R;
			Slope_Trick(){U=Line(),L=R=Leftist_Tree();}
			il vd selfchk(){
				while(L.size()<abs(U.k))L.push(R.top()),R.pop();
				while(L.size()>abs(U.k))R.push(L.top()),L.pop();
			}
			vd operator+=(Slope_Trick&b){
				this->U+=b.U;
				if(this->L.top()<b.L.top())this->R+=b.L,this->R+=b.R;
				else this->L+=b.L,this->L+=b.R;
				selfchk();
			}
			vd operator+=(Line&b){this->U+=b,selfchk();}
			il vd prechk(){R=Leftist_Tree();}
			il vd sufchk(){L=Leftist_Tree();}
			il int Mum(){return Line(U.k+L.size(),U.b-L.sum())[L.top()];}
		};
	}using namespace Slope_Trick_Tool;

Math

C++
namespace Math{
		#define T ll
		il T adding(T a,T b,const T&mod){return (a+=b)>=mod&&(a-=mod),a;}
		il T subing(T a,T b,const T&mod){return (a-=b)<0&&(a+=mod),a;}
		il T seting(T a,const T&mod){return adding(a%mod,mod,mod);}
		il T qpow(T a,T b,const T&mod){//快速幂
			T r=1;
			for(;b;b>>=1,a=1ll*a*a%mod)if(b&1)r=1ll*r*a%mod;
			return r;
		}
		il T smul(T a,T b,const T&mod){//龟速乘
			T r=0;a=seting(a,mod);
			for(;b;b>>=1,a=adding(a,a,mod))if(b&1)r=adding(r,a,mod);
			return r;
		}
		T exgcd(T a,T b,T&x,T&y){
			if(!b)return x=1,y=0,a;
			T c=exgcd(b,a%b,y,x);y-=(a/b)*x;return c;
		}
		struct line{T k,b;line(T a1=1,T a2=0){k=a1,b=a2;}};//y=kx+b
		line operator+(const line&a,const line&b){
			line c;T x,y,d=exgcd(a.k,b.k,x,y);
			c=line(a.k/d*b.k,a.b+smul(x,subing(b.b,a.b%b.k,b.k)/d,b.k/d)*a.k);
			c.b=seting(c.b,c.k);return c;
		}
		il T exfac(T n,T p,const T&pk){
			if(!n)return 1;T r=1;
			for(T i=2;i<=pk;i++)if(i%p)r=1ll*r*i%pk;
			r=qpow(r,n/pk,pk);
			for(T i=2;i<=n%pk;i++)if(i%p)r=1ll*r*i%pk;
			return 1ll*r*exfac(n/p,p,pk)%pk;
		}
		il T exinv(T n,const T&mod){T x,y;exgcd(n,mod,x,y);return adding(x,mod,mod);}
		il T exC(T n,T m,T p,const T&pk){
			if(p==pk)
			T a=exfac(n,p,pk),b=exfac(m,p,pk),c=exfac(n-m,p,pk),r=0;
			for(T i=n/p;i;i/=p)r+=i;
			for(T i=m/p;i;i/=p)r-=i;
			for(T i=(n-m)/p;i;i/=p)r-=i;
			return 1ll*a*exinv(b,pk)%pk*exinv(c,pk)%pk*qpow(p,r,pk)%pk;
		}
		vector<pair<T,T> >P;
		il vd funk(T p){
			vector<pair<T,T> >().swap(P);T pk;
			for(T i=2;i*i<=p;i++)if(p%i==0){
				pk=1;while(p%i==0)pk*=i,p/=i;
				P.pb(MP(i,pk));
			}if(p>1)P.pb(MP(p,p));
		}
		il T exlucas(T n,T m,T mod){
			line r;
			for(pair<T,T>p:P)r=r+line(p.se,exC(n,m,p.fi,p.se));
			return r.b;
		}
		#undef T
	}using namespace Math;

网络流 ISAP

C++
#define FE(u) for(int i=h[u],v=e[i].to;i;v=e[i=e[i].nxt].to)
	int n,m,sp,tp,etot=1,h[N],dep[N],gap[N],q[N],head,tail;ll mxflow;
	struct edge{int to,nxt,val;}e[M<<1];
	il vd lj(int u,int v,int w){e[++etot]=(edge){v,h[u],w},h[u]=etot;}
	il vd bfs(){
		for(int i=1;i<=n;i++)dep[i]=gap[i]=0;q[head=tail=1]=tp,++gap[dep[tp]=1];
		for(int u=q[head];head<=tail;u=q[++head])FE(u)if(!dep[v])q[++tail]=v,++gap[dep[v]=dep[u]+1];
	}int dfs(int u,int flow){
		if(u==tp)return mxflow+=flow,flow;int used=0,P;
		FE(u)if(e[i].val&&dep[u]==dep[v]+1){
			P=dfs(v,min(e[i].val,flow-used)),e[i].val-=P,e[i^1].val+=P;
			if((used+=P)==flow)return used;
		}if(!--gap[dep[u]])dep[sp]=n+1;return ++gap[++dep[u]],used;
	}il ll ISAP(){mxflow=0,bfs();while(dep[sp]<=n)dfs(sp,INF);return mxflow;}

BSS

C++
namespace BSS{
		struct SAM{
			struct state{int son[26],fa,len;int&operator[](const int&pos){return son[pos];}}A[N<<1];
			int State,last,belong[N<<1],mnpos[N<<1],pos[N],cnt[N<<1],sz[N<<1],fail[N<<1][26];vector<int>T[N<<1];
			/*
			pos[i]:第 i 个后缀对应的节点。
			cnt[i]:当前 endpos 等价类每个串出现了多少次。
			mnpos[i]:endpos 等价类最小的一个。反串上即最末的一个。
			sz[i]:endpos 集合的大小。
			fail[i][c]:i 向前添加 c DAG 结点上对应的出边
			*/
			il vd insert(int c){
				int p=last,np=last=++State,q,nq;A[np].len=A[p].len+1;
				for(;p&&!A[p][c];p=A[p].fa)A[p][c]=np;
				if(!p)return void(A[np].fa=1);q=A[p][c];
				if(A[q].len==A[p].len+1)return void(A[np].fa=q);
				A[nq=++State]=A[q],A[nq].len=A[p].len+1,A[q].fa=A[np].fa=nq;
				for(;p&&A[p][c]==q;p=A[p].fa)A[p][c]=nq;
			}
			il vd chkmin(int&x,int v){x=!x?v:!v?x:min(x,v);}
			vd dfs(int u){for(int v:T[u])dfs(v),chkmin(mnpos[u],mnpos[v]),cnt[u]+=cnt[v];}
			il vd build(char*ch,int n){
				State=last=1;for(int i=1;i<=n;i++)insert(ch[i]-'a'),mnpos[pos[i]=last]=i,cnt[last]=1;
				for(int i=2;i<=State;i++)T[A[i].fa].pb(i),sz[i]=A[i].len-A[A[i].fa].len;dfs(1);
				for(int i=2;i<=State;i++)fail[A[i].fa][ch[mnpos[i]-A[A[i].fa].len]-'a']=i;
			}
			il vd merge(){for(int i=State;i>1;i--)if(!belong[i])for(int c=0;c<26;c++)if(A[i][c]){belong[i]=belong[A[i][c]];break;}}
		}PS,NS;
		int occ;vector<int>endpos[N<<1],startpos[N<<1];
		il vd dfs(int u,int v){
			int lenu=PS.A[u].len,lenv=NS.A[v].len;
			if(lenu==lenv&&u!=1&&v!=1)PS.belong[u]=NS.belong[v]=++occ;
			for(int c=0,x,y;c<26;c++){
				x=PS.A[u][c],y=lenu^lenv?v:NS.fail[v][c];
				if(x&&y&&PS.A[x].len==lenu+1)dfs(x,y);
			}
		}
		il vd build(char*ch,int n){
			PS.build(ch,n),reverse(ch+1,ch+1+n),NS.build(ch,n),reverse(ch+1,ch+1+n);
			dfs(1,1),PS.merge(),NS.merge();
			for(int i=PS.State;i>1;i--)endpos[PS.belong[i]].pb(i);//从上往下
			for(int i=NS.State;i>1;i--)startpos[NS.belong[i]].pb(i);//从左往右(相反因为是反串)
		}
	}using namespace BSS;
原文