BZOJ 1180: [CROATIAN2009]OTOCI

7 篇文章 0 订阅

没什么好说的,就是一道模板题。

只是象征性的留个代码.....

#include<map>
#include<cmath>
#include<queue>
#include<string>
#include<cstdio>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define inf 1000000000
#define me(a,x) memset(a,x,sizeof a)
#define cp(a,x) memcpy(a,x,sizeof a)
#define N 30010
using namespace std;
typedef long long LL;
int fa[N],n,m,c[N][2],st[N],w[N],sum[N];
bool rev[N];
bool Rt(int x)
{
	if(c[fa[x]][0]==x || c[fa[x]][1]==x)return 0;
	return 1;
}
void pushup(int x)
{
	int l=c[x][0],r=c[x][1];
	sum[x]=sum[l]+sum[r]+w[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 rotate(int x)
{
	int y=fa[x],z=fa[y],a=c[y][1]==x,b=c[z][1]==y,g=c[x][!a];
	if(!Rt(y))c[z][b]=x;
	fa[g]=y,c[y][a]=g;
	fa[y]=x,c[x][!a]=y;
	fa[x]=z;
	pushup(y); pushup(x);
}
void splay(int x)
{
	int top=0,i;
	for(i=x;!Rt(i);i=fa[i])st[++top]=i;
	st[++top]=i;
	for(i=top;i;i--)pushdown(st[i]);
	while(!Rt(x))
	{
		int y=fa[x],z=fa[y],a=c[y][1]==x,b=c[z][1]==y;
		if(!Rt(y))
		{
			if(a==b)rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
}
void access(int x)
{
	int last=0;
	while(x)
	{
		splay(x);
		c[x][1]=last;
		pushup(x);
		last=x,x=fa[x];
	}
}
void make_root(int x)
{
	access(x); splay(x);
	rev[x]^=1;
}
void split(int x,int y)
{
	make_root(x);
	access(y); splay(y);
}
void link(int x,int y)
{
	make_root(x); fa[x]=y;
}
void cut(int x,int y)
{
	split(x,y);
	fa[x]=c[y][0]=0;
}
int findrt(int x)
{
	access(x); splay(x);
	while(c[x][0])x=c[x][0];
	return x;
}
int main()
{
	int i,x,y,p;
	char ch[10];
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&w[i]);
		sum[i]=w[i];
	}
	scanf("%d",&m);
	while(m--)
	{
		scanf("%s%d%d",ch,&x,&y);
		if(ch[0]=='b')
		{
			if(findrt(x)==findrt(y))printf("no\n");
			else
			{
				printf("yes\n"); link(x,y);
			}
		}
		if(ch[0]=='p')
		{
			splay(x); w[x]=y; pushup(x);
		}
		if(ch[0]=='e')
		{
			if(findrt(x)!=findrt(y))printf("impossible\n");
			else split(x,y),printf("%d\n",sum[y]);
		}
	}
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值