bzoj2002:[Hnoi2010]Bounce 弹飞绵羊
思路:动态树。把每个装置看成点,能弹到的装置连边,能弹飞的装置连到n+1号点,每个点点权设为1,这样问几次弹飞就可以转化为该点到n+1号点的路径的点权和。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> const int maxn=200010; using namespace std; int n,m,next[maxn];char ch; void read(int &x){ for (ch=getchar();!isdigit(ch);ch=getchar()); for (x=0;isdigit(ch);ch=getchar()) x=x*10+ch-'0'; } struct LCT{ int c[maxn][2],fa[maxn],size[maxn],rev[maxn]; inline void update(int x){size[x]=size[c[x][0]]+size[c[x][1]]+1;} inline bool isroot(int x){return (c[fa[x]][0]!=x)&&(c[fa[x]][1])!=x;} inline int which(int x){return c[fa[x]][1]==x;} inline void flip(int x){rev[x]^=1,swap(c[x][0],c[x][1]);} inline void down(int x){if (rev[x]) flip(c[x][0]),flip(c[x][1]),rev[x]=0;} void relax(int x){if (!isroot(x)) relax(fa[x]);down(x);} void rotate(int x){ int y=fa[x],z=fa[y],nx=which(x),ny=which(y); fa[c[x][!nx]]=y,c[y][nx]=c[x][!nx]; fa[x]=z;if (!isroot(y)) c[z][ny]=x; fa[y]=x,c[x][!nx]=y;update(y); } void splay(int x){ relax(x); while (!isroot(x)){ if (isroot(fa[x])) rotate(x); else if (which(x)==which(fa[x])) rotate(fa[x]),rotate(x); else rotate(x),rotate(x); } update(x); } void access(int x){for (int p=0;x;x=fa[x]) splay(x),fa[c[x][1]=p]=x,update(x),p=x;} void makeroot(int x){access(x),splay(x),flip(x),update(x);} void link(int a,int b){makeroot(a),fa[a]=b;} void cut(int a,int b){makeroot(a),access(b),splay(b),c[b][0]=fa[a]=0;} void query(int a){makeroot(n+1),access(a),splay(a),printf("%d\n",size[c[a][0]]);} void modify(int a,int k){int t=a+k>n?n+1:a+k;cut(a,next[a]),link(a,t),next[a]=t;} }T; int main(){ read(n),T.size[n+1]=1; for (int i=1,a;i<=n;i++) read(a),T.fa[i]=min(i+a,n+1),T.size[i]=1,next[i]=T.fa[i]; read(m); for (int i=1,op,a,k;i<=m;i++){ read(op),read(a),a++; if (op==1) T.query(a); else read(k),T.modify(a,k); } return 0; }