给一些点初始为0,每次增加一个点的权值,或者访问两点之间权值的最大值.
裸的LCT,套一下模板:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
const int maxn=100000+20;
struct node
{
node *f;
node *ch[2];
int val;
int maxval;
bool rev;
}tree[maxn],*null,Tnull;
bool isroot(node *u)
{
return u==null||u->f->ch[0]!=u&&u->f->ch[1]!=u;
}
void init(node *u)
{
u->f=u->ch[0]=u->ch[1]=null;
u->val=u->maxval=0;
u->rev=0;
}
void pushup(node *u)
{
u->maxval=max(u->val,max(u->ch[0]->maxval,u->ch[1]->maxval));
}
void pushdown(node *u)
{
if(u==null)return ;
if(u->rev)
{
swap(u->ch[0],u->ch[1]);
if(u->ch[0]!=null)u->ch[0]->rev^=1;
if(u->ch[1]!=null)u->ch[1]->rev^=1;
u->rev=0;
}
}
void rotate(node *u)
{
node *f=u->f;
node *ff=f->f;
int d=u==f->ch[1];
if(u->ch[d^1]!=null)u->ch[d^1]->f=f;
f->ch[d]=u->ch[d^1];
u->f=ff;
if(ff!=null)
{
if(f==ff->ch[0])ff->ch[0]=u;
else if(f==ff->ch[1])ff->ch[1]=u;
}
u->ch[d^1]=f;
f->f=u;
pushup(f);
}
node *sta[maxn];
int cnt;
void splay(node *u)
{
if(u==null)return ;
cnt=1;
sta[0]=u;
for(node *y=u;!isroot(y);y=y->f)
{
sta[cnt++]=y->f;
}
while(cnt)pushdown(sta[--cnt]);
while(!isroot(u))
{
node *f=u->f;
if(isroot(f))
{
rotate(u);
}
else
{
node *ff=f->f;
int d=u==f->ch[1];
int dd=f==ff->ch[1];
if(d==dd)rotate(f);
else rotate(u);
rotate(u);
}
}
pushup(u);
}
node *access(node *u)
{
node *v=null;
while(u!=null)
{
splay(u);
u->ch[1]=v;
v->f=u;
pushup(u);
v=u;
u=u->f;
}
return v;
}
int n,m;
struct edge
{
int v,next;
}e[2*maxn];
int head[maxn];
int k;
char s[5];
void add(int u,int v)
{
e[k].v=v;
e[k].next=head[u];
head[u]=k++;
}
void dfs(int u,int f)
{
tree[u].val=tree[u].maxval=0;
if(f!=-1)tree[u].f=tree+f;
else tree[u].f=null;
for(int i=head[u];~i;i=e[i].next)
{
int v=e[i].v;
if(v==f)continue;
dfs(v,u);
}
}
void increase(node *u,int v)
{
access(u);
splay(u);
u->val+=v;
pushup(u);
}
void changeroot(node *u)
{
access(u)->rev^=1;
}
node *getroot(node *u)
{
access(u);
splay(u);
while(u->f!=null)u=u->f;
splay(u);
return u;
}
void query(node *u,node *v)
{
changeroot(u);
access(v);
splay(v);
node *q=getroot(v);
printf("%d\n",q->maxval);
}
int main()
{
null=&Tnull;
init(null);
scanf("%d",&n);
memset(head,-1,sizeof(head));
k=1;
for(int i=1;i<=n;i++)init(&tree[i]);
for(int i=1;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
dfs(1,-1);
scanf("%d",&m);
int u,v;
for(int i=1;i<=m;i++)
{
scanf("%s%d%d",s,&u,&v);
if(s[0]=='I')increase(tree+u,v);
else query(tree+u,tree+v);
}
return 0;
}