【BZOJ2843】极地旅行社

2 篇文章 0 订阅

题意:连边,询问权值和,单点修改权值

裸的不能再裸的lct模板题。。。话说我就贴了个刚写完的板,改了下输入,输出,数据范围都没看,居然就a了。。。。一点坑点都没有,要不要这样啊。。。

#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 200005
using namespace std;
int n,m;
int read()
{
    int x=0,f=1;
	char c=getchar();
    for (;c<'0'||c>'9';c=getchar())
		if(c=='-') f=-1;
    for (;c>='0'&&c<='9';c=getchar())
		x=x*10+c-'0';
    return x*f;
}
struct LCT
{
	int son[N][2],father[N];
	int val[N],sum[N],Max[N];
	bool rev[N];
	int find(int x)
	{
		access(x);
		splay(x);
		while (son[x][0]) x=son[x][0];
		return x;
	}
	bool isroot(int x)
	{
		return son[father[x]][0]!=x&&son[father[x]][1]!=x;
	}
	void up(int x)
	{
		int l=son[x][0],r=son[x][1];
		sum[x]=val[x]+sum[l]+sum[r];
		Max[x]=max(val[x],max(Max[l],Max[r]));
	}
	void down(int x)
	{	
		if (!x||!rev[x]) return;
		rev[son[x][0]]^=1,rev[son[x][1]]^=1;
		swap(son[x][0],son[x][1]);
		rev[x]=0;
	}
	void pushdown(int x)
	{
		if (!isroot(x)) pushdown(father[x]);
		down(x);
	}
	void rotate(int x)
	{
		int y=father[x],z=father[y],l=(son[y][0]!=x),r=l^1;
		if (!isroot(y)) son[z][son[z][1]==y]=x;
		father[x]=z;
		father[y]=x;
		father[son[x][r]]=y;
		son[y][l]=son[x][r];
		son[x][r]=y;
		up(y);
		up(x);
	}
	void splay(int x)
	{
		for (pushdown(x);!isroot(x);rotate(x))
		{
			int y=father[x],z=father[y];
			if (!isroot(y))
				rotate((x==son[y][0])==(y==son[z][0])?y:x);
		}
	}
	void access(int x)
	{
		for (int t=0;x;t=x,x=father[x])
			splay(x),son[x][1]=t,up(x);
	}
	void makeroot(int x)
	{
		access(x);
		splay(x);
		rev[x]^=1;
	}
	void link(int x,int y)
	{
		makeroot(x);
		father[x]=y;
	}
	void cut(int x,int y)
	{
		makeroot(x);
		access(y);
		splay(y);
		son[y][0]=father[x]=0;
		up(y);
	}
	void mdy(int x,int y)
	{
		splay(x);
		val[x]=y;
		up(x);
	}
	int qry(int x,int y)
	{
		makeroot(x);
		access(y);
		splay(y);
		return sum[y];
	}
}T;
int main()
{
	n=read();
	for (int i=1;i<=n;i++)
		T.val[i]=read(),T.sum[i]=T.Max[i]=T.val[i];
	m=read();
	while (m--)
	{
		char p[10];
		int x,y;
		scanf("%s%d%d",p,&x,&y);
		switch(p[0])
		{
			case 'e':
				if (T.find(x)==T.find(y))
					printf("%d\n",T.qry(x,y));
				else
					puts("impossible");
				break;
			case 'b':
				if (T.find(x)!=T.find(y))
					T.link(x,y),puts("yes");
				else
					puts("no");
				break;
			case 'p':
				T.mdy(x,y);
				break;
		}
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值