【bzoj1180】[CROATIAN2009]OTOCI

          既然bzoj上是权限题就发个题目链接    开了这个还会有人看我的么QAQ

          LCT模板题,他喊你做什么,你就做什么就行辣(这题要么用来预习LCT,要么用来复习LCT)

      直接贴代码(估计点了题目链接的都不会看我的代码辣QAQ

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<queue>
#define N 30005
using namespace std;
int m,n,rev[N];
int c[N][2],sum[N],val[N],au[N],fa[N];
void pushup(int x){
	int l=c[x][0],r=c[x][1];
	sum[x]=sum[l]+sum[r]+val[x];
}
void pushdown(int x){
	int l=c[x][0],r=c[x][1];
	if(rev[x]){
		rev[x]=0;
		rev[l]^=1;rev[r]^=1;
		swap(c[x][0],c[x][1]);
	}
}
void rot(int x){
	int y=fa[x],z=fa[y];
	int l=(x!=c[y][0]),r=l^1;
	if(au[y]){if(y==c[z][0])c[z][0]=x;else c[z][1]=x;}
	else {au[y]=1;au[x]=0;}
	fa[y]=x;fa[x]=z;fa[c[x][r]]=y;
	c[y][l]=c[x][r];c[x][r]=y;
	pushup(y);pushup(x);
}
void splay(int x){
	int y,z;
	pushdown(x);
	while(au[x]){
		y=fa[x];z=fa[y];
		if(au[y])pushdown(z);
		pushdown(y);pushdown(x);
		if(au[y]){if(c[y][0]==x^c[z][0]==y)rot(x);else rot(y);}
		rot(x);
	}
}
void access(int x){
	int y=0;
	while(x){
		splay(x);
		au[c[x][1]]=0;c[x][1]=y;
		fa[y]=x;au[y]=1;
		pushup(x);
		y=x;x=fa[x];
	}
}
void evert(int x){access(x);splay(x);rev[x]^=1;}
void link(int x,int y){evert(y);fa[y]=x;}
void cut(int x,int y){
	evert(x);access(y);splay(y);
	au[c[y][0]]=fa[c[y][0]]=0;c[y][0]=0;
}
int find_rt(int v){
	access(v);splay(v);
	pushdown(v);
	while(c[v][0]){v=c[v][0];pushdown(v);}
	return v;
}
char ch[15];
int main(){
    int i,j,k,x,y;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
    	scanf("%d",&val[i]);
    	sum[i]=val[i];
	}
	scanf("%d",&m);
	for(i=1;i<=m;i++){
		scanf("%s",ch);
		scanf("%d%d",&x,&y);
		if(ch[0]=='b'){
			if(find_rt(x)!=find_rt(y)){
				puts("yes");
				link(x,y);
			}
			else puts("no");
		}
		if(ch[0]=='p'){
                        evert(x);
                        val[x]=y;
                        pushup(x);
		}
		if(ch[0]=='e'){
			if(find_rt(x)!=find_rt(y))puts("impossible");
			else {
				evert(x);access(y);splay(y);
				printf("%d\n",sum[y]);
			}
		}
	}
    return 0;
}
水题竟然没有1A,不开心。

你可能感兴趣的:(题解,LCT)