#include<bits/stdc++.h> using namespace std; // dfn[i] 表示节点 i 在 dfs序 中的位置 // dfx[i] 表示 dfs序 中第 i 个位置的节点 // son[i] 表示 i 的重儿子编号 // eid[i] 表示第 i 条边映射到点权的点的编号 // top[i] 表示点 i 所在重链的顶端节点 const int N=1e5+2; struct er{ int first,second,third; }; int siz[N],dep[N],fa[N],son[N],top[N],dfn[N],eid[N],dfx[N],num; vector<er> vec[N]; long long T[1<<18],a[N]; void build(int now,int l,int r){ if(l==r){ T[now]=a[dfx[l]]; return; } int mid=l+r>>1; build(now*2,l,mid); build(now*2+1,mid+1,r); T[now]=max(T[now*2],T[now*2+1]); } void upd(int now,int l,int r,int X,int v){ if(l==r){ T[now]=v; return; } int mid=l+r>>1; if(X<=mid)upd(now*2,l,mid,X,v); else upd(now*2+1,mid+1,r,X,v); T[now]=max(T[now*2],T[now*2+1]); } int qry(int now,int l,int r,int L,int R){ if(r<L||l>R) return 0; if(L<=l&&r<=R) return T[now]; int mid=l+r>>1; return max(qry(now*2,l,mid,L,R),qry(now*2+1,mid+1,r,L,R)); } void dfs(int id,int f){ fa[id]=f; siz[id]=1; dep[id]=dep[f]+1; int s=0; for(auto ivid:vec[id]){ int i=ivid.first; int v=ivid.second; int id2=ivid.third; a[i]=v; eid[id2]=i; if(i==f)continue; dfs(i,id); siz[id]+=siz[i]; if(siz[i]>siz[son[id]])son[id]=i; } } void dfs2(int id,int f){ dfn[id]=++num; dfx[num]=id; if(son[f]==id)top[id]=top[f]; else top[id]=id; if(son[id])dfs2(son[id],id); for(auto ivid:vec[id]){ int i=ivid.first; int v=ivid.second; if(i!=son[id]&&i!=f)dfs2(i,id); } } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; for(int i=1;i<n;i++){ int x,y,z; cin>>x>>y>>z; vec[x].push_back({y,z,i}); vec[y].push_back({x,z,i}); } dfs(1,0); dfs2(1,0); build(1,1,n); while(1){ string s; cin>>s; if(s=="DONE")break; int x,y; cin>>x>>y; if(s=="CHANGE")upd(1,1,n,dfn[eid[x]],y); else{ int ans=0; while(top[x]!=top[y]){ if(dep[top[x]]<dep[top[y]])swap(x,y); ans=max(ans,qry(1,1,n,dfn[top[x]],dfn[x])); x=fa[top[x]]; } if(dep[x]>dep[y])swap(x,y); ans=max(ans,qry(1,1,n,dfn[x]+1,dfn[y])); cout<<ans<<'\n'; } } return 0; }
Note.ms
/slpf