1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| #include <iostream> #include <cstring> #include <algorithm> using namespace std; #define swpa swap #define remove _ const int N = 2e6+10, M=N*2; int n,m,cnt2; int w[N],h[N],ne[M],e[M],idx; int id[N],nw[N],cnt; int dep[N],sz[N],top[N],fa[N],son[N]; int root[N]; struct Tree{ int ls,rs; int sum,mx; }tr[N*4]; int c[N]; inline void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ; }
void dfs1(int u,int father,int depth){ dep[u]=depth,fa[u]=father,sz[u]=1; for(int i=h[u];~i;i=ne[i]){ int j=e[i]; if(j==father)continue; dfs1(j,u,depth+1); sz[u]+=sz[j]; if(sz[son[u]]<sz[j])son[u]=j; } }
void dfs2(int u,int t){ id[u]=++cnt,nw[cnt]=w[u],top[u]=t; if(!son[u])return; dfs2(son[u],t); for(int i=h[u];~i;i=ne[i]){ int j=e[i]; if(j==fa[u]||j==son[u])continue; dfs2(j,j); } }
inline void pushup(int u){ tr[u].sum=tr[tr[u].ls].sum+tr[tr[u].rs].sum; tr[u].mx=max(tr[tr[u].ls].mx,tr[tr[u].rs].mx); } inline void pushdown(int u){ } inline void modify(int &o,int l,int r,int i,int k){ if(!o)o=++cnt2; tr[o].mx=tr[o].sum=k; if(l==r)return; int mid=l+r>>1; if(i<=mid)modify(tr[o].ls,l,mid,i,k); else modify(tr[o].rs,mid+1,r,i,k); pushup(o); } void remove(int &o,int l,int r,int i,int k=0){ if(l==r){tr[o].sum=tr[o].mx=0;return;} int mid=l+r>>1; if(i<=mid)remove(tr[o].ls,l,mid,i,k); else remove(tr[o].rs,mid+1,r,i,k); pushup(o); } inline int query_max(int u,int ql,int qr,int l,int r){ if(!u)return -1; if(qr<l||ql>r)return -1; if(l>=ql&&r<=qr)return tr[u].mx; int mid=l+r>>1; return max(query_max(tr[u].ls,ql,qr,l,mid), query_max(tr[u].rs,ql,qr,mid+1,r)); } inline int query_sum(int u,int ql,int qr,int l,int r){ if(!u)return 0; if(qr<l||ql>r)return 0; if(l>=ql&&r<=qr)return tr[u].sum; int mid=l+r>>1; return query_sum(tr[u].ls,ql,qr,l,mid)+query_sum(tr[u].rs,ql,qr,mid+1,r); }
inline int query_path_sum(int o,int u, int v) { int res = 0; while (top[u] != top[v]) { if (dep[top[u]]<dep[top[v]])swap(u, v); res+=query_sum(root[o],id[top[u]],id[u],1,n); u=fa[top[u]]; } if (dep[u]>dep[v])swap(u, v); res+=query_sum(root[o],id[u],id[v],1,n); return res; } inline int query_path_max(int o,int u,int v){ int res=0; while(top[u]!=top[v]){ if(dep[top[u]]<dep[top[v]])swpa(u,v); res=max(res,query_max(root[o],id[top[u]],id[u],1,n)); u=fa[top[u]]; } if(dep[u]>dep[v])swap(u,v); res=max(res,query_max(root[o],id[u],id[v],1,n)); return res; } signed main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); memset(h,-1,sizeof h); cin>>n>>m; for(int i=1;i<=n;i++)cin>>w[i]>>c[i]; for(int i=1;i<n;i++){ int a,b; cin>>a>>b; add(a,b),add(b,a); } dfs1(1,-1,1); dfs2(1,1); for(int i=1;i<=n;i++)modify(root[c[i]],1,n,id[i],w[i]); while(m--){ string op; int x,y; cin>>op>>x>>y; if(op[1]=='C')remove(root[c[x]],1,n,id[x]),modify(root[y],1,n,id[x],w[x]),c[x]=y; if(op[1]=='W')remove(root[c[x]],1,n,id[x]),modify(root[c[x]],1,n,id[x],y),w[x]=y; if(op[1]=='S')cout<<query_path_sum(c[x],x,y)<<'\n'; if(op[1]=='M')cout<<query_path_max(c[x],x,y)<<'\n'; } }
|