bzoj2843&&1180

时间:2023-03-10 03:55:08
bzoj2843&&1180

题解:

lct

和上一题差不多

这一题还要判断是否有链接

其实直接并查集判断就可以了

代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
const int N=;
using namespace std;
int read()
{
int x=;char ch=getchar();
for (;ch<''||ch>'';ch=getchar());
for (;ch>=''&&ch<='';ch=getchar())x=x*+ch-'';
return x;
}
int n,m,top,cnt,c[N][],fa[N],size[N],q[N],rev[N],f[N],sum[N],val[N],at[N],mt[N];
int isroot(int x){return c[fa[x]][]!=x&&c[fa[x]][]!=x;}
void update(int x)
{
int l=c[x][],r=c[x][];
sum[x]=sum[l]+sum[r]+val[x];
size[x]=size[l]+size[r]+;
}
void pushdown(int x)
{
int l=c[x][],r=c[x][];
if (rev[x])
{
rev[x]^=;rev[l]^=;rev[r]^=;
swap(c[x][],c[x][]);
}
}
void rotate(int x)
{
int y=fa[x],z=fa[y],l,r;
l=(c[y][]==x);r=l^;
if (!isroot(y))c[z][c[z][]==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 down(int x){if (!isroot(x))down(fa[x]);pushdown(x);}
void splay(int x)
{
down(x);
for (int y=fa[x];!isroot(x);rotate(x),y=fa[x])
if (!isroot(y))rotate((c[y][]==x)==(c[fa[y]][]==y)?y:x);
}
void access(int x)
{
for(int t=;x;t=x,x=fa[x])
{
splay(x);
c[x][]=t;
update(x);
}
}
void makeroot(int x){access(x);splay(x);rev[x]^=;}
void split(int x,int y){makeroot(y);access(x);splay(x);}
void link(int x,int y){makeroot(x);fa[x]=y;}
int find(int x){if (x==f[x])return f[x];return f[x]=find(f[x]);}
int main()
{
n=read();
for (int i=;i<=n;i++)val[i]=sum[i]=read(),f[i]=i,size[i]=;
m=read();char ch[];
while(m--)
{
scanf("%s",ch);
int u=read(),v=read();
if (ch[]=='b')
{
if (f[find(u)]==f[find(v)])puts("no");
else puts("yes"),link(u,v),f[find(u)]=f[find(v)];
}
if (ch[]=='p')
{
val[u]=v;
makeroot(u);
}
if (ch[]=='e')
{
if (f[find(u)]!=f[find(v)])
{
puts("impossible");
continue;
}
split(u,v);
printf("%d\n",sum[u]);
}
}
return ;
}