分析
LCT板子题
而且只需要link,cut这两个基本操作就可以了
对于当前点 i 能到达的点 j,我们就连边,如果有修改就先删除原来的连边,然后再link两个点
顺便记录一下sze,最后直接输出答案即可
码程序时手滑,调试一上午,rotate又写挂了,沮丧%>_<%
唯一可能需要注意一下的就是虽然题目说的是0~ n-1,但由于我们0号节点在splay中作为了空节点,就换成从 1 开始存
代码
#include<bits/stdc++.h>
#define N 200009
#define in read()
using namespace std;
inline int read(){
char ch;int f=1,res=0;
while((ch=getchar())<'0'||ch>'9') if(ch=='-') f=-1;
while(ch>='0'&&ch<='9'){
res=(res<<3)+(res<<1)+ch-'0';
ch=getchar();
}
return f==1?res:-res;
}
int n,m,end[N],fa[N],ch[N][2],sze[N],num[N],lzy[N];
int qr=0;
bool which(int x) { return x==ch[fa[x]][1];}
void pushup(int x) {sze[x]=sze[ch[x][0]]+sze[ch[x][1]]+(x!=n+1);}
int isroot(int x){
if(!fa[x]) return 1;
return (ch[fa[x]][0]!=x)&&(ch[fa[x]][1]!=x);
}
void rotate(int x){
int y=fa[x],z=fa[y],d=which(x),d1=which(y);
if(z&&!isroot(y)) ch[z][d1]=x;
fa[y]=x;fa[x]=z;
ch[y][d]=ch[x][d^1];ch[x][d^1]=y;
if(ch[y][d]) fa[ch[y][d]]=y;//
pushup(y);pushup(x);//update
return;
}
void pushdown(int x){
if(lzy[x]){
swap(ch[x][0],ch[x][1]);
if(ch[x][0])lzy[ch[x][0]]^=1;
if(ch[x][1])lzy[ch[x][1]]^=1;
lzy[x]=0;
}
}
int que[N];
void splay(int x){
que[qr=0]=x;
for(int i=x;!isroot(i);i=fa[i])que[++qr]=fa[i];
for(int i=qr;i>=0;--i) pushdown(que[i]);
while(!isroot(x)){
if(!isroot(fa[x])){
if(which(x)==which(fa[x])) rotate(fa[x]);
else rotate(x);
}
rotate(x);
}
}
void access(int x){
for(int y=0;x;y=x,x=fa[x]){
splay(x);ch[x][1]=y;pushup(x);
}
}
void makeroot(int x){
access(x);
splay(x);
lzy[x]^=1,pushdown(x);return;
}
void link(int x,int y){
makeroot(x);fa[x]=y;
return;
}
void cut(int x,int y){
makeroot(x);access(y);splay(y);
fa[x]=0;ch[y][0]=0;pushup(y);return;
}
int main(){
n=in;int i,j,k;
for(i=1;i<=n+1;++i)sze[i]=1;
for(i=1;i<=n;++i){
k=in;end[i]=k;
fa[i]=min(i+k,n+1);
}
m=in;
while(m--){
i=in;j=in;j++;
if(i==1){
makeroot(n+1);
access(j);
splay(j);
printf("%d\n",sze[j]);
}
else{
k=in;
cut(j,min(j+end[j],n+1));
link(j,min(j+k,n+1));
end[j]=k;
}
}
return 0;
}
然后听说这道题还可以用分块做。ldx大佬毕竟很强