背景:
好无聊…
题目传送门:
https://www.luogu.org/problemnew/show/P4114
题意:
一棵树,支持修改边权值和询问路径权值最大值的操作。
思路:
感觉
L
C
T
LCT
LCT比树剖的边操作好打。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node1{int d,fa,ma,op,son[2];} tr[300010];
struct node2{int x,y,z;} a[300010];
int n;
bool isroot(int x)
{
return tr[tr[x].fa].son[0]!=x&&tr[tr[x].fa].son[1]!=x;
}
bool isson(int x)
{
return x==tr[tr[x].fa].son[1];
}
void change(int x)
{
if(!x)return;
swap(tr[x].son[0],tr[x].son[1]);
tr[x].op^=1;
}
void pushup(int x)
{
tr[x].ma=max(tr[x].d,max(tr[tr[x].son[0]].ma,tr[tr[x].son[1]].ma));
}
void pushdown(int x)
{
if(!tr[x].op) return;
change(tr[x].son[0]),change(tr[x].son[1]);
tr[x].op=0;
}
void rot(int x)
{
int w=isson(x),y=tr[x].fa,yy=tr[y].fa;
tr[y].son[w]=tr[x].son[w^1];
if(tr[y].son[w]) tr[tr[y].son[w]].fa=y;
tr[x].fa=yy;
if(!isroot(y)) tr[yy].son[isson(y)]=x;
tr[x].son[w^1]=y;tr[y].fa=x;
pushup(y);
}
int sta[300010];
int top;
void splay(int x)
{
sta[top=1]=x;
for(int i=x;!isroot(i);i=tr[i].fa)
sta[++top]=tr[i].fa;
while(top) pushdown(sta[top--]);
for(int y=tr[x].fa;!isroot(x);rot(x),y=tr[x].fa)
if(!isroot(y)) isson(x)^isson(y)?rot(x):rot(y);
pushup(x);
}
void access(int x)
{
for(int y=0;x;y=x,x=tr[x].fa)
splay(x),tr[x].son[1]=y,pushup(x);
}
int findroot(int x)
{
access(x);splay(x);
while(tr[x].son[0]) x=tr[x].son[0];
//splay(x);
return x;
}
void makeroot(int x)
{
access(x);splay(x);change(x);
}
void link(int x,int y)
{
makeroot(x);
if(findroot(y)!=x) tr[x].fa=y;
}
void split(int x,int y)
{
makeroot(x);access(y);splay(y);
}
int query(int x,int y)
{
split(x,y);
return tr[y].ma;
}
int main()
{
int T,x,y;
char s[10];
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0;i<=(n<<1);i++)
tr[i]=(node1){0,0,0,0,0,0};
for(int i=1;i<=n-1;i++)
{
scanf("%d %d %d",&a[i].x,&a[i].y,&a[i].z);
link(a[i].x,i+n);
link(i+n,a[i].y);
tr[i+n].d=a[i].z;
pushup(i+n);
}
bool bz=true;
while(bz)
{
scanf("%s",s+1);
switch(s[1])
{
case 'Q':scanf("%d %d",&x,&y);printf("%d\n",query(x,y));break;
case 'C':scanf("%d %d",&x,&y);splay(x+n);tr[x+n].d=y;pushup(x+n);break;
case 'D':bz=false;break;
}
}
}
}