题目大意
有n个位置排列在一条直线上,编号从
每个位置有一个值
现在有
题目分析
如果我们从i连向
对于弹一次会弹出去的点,我们让它们都连向n+1,这样就可以方便我们处理询问。
时间复杂度O(mlogn)。
代码实现
#include <algorithm>
#include <iostream>
#include <cctype>
#include <cstdio>
#include <stack>
using namespace std;
int read()
{
int x=0,f=1;
char ch=getchar();
while (!isdigit(ch)) f=ch=='-'?-1:f,ch=getchar();
while (isdigit(ch)) x=x*10+ch-'0',ch=getchar();
return x*f;
}
int buf[30];
void write(int x)
{
if (x<0) putchar('-');
for (;x;x/=10) buf[++buf[0]]=x%10;
if (!buf[0]) buf[++buf[0]]=0;
for (;buf[0];putchar(buf[buf[0]--]+'0'));
}
const int N=200005;
int k[N];
int n,q;
struct link_cut_tree
{
int par[N],fa[N],size[N];
int son[N][2];
stack<int> st;
bool mark[N];
bool side(int x){return son[fa[x]][1]==x;}
void update(int x){size[x]=size[son[x][0]]+size[son[x][1]]+1;}
void R(int x){swap(son[x][0],son[x][1]),mark[x]^=1;}
void clear(int x)
{
if (mark[x])
{
if (son[x][0]) R(son[x][0]);
if (son[x][1]) R(son[x][1]);
mark[x]=0;
}
}
void pushdown(int x,int y)
{
for (;x!=y;st.push(x),x=fa[x]);
for (;!st.empty();clear(st.top()),st.pop());
}
void rotate(int x)
{
int y=fa[x];bool s=side(x);
if (fa[y]) son[fa[y]][side(y)]=x;
if (son[x][s^1]) fa[son[x][s^1]]=y;
son[y][s]=son[x][s^1],son[x][s^1]=y;
fa[x]=fa[y],fa[y]=x;
if (par[y]) par[x]=par[y],par[y]=0;
update(y),update(x);
}
void splay(int x,int y)
{
for (pushdown(x,y);fa[x]!=y;rotate(x))
if (fa[fa[x]]!=y)
if (side(x)==side(fa[x])) rotate(fa[x]);
else rotate(x);
}
int access(int x)
{
int nxt=0;
for (;x;update(nxt=x),x=par[x])
{
splay(x,0);
if (son[x][1]) par[son[x][1]]=x,fa[son[x][1]]=0;
if (nxt) par[nxt]=0,fa[nxt]=x;
son[x][1]=nxt;
}
return nxt;
}
void makeroot(int x){R(access(x));}
void link(int x,int y)
{
makeroot(x),access(x),splay(x,0);
par[x]=y,access(y);
}
void cut(int x,int y)
{
makeroot(x),access(y),splay(y,0);
fa[x]=son[y][0]=par[y]=0,update(y);
}
int query(int x)
{
makeroot(n+1),access(x),splay(x,0);
return size[x];
}
}lct;
void init()
{
for (int i=1;i<=n+1;++i) lct.size[i]=1;
for (int i=1;i<=n;++i) lct.link(i,i+k[i]<=n?i+k[i]:n+1);
}
int main()
{
freopen("bounce.in","r",stdin),freopen("bounce.out","w",stdout);
n=read();
for (int i=1;i<=n;++i) k[i]=read();
init();
for (q=read();q--;)
{
int op=read(),x=read()+1,y;
if (op==1) write(lct.query(x)-1),putchar('\n');
else
{
lct.cut(x,x+k[x]<=n?x+k[x]:n+1);
k[x]=read();
lct.link(x,x+k[x]<=n?x+k[x]:n+1);
}
}
fclose(stdin),fclose(stdout);
return 0;
}