RT,也不知道是不是标记写挂了。
在线等
#include<bits/stdc++.h>
#define int long long
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
using namespace std;
const int N=2e5+10;
struct edge{int u,v,w;}g[N];
vector<int>e[N];
int val[N],fa[N],dep[N],sz[N],son[N],top[N],dfn[N],in[N],t[4][N<<2],tag[N<<2];
int n,m,tim;
void dfs(int u,int ls){
fa[u]=ls;sz[u]=1;dep[u]=dep[ls]+1;int mx=-1;
for(auto v:e[u])if(v!=ls){
dfs(v,u);sz[u]+=sz[v];
if(sz[v]>mx)mx=sz[v],son[u]=v;
}return;
}
void work(int u,int ls){
dfn[u]=++tim;
if(son[u]&&son[u]!=ls){
top[son[u]]=top[u];work(son[u],u);
}
for(auto v:e[u])if(v!=ls&&v!=son[u])top[v]=v,work(v,u);
return;
}
int ans(int i,int x,int y){
if(i==1)return x+y;
if(i==2)return min(x,y);
if(i==3)return max(x,y);
}
void pushup(int x){for(int i=1;i<=3;++i)t[i][x]=ans(i,t[i][ls(x)],t[i][rs(x)]);}
void addtag(int x){
tag[x]*=-1;
t[1][x]*=-1;
int tmp=t[2][x];
t[2][x]=-t[3][x];t[3][x]=-tmp;
return;
}
void pushdown(int x){
if(tag[x]!=1){
addtag(ls(x));addtag(rs(x));tag[x]=1;
}
}
void build(int x,int L=1,int R=n){
tag[x]=1;
if(L==R){t[1][x]=t[2][x]=t[3][x]=in[L];return;}int mid=L+R>>1;
build(ls(x),L,mid);build(rs(x),mid+1,R);pushup(x);
}
void mdf(int x,int k,int w,int L=1,int R=n){
if(L==R){t[1][x]=t[2][x]=t[3][x]=w;return;}
pushdown(x);
int mid=L+R>>1;
if(k<=mid)mdf(ls(x),k,w,L,mid);
if(k>mid)mdf(rs(x),k,w,mid+1,R);
pushup(x);
}
void qf(int x,int l,int r,int L=1,int R=n){
if(l<=L&&R<=r){tag[x]*=-1;t[1][x]*=-1;int tmp=t[2][x];t[2][x]=-t[3][x];t[3][x]=-tmp;return;}
pushdown(x);
int mid=L+R>>1;
if(l<=mid)qf(ls(x),l,r,L,mid);if(mid<r)qf(rs(x),l,r,mid+1,R);pushup(x);
}
int sm(int x,int l,int r,int L=1,int R=n){
if(l<=L&&R<=r){return t[1][x];}
int res=0,mid=L+R>>1;pushdown(x);
if(l<=mid)res+=sm(ls(x),l,r,L,mid);
if(mid<r)res+=sm(rs(x),l,r,mid+1,R);
return res;
}
int mn(int x,int l,int r,int L=1,int R=n){
if(l<=L&&R<=r){return t[2][x];}
int res=1e17,mid=L+R>>1;pushdown(x);
if(l<=mid)res=min(res,mn(ls(x),l,r,L,mid));
if(mid<r)res=min(res,mn(rs(x),l,r,mid+1,R));
return res;
}
int mx(int x,int l,int r,int L=1,int R=n){
if(l<=L&&R<=r){return t[3][x];}
int res=-1e17,mid=L+R>>1;pushdown(x);
if(l<=mid)res=max(res,mx(ls(x),l,r,L,mid));
if(mid<r)res=max(res,mx(rs(x),l,r,mid+1,R));
return res;
}
void sp_qf(int u,int v){
while(top[u]!=top[v]){
if(dep[top[u]]>dep[top[v]]){
qf(1,dfn[top[u]],dfn[u]);u=fa[top[u]];
}
else qf(1,dfn[top[v]],dfn[v]),v=fa[top[v]];
}
if(dep[u]>dep[v])swap(u,v);
qf(1,dfn[u]+1,dfn[v]);
}
int sp_qh(int u,int v){
int res=0;
while(top[u]!=top[v]){
if(dep[top[u]]>dep[top[v]]){
res+=sm(1,dfn[top[u]],dfn[u]);u=fa[top[u]];
}
else res+=sm(1,dfn[top[v]],dfn[v]),v=fa[top[v]];
}
if(dep[u]>dep[v])swap(u,v);
res+=sm(1,dfn[u]+1,dfn[v]);return res;
}
int sp_mn(int u,int v){
int res=1e17;
while(top[u]!=top[v]){
if(dep[top[u]]>dep[top[v]]){
res=min(res,mn(1,dfn[top[u]],dfn[u]));u=fa[top[u]];
}
else res=min(res,mn(1,dfn[top[v]],dfn[v])),v=fa[top[v]];
}
if(dep[u]>dep[v])swap(u,v);
res=min(res,mn(1,dfn[u]+1,dfn[v]));return res;
}
int sp_mx(int u,int v){
int res=-1e17;
while(top[u]!=top[v]){
if(dep[top[u]]>dep[top[v]]){
res=max(res,mx(1,dfn[top[u]],dfn[u]));u=fa[top[u]];
}
else res=max(res,mx(1,dfn[top[v]],dfn[v])),v=fa[top[v]];
}
if(dep[u]>dep[v])swap(u,v);
res=max(res,mx(1,dfn[u]+1,dfn[v]));return res;
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<n;++i){
int u,v,w;cin>>u>>v>>w;++u,++v;
e[u].push_back(v);e[v].push_back(u);
g[i]={u,v,w};
}
dfs(1,1);top[1]=1;work(1,1);
for(int i=1;i<n;++i){
int u=g[i].u,v=g[i].v,w=g[i].w;
if(dep[u]<dep[v])swap(u,v);
in[u]=w;val[i]=u;
}
cin>>m;
build(1);
for(int i=1;i<=m;++i){
string op;int u,v;cin>>op>>u>>v;++u,++v;
if(op=="C")--u,--v,mdf(1,dfn[val[u]],v);
if(op=="N")sp_qf(u,v);
if(op=="SUM")cout<<sp_qh(u,v)<<'\n';
if(op=="MAX")cout<<sp_mx(u,v)<<'\n';
if(op=="MIN")cout<<sp_mn(u,v)<<'\n';
}
return 0;
}