luoguP3979 遥远的国度 LCT+multiset维护子树信息
Code:
#include<bits/stdc++.h>
#define maxn 150000
#define ll long long
#define inf 2147483647000
using namespace std;
#define getset chil[x].empty()?inf:*chil[x].begin()
void setIO(string s)
{
string in=s+".in", out=s+".out";
freopen(in.c_str(),"r",stdin);
// freopen(out.c_str(),"w",stdout);
}
multiset<ll>chil[maxn];
ll value[maxn],mintot[maxn],mins[maxn],minv[maxn];
namespace tree
{
#define lson ch[x][0]
#define rson ch[x][1]
int ch[maxn][2], f[maxn], rev[maxn], sta[maxn];
ll tag[maxn];
int get(int x)
{
return ch[f[x]][1]==x;
}
int isroot(int x)
{
return (1^(ch[f[x]][1]==x||ch[f[x]][0]==x));
}
void markrev(int x)
{
if(!x) return;
swap(lson,rson),rev[x]^=1;
}
void pushup(int x)
{
if(!x) return;
minv[x]=mintot[x]=value[x], mins[x]=inf;
minv[x]=min(minv[x], min(minv[lson], minv[rson]));
mins[x]=min(min(mins[lson], mins[rson]), getset);
mintot[x]=min(minv[x], mins[x]);
}
void marktag(int x,ll delta)
{
if(!x) return;
value[x]=tag[x]=minv[x]=delta;
mintot[x]=min(min(mins[lson], mins[rson]),getset);
}
void pushdown(int x)
{
if(!x) return;
if(tag[x])
{
marktag(lson,tag[x]);
marktag(rson,tag[x]);
tag[x]=0;
}
if(rev[x])
{
markrev(lson);
markrev(rson);
rev[x]^=1;
}
}
void rotate(int x)
{
int old=f[x],fold=f[old],which=get(x);
if(!isroot(old)) ch[fold][ch[fold][1]==old]=x;
ch[old][which]=ch[x][which^1],f[ch[old][which]]=old;
ch[x][which^1]=old,f[old]=x,f[x]=fold;
pushup(old),pushup(x);
}
void splay(int x)
{
int u=x,v=0,fa;
sta[++v]=u;
while(!isroot(u)) sta[++v]=f[u],u=f[u];
while(v) pushdown(sta[v--]);
for(u=f[u];(fa=f[x])!=u;rotate(x))
if(f[fa]!=u)
rotate(get(fa)==get(x)?fa:x);
}
void Access(int x)
{
for(int t = 0 ; x ; t = x, x = f[x])
{
splay(x);
if(rson) chil[x].insert(mintot[rson]);
if(t) chil[x].erase(chil[x].lower_bound(mintot[t]));
rson=t;
pushup(x);
}
}
void MakeRoot(int x)
{
Access(x),splay(x), markrev(x);
}
void split(int x,int y)
{
MakeRoot(x),Access(y),splay(y);
}
void link(int x,int y)
{
MakeRoot(x), pushdown(x), pushup(x), f[x]=y, chil[y].insert(mintot[x]), pushup(y);
}
};
int n,Q,edges,root;
int from[maxn],to[maxn<<1],hd[maxn],nex[maxn<<1];
int main()
{
// setIO("input");
scanf("%d%d",&n,&Q);
for(int i=1,u,v;i<n;++i)
{
scanf("%d%d",&u,&v);
from[++edges]=u, to[edges]=v;
}
minv[0]=mins[0]=mintot[0]=value[0]=inf;
chil[0].insert(inf);
for(int i=1;i<=n;++i) scanf("%lld",&value[i]),mintot[i]=minv[i]=value[i], mins[i]=inf;
for(int i=1;i<=edges;++i) tree::link(from[i],to[i]);
scanf("%d",&root);
tree::MakeRoot(root);
int opt,a,b,c,x;
ll xx;
while(Q--)
{
scanf("%d",&opt);
switch(opt)
{
case 1 :
{
scanf("%d",&a);
tree::MakeRoot(root=a);
break;
}
case 2 :
{
scanf("%d%d%lld",&a,&b,&xx);
tree::split(a,b);
tree::marktag(b,xx);
tree::MakeRoot(root);
break;
}
case 3 :
{
scanf("%d",&x);
tree::Access(x);
tree::splay(x);
printf("%lld\n",min(value[x],getset));
break;
}
}
}
return 0;
}