您的位置:首页 > 其它

【BZOJ2843】极地旅行社

2016-07-20 20:23 387 查看
题意:连边,询问权值和,单点修改权值

裸的不能再裸的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
[2],father
;
int val
,sum
,Max
;
bool rev
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: