VIII.[BZOJ2959]长跑
我想把出这么毒瘤的题的人拖出来揍一顿
这题稍微想想,就是动态维护点双连通分量并缩点,然后在缩出来的树上求两点距离。
思想简单但代码极其复杂。
首先,我们可以使用冰茶姬维护点双,所有的点全都并到一个冰茶姬中,除了冰茶姬的象征节点,其它的点一律全都删去,不保留任何信息。这样的话,比如说在\(rotate\)和\(splay\)时,不能直接跳到父亲,而是必须跳到父亲的冰茶姬的象征节点。准确地说,代码中所有的节点变量,都最好习惯性地在冰茶姬里面\(find\)一下,虽然码量大了点,常数大了点,但是十分靠谱。
当我们连一条边时:
如果两个端点在不同的连通块中,LCT中直接\(link\)。
否则,将两个端点之间的路径\(split\)出来,然后把这里面所有东西打包到一个冰茶姬中(因为这肯定构成一个点双)。放一下打包的\(shrink\)函数:
(\(merge\):冰茶姬合并操作)
inline void shrink(int x,int y){
merge(x,y);
if(x!=y)t[y].val+=t[x].val,t[x].fa=0;
pushdown(x);
if(lson)shrink(lson,y);
if(rson)shrink(rson,y);
lson=rson=0;
}
注意到我们代码中实际调用这个函数的格式是\(shrink(y,y)\),并且\(y\)是\(split\)出的splay的根。因此,必须要特判\(x\neq y\),不然就会产生奇奇怪怪的错误。
另外,这个出题人居然非常毒瘤的卡了常(或者只是我代码常数过大),不得不\(inline,register\)和快读一股脑全上才卡过去。
代码(LCT真的最好压个行,不然代码显得太长太长了):
#include<bits/stdc++.h>
using namespace std;
#define lson t[x].ch[0]
#define rson t[x].ch[1]
int n,m,dsu[151000],val[151000];
int read(){
int x=0;
char c=getchar();
while(c>'9'||c<'0')c=getchar();
while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();
return x;
}
inline int find(int x){
return dsu[x]==x?x:dsu[x]=find(dsu[x]);
}
inline void merge(int x,int y){
x=find(x),y=find(y);
if(x!=y)dsu[x]=y;
}
struct LCT{
int ch[2],fa,val,sum;
bool rev;
}t[151000];
inline int identify(int x){
if(t[find(t[x].fa)].ch[0]==x)return 0;
if(t[find(t[x].fa)].ch[1]==x)return 1;
return -1;
}
inline void REV(int x){
t[x].rev^=1,swap(lson,rson);
}
inline void pushup(int x){
t[x].sum=t[x].val;
if(lson)t[x].sum+=t[lson].sum;
if(rson)t[x].sum+=t[rson].sum;
}
inline void pushdown(int x){
if(!t[x].rev)return;
if(lson)REV(lson);
if(rson)REV(rson);
t[x].rev=false;
}
inline void rotate(int x){
register int y=find(t[x].fa);
register int z=find(t[y].fa);
register int dirx=identify(x);
register int diry=identify(y);
register int b=t[x].ch[!dirx];
if(diry!=-1)t[z].ch[diry]=x;t[x].fa=z;
if(b)t[b].fa=y;t[y].ch[dirx]=b;
t[x].ch[!dirx]=y,t[y].fa=x;
pushup(y),pushup(x);
}
inline void pushall(int x){
if(identify(x)!=-1)pushall(t[x].fa=find(t[x].fa));
pushdown(x);
}
inline void splay(int x){
pushall(x);
while(identify(x)!=-1){
register int fa=t[x].fa;
if(identify(fa)==-1)rotate(x);
else if(identify(fa)==identify(x))rotate(fa),rotate(x);
else rotate(x),rotate(x);
}
}
inline void access(int x){
for(register int y=0;x;x=find(t[y=x].fa))splay(x),rson=y,pushup(x);
}
inline void makeroot(int x){
access(x),splay(x),REV(x);
}
inline int findroot(int x){
access(x),splay(x);
pushdown(x);
while(lson)x=lson,pushdown(x);
splay(x);
return x;
}
inline void split(int x,int y){
makeroot(x),access(y),splay(y);
}
inline void shrink(int x,int y){
merge(x,y);
if(x!=y)t[y].val+=t[x].val,t[x].fa=0;
pushdown(x);
if(lson)shrink(lson,y);
if(rson)shrink(rson,y);
lson=rson=0;
}
inline void link(int x,int y){
if(findroot(x)==findroot(y))split(x,y),shrink(y,y);
else makeroot(x),t[x].fa=y;
}
int main(){
n=read(),m=read();
for(register int i=1;i<=n;i++)val[i]=t[i].sum=t[i].val=read(),dsu[i]=i;
for(register int i=1,t1,t2,t3,t4;i<=m;i++){
t1=read(),t2=read(),t3=read();
if(t1==1)t2=find(t2),t3=find(t3),link(t2,t3);
if(t1==2)t4=t2,t4=find(t4),splay(t4),t[t4].val+=t3-val[t2],val[t2]=t3,pushup(t4);
if(t1==3){
t2=find(t2),t3=find(t3);
if(findroot(t2)==findroot(t3))split(t2,t3),printf("%d\n",t[t3].sum);
else puts("-1");
}
}
return 0;
}