其他分享
首页 > 其他分享> > bzoj 2002 [Hnoi2010]Bounce 弹飞绵羊 lct

bzoj 2002 [Hnoi2010]Bounce 弹飞绵羊 lct

作者:互联网

题意:告诉你每个装置的弹飞到哪个装置,问你从某个起点出发可以被弹飞多少次?(可修改)

解:LCT模板(link and cut  and ask (size))
 

#include<bits/stdc++.h>
#define mod 51061
#define en '\n'
#define ll unsigned int
const int maxn = 2e5 +100;
using namespace std;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,top,cnt,a[maxn];
int c[maxn][2],fa[maxn];
int size[maxn],q[maxn];
bool rev[maxn];
//int sum[maxn];
int sum[maxn],val[maxn],at[maxn],mt[maxn];
void cal(int x,int m,int a)
{
	if(!x)return;
	val[x]=(val[x]*m+a)%mod;
	sum[x]=(sum[x]*m+a*size[x])%mod;
	at[x]=(at[x]*m+a)%mod;
	mt[x]=(mt[x]*m)%mod;
}
bool isroot(int x)
{
	return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
}
void update(int x)
{
	int l=c[x][0],r=c[x][1];
	//sum[x]=(sum[l]+sum[r]+val[x])%mod;
	size[x]=(size[l]+size[r]+1);
}
void pushdown(int x)
{
	int l=c[x][0],r=c[x][1];
	if(rev[x])
	{
		rev[x]^=1;rev[l]^=1;rev[r]^=1;
		swap(c[x][0],c[x][1]);
	}
	int m=mt[x],a=at[x];
	mt[x]=1;at[x]=0;
	if(m!=1||a!=0)
	{
		cal(l,m,a);cal(r,m,a);
	}
}
void rotate(int x)
{
	int y=fa[x],z=fa[y],l,r;
	l=(c[y][1]==x);r=l^1;
	if(!isroot(y))c[z][c[z][1]==y]=x;
	fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
	c[y][l]=c[x][r];c[x][r]=y;
	update(y);update(x);
}
void splay(int x)
{
	q[++top]=x;
	for(int i=x;!isroot(i);i=fa[i])
		q[++top]=fa[i];
	while(top)pushdown(q[top--]);
	while(!isroot(x))
	{
		int y=fa[x],z=fa[y];
		if(!isroot(y))
		{
			if(c[y][0]==x^c[z][0]==y)rotate(x);
			else rotate(y);
		}
		rotate(x);
	}
}
void access(int x)
{
	for(int t=0;x;t=x,x=fa[x])
	{
		splay(x);c[x][1]=t;update(x);
	}
}
void makeroot(int x)
{
	access(x);splay(x);rev[x]^=1;
}
void split(int x,int y)
{
	makeroot(y);access(x);splay(x);
}
void link(int x,int y)
{
	makeroot(x);fa[x]=y;
}
void cut(int x,int y)
{
	makeroot(x);access(y);splay(y);c[y][0]=fa[x]=0;
}
int main()
{
    #ifdef local
    freopen("input2.txt","r",stdin);
    #endif // local

	n=read();
    for(int i=1;i<=n;i++){
        a[i]=read();
        link(i, min(n+1,i+a[i]));
    } m=read();
    while(m--){
        int f=read();
        if(f==1){
            int tem=1+read();
            split(tem,n+1);
            cout<<size[tem]-1<<en;
        }else{
            int tem=read()+1;
            cut(tem,min(n+1,tem+a[tem]));
            a[tem]=read();
            link(tem, min(n+1,tem+a[tem]));
        }
    }
	return 0;
}

 

标签:lct,ch,int,Bounce,while,弹飞,getchar,define
来源: https://blog.csdn.net/qq_40675883/article/details/100551943