【BZOJ】【2002】【HNOI2010】弹飞绵羊

时间:2023-03-09 17:44:22
【BZOJ】【2002】【HNOI2010】弹飞绵羊

呃这题的Hint写着splay启发式合并……但是蒟蒻不懂T_T只好写个简单的LCT来蒙混过关,就是时间效率上差劲的很……

不过能够一次AC心情也是蛮愉悦的~

 /**************************************************************
Problem: 2002
User: Tunix
Language: C++
Result: Accepted
Time:1932 ms
Memory:6156 kb
****************************************************************/ //BZOJ 2002
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
using namespace std;
const int N=; int c[N][],fa[N],size[N],n,m,k[N],st[N],top=;
bool rev[N]; int read(){
char ch=getchar();
int sig=,tmp=;
while(ch<''||ch>'') {if (ch=='-') sig=-;ch=getchar();}
while(ch>='' && ch<='') {tmp=*tmp+ch-'';ch=getchar();}
return tmp*sig;
} void Push_down(int x){
if (rev[x]){
rev[x]^=; rev[c[x][]]^=; rev[c[x][]]^=;
swap(c[x][],c[x][]);
}
}
inline void Push_up(int x){
size[x]=size[c[x][]]+size[c[x][]]+;
}
bool isroot(int x){
return c[fa[x]][]!=x && c[fa[x]][]!=x;
}
void rotate(int x){
int y=fa[x],z=fa[y],l=c[y][]==x,r=l^;
if (!isroot(y)) c[z][c[z][]==y]=x;
fa[x]=z; fa[y]=x; fa[c[x][r]]=y;
c[y][l]=c[x][r]; c[x][r]=y;
Push_up(y); Push_up(x);
}
void splay(int x){
top=; st[top++]=x;
for(int i=x;!isroot(i);i=fa[i])
st[top++]=fa[i];
while(top--) Push_down(st[top]); while(!isroot(x)){
int y=fa[x],z=fa[y];
if (!isroot(y)){
if (c[y][]==x^c[z][]==y) rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x){
for(int t=;x;t=x,x=fa[x])
splay(x),c[x][]=t,Push_up(x);
}
void makeroot(int x){
access(x); splay(x); rev[x]^=;
}
int find(int x){
access(x); splay(x);
while(c[x][]) x=c[x][];
return x;
}
void cut(int x,int y){
makeroot(x); access(y); splay(y);
if (c[y][]==x) c[y][]=fa[x]=;
}
void link(int x,int y){
makeroot(x); fa[x]=y;
}
//LCT end
int main(){
// freopen("input.txt","r",stdin);
n=read();
F(i,,n){
k[i]=read();
link(i,min(i+k[i],n+));
}
m=read();
int x,y,z;
F(i,,m){
x=read(); y=read(); y++;
if (x==) {
makeroot(n+);
access(y);splay(y);
printf("%d\n",size[y]-);
}
else if(x==){
z=read();
cut(y,min(y+k[y],n+));
k[y]=z;
link(y,min(y+k[y],n+));
}
}
return ;
}