【Codeforces Round #240 (Div. 1) 】E—Mashmokh's Designed Problem(Spaly)

传送门

题意:给一颗树,每个节点的儿子之间有先后关系
支持询问2点距离,给某个子树换父亲,询问最后一个深度为k的节点

考虑用SplaySplay维护括号序
由于深度变化是连续的,就只需要维护最大最小值
第一个操作求区间最小值,第三个判断k是否在最大最小值区间内即可

注意由于有哨兵节点,所以根的深度设为了1

#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
	static char ibuf[RLEN],*ib,*ob;
	(ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
	return (ob==ib)?EOF:*ib++;
}
#define gc getchar
inline int read(){
	char ch=gc();
	int res=0,f=1;
	while(!isdigit(ch))f^=ch=='-',ch=gc();
	while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
	return f?res:-res;
}
#define ll long long
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define cs const
const int mod=998244353,g=3;
inline int add(int a,int b){return a+b>=mod?a+b-mod:a+b;}
inline void Add(int &a,int b){a=add(a,b);}
inline int dec(int a,int b){return a>=b?a-b:a-b+mod;}
inline void Dec(int &a,int b){a=dec(a,b);}
inline int mul(int a,int b){return 1ll*a*b>=mod?1ll*a*b%mod:a*b;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,a=mul(a,a))(b&1)?(res=mul(res,a)):0;return res;}
inline void chemx(int &a,int b){a<b?a=b:0;}
inline void chemn(int &a,int b){a>b?a=b:0;}
cs int N=400005;
int n,m;
int dfn,in[N],out[N],idx[N];
vector<int> e[N];
namespace Splay{
	int rt,fa[N],son[N][2],mn[N],mx[N],val[N],tag[N];
	#define lc(u) son[u][0]
	#define rc(u) son[u][1]
	inline bool isrc(int u){
		return rc(fa[u])==u;
	}
	inline void addnode(int u,int v){
		mx[u]=mn[u]=val[u]=v;
	}
	inline void pushup(int u){
		mx[u]=mn[u]=val[u];
		if(lc(u))chemx(mx[u],mx[lc(u)]),chemn(mn[u],mn[lc(u)]);
		if(rc(u))chemx(mx[u],mx[rc(u)]),chemn(mn[u],mn[rc(u)]);
	}
	inline void rotate(int v){
		int u=fa[v],z=fa[u];
		int t=rc(u)==v;
		fa[v]=z,son[z][isrc(u)]=v;
		son[u][t]=son[v][t^1],fa[son[v][t^1]]=u;
		son[v][t^1]=u,fa[u]=v;
		pushup(u),pushup(v);
	}
	inline void pushnow(int u,int v){
		mx[u]+=v,mn[u]+=v,val[u]+=v,tag[u]+=v;
	}
	inline void pushdown(int u){
		if(!tag[u])return;
		if(lc(u))pushnow(lc(u),tag[u]);
		if(rc(u))pushnow(rc(u),tag[u]);
		tag[u]=0;
	}
	int stk[N],top;
	inline void splay(int v,int goal){
		stk[top=1]=v;
		for(int x=v;fa[x];x=fa[x])stk[++top]=fa[x];
		for(int i=top;i;i--)pushdown(stk[i]);
		while(fa[v]!=goal){
			int u=fa[v];
			if(fa[u]!=goal)
			isrc(u)==isrc(v)?rotate(u):rotate(v);
			rotate(v);
		}
		if(!goal)rt=v;
	}
	inline void build(int &r1,int l,int r){
		if(l>r){r1=0;return;}
		if(l==r){r1=l;return;}
		r1=(l+r)>>1;
		build(lc(r1),l,r1-1);
		if(lc(r1))fa[lc(r1)]=r1;
		build(rc(r1),r1+1,r);
		if(rc(r1))fa[rc(r1)]=r1;
		pushup(r1);
	}
	inline int pre(int u){
		splay(u,0),u=lc(u);
		while(rc(u))u=rc(u);
		splay(u,0);return u;
	}
	inline int suf(int u){
		splay(u,0),u=rc(u);
		while(lc(u))u=lc(u);
		splay(u,0);return u;
	}
	inline int find(int u,int va){
		while(u){
			pushdown(u);
			if(mx[rc(u)]>=va&&va>=mn[rc(u)])u=rc(u);
			else if(val[u]==va)break;
			else u=lc(u);
		}
		return u;
	}
	inline int dis(int u,int v){
		u=in[u],v=in[v];
		splay(u,0),splay(v,u);
		int t=min(val[u],val[v]);
		if(lc(u)==v&&rc(v))chemn(t,mn[rc(v)]-1);
		if(rc(u)==v&&lc(v))chemn(t,mn[lc(v)]-1);
		return val[u]+val[v]-2*t;
	}
	inline void gofa(int u,int h){
		splay(in[u],0),splay(1,in[u]);
		int goal=idx[find(1,val[in[u]]-h)],pr=pre(in[u]),nxt=suf(out[u]);
		splay(pr,0),splay(nxt,pr);
		int pp=lc(nxt);lc(nxt)=fa[pp]=0;
		pushup(nxt),pushup(pr);
		nxt=out[goal];
		pr=pre(nxt);
		splay(pr,0),splay(nxt,pr);
		lc(nxt)=pp,fa[pp]=nxt;
		pushnow(pp,1-h);
		pushup(nxt),pushup(pr);
	}
}
inline void dfs(int u,int dep){
	in[u]=++dfn,idx[dfn]=u,Splay::addnode(dfn,dep);
	for(int i=0;i<e[u].size();i++){
		int v=e[u][i];
		dfs(v,dep+1);
	}
	out[u]=++dfn,idx[dfn]=u,Splay::addnode(dfn,dep);
}
int main(){
	n=read(),m=read();
	for(int i=1;i<=n;i++){
		int l=read();
		for(int j=1;j<=l;j++)e[i].pb(read());
	}
	dfn++,dfs(1,1),dfn++;
	Splay::build(Splay::rt,1,dfn);
	while(m--){
		int op=read();
		if(op==1){
			int u=read(),v=read();
			cout<<Splay::dis(u,v)<<'\n';
		}
		if(op==2){
			int u=read(),h=read();
			Splay::gofa(u,h);
		}
		if(op==3){
			int k=read();
			cout<<idx[Splay::find(Splay::rt,k+1)]<<'\n';
		}
	}
}
posted @ 2019-08-27 14:18  Stargazer_cykoi  阅读(82)  评论(0编辑  收藏  举报