luogu3203 弹飞绵羊 (LCT)

时间:2023-12-23 23:47:19

新建一个N+1的点,飞出去的连到这个上,记size,每次统计x和N+1的链长就可以。

别忘了编号是从0开始的

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<ctime>
#include<set>
#define pa pair<int,int>
#define lowb(x) ((x)&(-(x)))
#define REP(i,n0,n) for(i=n0;i<=n;i++)
#define PER(i,n0,n) for(i=n;i>=n0;i--)
#define MAX(a,b) ((a>b)?a:b)
#define MIN(a,b) ((a<b)?a:b)
#define CLR(a,x) memset(a,x,sizeof(a))
#define rei register int
#define lt ch[x][0]
#define rt ch[x][1]
using namespace std;
const int maxn=;
typedef long long ll; ll rd(){
ll x=;char c=getchar();int neg=;
while(c<''||c>''){if(c=='-') neg=-;c=getchar();}
while(c>=''&&c<='') x=x*+c-'',c=getchar();
return x*neg;
} int N,M;
int fa[maxn],ch[maxn][],siz[maxn],to[maxn];
bool rev[maxn]; inline void update(int x){siz[x]=(lt?siz[lt]:)+(rt?siz[rt]:)+;}
inline void pushrev(int x){
rev[x]^=;swap(lt,rt);
}
inline void pushdown(int x){
if(!rev[x]) return;
if(lt) pushrev(lt);
if(rt) pushrev(rt);
rev[x]=;
}
inline bool isRoot(int x){return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;}
inline bool isRson(int x){return x==ch[fa[x]][];}
inline void rotate(int x){
int f=fa[x],ff=fa[f];bool b=isRson(x);
if(!isRoot(f)) ch[ff][isRson(f)]=x;fa[x]=ff;
if(ch[x][b^]) fa[ch[x][b^]]=f;ch[f][b]=ch[x][b^];
ch[x][b^]=f;fa[f]=x;update(f);update(x);
}
inline void pushdall(int x){
if(!isRoot(x)) pushdall(fa[x]);pushdown(x);
}
inline void splay(int x){
pushdall(x);
while(!isRoot(x)&&!isRoot(fa[x])){
if(isRson(x)==isRson(fa[x])) rotate(fa[x]);
else rotate(x);rotate(x);
}if(!isRoot(x)) rotate(x);
}
inline void access(int x){
for(int y=;x;y=x,x=fa[x]){
splay(x);rt=y;update(x);
}
}
inline void setRoot(int x){
access(x);splay(x);
pushrev(x);
}
inline int getRoot(int x){
access(x);splay(x);
while(lt){
pushdown(x);x=lt;
}return x;
}
inline void link(int x,int y){//x->y
setRoot(x);
access(y);splay(y);fa[x]=y;
}
inline void cut(int x,int y){
setRoot(x);access(y);splay(y);
ch[y][]=fa[x]=;update(y);
}
inline int query(int x,int y){
setRoot(x);
access(y);splay(y);
//printf("%d %d\n",ch[y][0],ch[y][1]);
return siz[y];
}
inline void change(int x,int k){
if(x+k>N&&x+to[x]>N) return;
if(to[x]) cut(x,MIN(x+to[x],N+));
link(x,MIN(x+k,N+));
to[x]=k;
} int main(){
//freopen("3203.in","r",stdin);
rei i,j,k;N=rd();
REP(i,,N) change(i,rd()),siz[i]=;siz[N+]=;
M=rd();REP(i,,M){
j=rd(),k=rd()+;
if(j==) printf("%d\n",query(N+,k)-);
else change(k,rd());
}
return ;
}