LCT秒天秒地用什么树剖
这题可以算是树剖的比较裸的题目了
把每一条边的权值下放到他两边的点中深度较深的那个
然后直接用树剖+线段树带进去乱搞就可以了
//minamoto
#include<bits/stdc++.h>
using namespace std;
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,:;}
inline int read(){
#define num ch-'0'
char ch;bool flag=;int res;
while(!isdigit(ch=getchar()))
(ch=='-')&&(flag=true);
for(res=num;isdigit(ch=getchar());res=res*+num);
(flag)&&(res=-res);
#undef num
return res;
}
char sr[<<],z[];int C=-,Z;
inline void Ot(){fwrite(sr,,C+,stdout),C=-;}
inline void print(int x){
if(C><<)Ot();if(x<)sr[++C]=,x=-x;
while(z[++Z]=x%+,x/=);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int N=1e5+;
int head[N],Next[N<<],ver[N<<],edge[N<<],tot=;
inline void add(int u,int v,int e){
ver[++tot]=v,Next[tot]=head[u],head[u]=tot,edge[tot]=e;
}
int val[N],num[N],son[N],sz[N],fa[N],dep[N],top[N],dfn[N],cnt;
int n;
void dfs1(int u){
sz[u]=,dep[u]=dep[fa[u]]+;
for(int i=head[u];i;i=Next[i]){
int v=ver[i];
if(v!=fa[u]){
fa[v]=u,num[i>>]=v,dfs1(v),sz[u]+=sz[v];
if(sz[son[u]]<sz[v]) son[u]=v;
}
}
}
void dfs2(int u,int t){
top[u]=t,dfn[u]=++cnt;
if(son[u]){
dfs2(son[u],t);
for(int i=head[u];i;i=Next[i]){
int v=ver[i];
if(v!=fa[u]&&v!=son[u]) dfs2(v,v);
}
}
}
int mx[N<<];
#define ls (p<<1)
#define rs (p<<1|1)
inline void upd(int p){mx[p]=max(mx[ls],mx[rs]);}
void build(int p,int l,int r){
if(l==r) return (void)(mx[p]=val[l]);
int mid=(l+r)>>;
build(ls,l,mid),build(rs,mid+,r);
upd(p);
}
int query(int p,int l,int r,int ql,int qr){
if(ql<=l&&qr>=r) return mx[p];
int mid=(l+r)>>,res=;
if(ql<=mid) cmax(res,query(ls,l,mid,ql,qr));
if(qr>mid) cmax(res,query(rs,mid+,r,ql,qr));
return res;
}
void update(int p,int l,int r,int x){
if(l==r) return (void)(mx[p]=val[l]);
int mid=(l+r)>>;
x<=mid?update(ls,l,mid,x):update(rs,mid+,r,x);
upd(p);
}
inline void change(int i,int t){
val[dfn[num[i]]]=t,update(,,n,dfn[num[i]]);
}
int query_path(int u,int v){
if(u==v) return ;
int res=;
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]]) swap(u,v);
cmax(res,query(,,n,dfn[top[u]],dfn[u]));
u=fa[top[u]];
}
if(u==v) return res;
if(dep[u]<dep[v]) swap(u,v);
cmax(res,query(,,n,dfn[son[v]],dfn[u]));
return res;
}
char s[];int a,b;
int main(){
// freopen("testdata.in","r",stdin);
n=read();
for(int i=,u,v,e;i<n;++i)
u=read(),v=read(),e=read(),add(u,v,e),add(v,u,e);
dfs1(),dfs2(,);
for(int i=;i<n;++i) val[dfn[num[i]]]=edge[i<<];
build(,,n);
while(true){
scanf("%s",s);if(s[]=='D') break;
if(s[]=='C') a=read(),b=read(),change(a,b);
else a=read(),b=read(),print(query_path(a,b));
}
Ot();
return ;
}