p3203 弹飞绵羊

传送门

分析

基本的lct操作,建一个点N表示弹飞出去的点,每次输出N的左子树的大小即可

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define N n+1
#define rep for(int y=0;x;y=x,x=fa[x])
int a[200100],son[200100][2],siz[200100],r[200100],fa[200100],x[200100];
inline void up(int x){siz[x]=siz[son[x][0]]+siz[son[x][1]]+1;}
inline void rev(int x){swap(son[x][0],son[x][1]);r[x]^=1;}
inline void pd(int x){
    if(r[x]){
      if(son[x][0])rev(son[x][0]);
      if(son[x][1])rev(son[x][1]);
      r[x]=0;
    }
}
inline bool notroot(int x){return son[fa[x]][0]==x||son[fa[x]][1]==x;}
inline void push_all(int x){if(notroot(x))push_all(fa[x]);pd(x);}
inline int gs(int x){return son[fa[x]][1]==x;}
inline void rot(int x){
    int y=fa[x],z=fa[y],b=gs(x),c=gs(y),d=son[x][!b];
    if(notroot(y))son[z][c]=x;fa[x]=z;if(d)fa[d]=y;
    son[y][b]=d;fa[y]=x;son[x][!b]=y;up(y),up(x);
}
inline void splay(int x){
    push_all(x);
    while(notroot(x)){
      int y=fa[x],z=fa[y];
      if(notroot(y)){
          if(gs(x)==gs(y))rot(y);
            else rot(x);
      }
      rot(x);
    }
}
inline void access(int x){rep splay(x),son[x][1]=y,up(x);}
inline void makeroot(int x){access(x);splay(x);rev(x);}
inline void spt(int x,int y){makeroot(x);access(y);splay(y);}
inline void link(int x,int y){makeroot(x);fa[x]=y;}
inline void cut(int x,int y){
    makeroot(x);access(y);splay(y);
    fa[x]=son[y][0]=0;
    up(y);
}
int main(){
    int n,m,i,j,k,t;
    scanf("%d",&n);
    for(i=1;i<=n;i++)siz[i]=1;
    for(i=1;i<=n;i++){
      scanf("%d",&x[i]);
      if(i+x[i]<=n)link(i,i+x[i]);
        else link(i,N);
    }
    scanf("%d",&m);
    for(i=1;i<=m;i++){
      scanf("%d%d",&k,&t);t++;
      if(k==1)spt(t,N),printf("%d\n",siz[son[N][0]]);
        else cut(t,min(N,t+x[t])),scanf("%d",&x[t]),link(t,min(N,t+x[t]));
    }
    return 0;
}
posted @ 2018-12-02 09:55  水题收割者  阅读(181)  评论(0编辑  收藏  举报