bzoj2002 [Hnoi2010]Bounce 弹飞绵羊

题目链接

link cut tree

比bzoj2049多维护一个size

注意查询之前要把n+1reverse一下,保证答案正确性

  1 #include<algorithm>
  2 #include<iostream>
  3 #include<cstdlib>
  4 #include<cstring>
  5 #include<cstdio>
  6 #include<string>
  7 #include<cmath>
  8 #include<ctime>
  9 #include<queue>
 10 #include<stack>
 11 #include<map>
 12 #include<set>
 13 #define rre(i,r,l) for(int i=(r);i>=(l);i--)
 14 #define re(i,l,r) for(int i=(l);i<=(r);i++)
 15 #define Clear(a,b) memset(a,b,sizeof(a))
 16 #define inout(x) printf("%d",(x))
 17 #define douin(x) scanf("%lf",&x)
 18 #define strin(x) scanf("%s",(x))
 19 #define LLin(x) scanf("%lld",&x)
 20 #define op operator
 21 #define CSC main
 22 typedef unsigned long long ULL;
 23 typedef const int cint;
 24 typedef long long LL;
 25 using namespace std;
 26 void inin(int &ret)
 27 {
 28     ret=0;int f=0;char ch=getchar();
 29     while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}
 30     while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar();
 31     ret=f?-ret:ret;
 32 }
 33 int ch[200020][2],fa[200020],s[200020],rev[200020];
 34 bool isroot(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
 35 void maintain(int x){if(x)s[x]=1+s[ch[x][0]]+s[ch[x][1]];}
 36 void down(int x)
 37 {
 38     if(rev[x])
 39     {
 40         swap(ch[x][0],ch[x][1]);
 41         rev[ch[x][0]]^=1;
 42         rev[ch[x][1]]^=1;
 43         rev[x]=0;
 44     }
 45 }
 46 void rotate(int x)
 47 {
 48     int y=fa[x],z=fa[y];
 49     if(!isroot(y))ch[z][ch[z][1]==y]=x;
 50     fa[x]=z,fa[y]=x;
 51     int d=ch[y][1]==x;
 52     fa[ch[x][d^1]]=y;
 53     ch[y][d]=ch[x][d^1];
 54     ch[x][d^1]=y;
 55     maintain(y),maintain(x);
 56 }
 57 int sta[200020],top;
 58 void splay(int x)
 59 {
 60     top=0;sta[++top]=x;int xx=x;
 61     while(!isroot(xx))sta[++top]=fa[xx],xx=fa[xx];
 62     while(top)down(sta[top--]);
 63     while(!isroot(x))
 64     {
 65         int y=fa[x],z=fa[y];
 66         if(!isroot(y))
 67             if((ch[y][1]==x)^(ch[z][1]==y))rotate(x);
 68             else rotate(y);else ;
 69         rotate(x);
 70     }
 71 }
 72 void access(int x)
 73 {
 74     int temp=0;
 75     while(x)
 76     {
 77         splay(x);
 78         ch[x][1]=temp;
 79         temp=x,x=fa[x];
 80     }
 81 }
 82 void reverse(int x)
 83 {
 84     access(x),splay(x),rev[x]^=1;
 85 }
 86 void link(int x,int y)
 87 {
 88     reverse(x),fa[x]=y,splay(x);
 89 }
 90 void cut(int x,int y)
 91 {
 92     reverse(x),access(y),splay(y),ch[y][0]=fa[x]=0;
 93 }
 94 int find(int x)
 95 {
 96     access(x),splay(x);
 97     int y=x;
 98     while(ch[y][0])y=ch[y][0];
 99     return y;
100 }
101 int n,m,x,a[200020];
102 int main()
103 {
104     inin(n);re(i,1,n+1)s[i]=1;
105     re(i,1,n)
106     {
107         inin(a[i]);a[i]=min(a[i]+i,n+1);
108         link(i,a[i]);
109     }
110     inin(m);
111     re(i,1,m)
112     {
113         int opt,q,w;
114         inin(opt);
115         if(opt==1)
116         {
117             reverse(n+1);
118             inin(q);q++;
119             access(q),splay(q);
120             printf("%d\n",s[ch[q][0]]);
121         }
122         else 
123         {
124             inin(q),inin(w);q++;
125             cut(q,a[q]);a[q]=min(w+q,n+1);
126             link(q,a[q]);
127         }
128     }
129      return 0;
130 }

 

你可能感兴趣的:(bzoj2002 [Hnoi2010]Bounce 弹飞绵羊)