来个代码
#include<bits/stdc++.h> #define O3 __attribute__((optimize("-O3"))) #define O2 __attribute__((optimize("-O2"))) #define setIO(s) freopen(s".in","r",stdin) , freopen(s".out","w",stdout) #define maxn 100002 #define rint register int #define rg register #define inf 100000000 #define isrt(x) (!(ch[f[x]][0]==x||ch[f[x]][1]==x)) #define get(x) (ch[f[x]][1]==x) #define lson ch[x][0] #define rson ch[x][1] using namespace std; inline int read(){ rint x=0,f=1;char ch=getchar(); while (ch<'0' || ch>'9'){if (ch=='-')f=-1;ch=getchar();} while ('0'<=ch && ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); return x*f; } int n,Q; multiset<int>S[maxn]; int ch[maxn][2],f[maxn],rev[maxn],sta[maxn],minv[maxn],mintot[maxn],minson[maxn],val[maxn]; O2 inline void mark(int x) { if(!x) return; swap(lson,rson),rev[x]^=1; } O2 inline void pushup(int x) { if(!x) return; minv[x]=mintot[x]=val[x],minson[x]=inf; minv[x]=min(minv[x],min(minv[lson],minv[rson])); minson[x]=min(min(minson[lson],minson[rson]),*S[x].begin()); mintot[x]=min(minv[x], minson[x]); } O2 inline void pushdown(int x) { if(!x||!rev[x]) return; mark(lson),mark(rson),rev[x]^=1; } O2 inline void rotate(int x) { int old=f[x],fold=f[old],which=get(x); if(!isrt(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); } O2 inline void splay(int x) { int v=0,u=x,fa; for(sta[++v]=u;!isrt(u);u=f[u]) sta[++v]=f[u]; while(v) pushdown(sta[v--]); for(u=f[u];(fa=f[x])!=u;rotate(x)) if(f[fa]!=u) rotate(get(x)==get(fa)?fa:x); } O2 inline void Access(int x) { int t=0; while(x) { splay(x); if(rson) S[x].insert(mintot[rson]); if(t) S[x].erase(S[x].lower_bound(mintot[t])); rson=t,t=x,x=f[x]; } } O2 inline void makert(int x) { Access(x),splay(x),mark(x); } O2 inline void link(int x,int y) { makert(x), f[x]=y, S[y].insert(mintot[x]); } int edges,hd[maxn],to[maxn],nex[maxn]; void addedge(int u,int v) { if(!u)return; nex[++edges]=hd[u],hd[u]=edges,to[edges]=v; } O2 void dfs(int u) { for(int i=hd[u];i;i=nex[i]) { int v=to[i]; dfs(v), S[u].insert(mintot[v]); pushup(u); } pushup(u); } O2 int main() { rg int i; int root=0; // setIO("input"); n=read(),Q=read(); mintot[0]=minv[0]=minson[0]=val[0]=inf; for(i=0;i<=n;++i) S[i].insert(inf); for(i=1;i<=n;++i) { int a; a=read(), val[i]=read(), f[i]=a; addedge(a, i); if(!a) root=i; } dfs(root); makert(root); while(Q--) { char opt[3]; int x,y; scanf("%s",opt); if(opt[0]=='V') { x=read(), y = read(); Access(x), splay(x), val[x]=y, pushup(x); } if(opt[0]=='E') { root = read(); makert(root); } if(opt[0]=='Q') { x = read(); Access(x), splay(x); printf("%d\n",min(val[x], *S[x].begin())); } } return 0; }
注意几点:
1. bzoj 交题一定加读入优化/输出优化
2. LCT 的操作都非常慢,能不用就不用
3. 加 register/O2/O3
4. inline 好像用处不大