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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| #include <bits/stdc++.h> using namespace std; const int maxn=100000+10; int n,q,w[maxn],c[maxn],T[maxn],sz; int head[maxn],to[maxn<<1],nxt[maxn<<1],tot; int dep[maxn],top[maxn],son[maxn],siz[maxn],fa[maxn],id[maxn],mp[maxn],tim; struct node{ int lson,rson,val,sum; #define mid (l+r>>1) #define lson(rt) tree[rt].lson #define rson(rt) tree[rt].rson #define val(rt) tree[rt].val #define sum(rt) tree[rt].sum }tree[maxn*24];
inline int read(){ register int x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} return (f==1)?x:-x; } inline void add(int x,int y){ to[++tot]=y; nxt[tot]=head[x]; head[x]=tot; }
void dfs1(int x,int f){ dep[x]=dep[f]+1; siz[x]=1; fa[x]=f; int maxson=-1; for(int i=head[x],y;i;i=nxt[i]){ y=to[i]; if(y==f) continue; dfs1(y,x); siz[x]+=siz[y]; if(maxson<siz[y]){ maxson=siz[y]; son[x]=y; } } }
void dfs2(int x,int topf){ id[x]=++tim; mp[tim]=x; top[x]=topf; if(son[x]) dfs2(son[x],topf); for(int i=head[x],y;i;i=nxt[i]){ y=to[i]; if(y==fa[x]||y==son[x]) continue; dfs2(y,y); } }
void pushup(int rt){ val(rt)=max(val(lson(rt)),val(rson(rt))); sum(rt)=sum(lson(rt))+sum(rson(rt)); } void pushdown(int rt){ val(rt)=sum(rt)=0; } void update(int &rt,int x,int l,int r,int v){ if(!rt) rt=++sz; if(l == r){ val(rt)=sum(rt)=v; return ; } if(x <= mid) update(lson(rt),x,l,mid,v); else update(rson(rt),x,mid+1,r,v); pushup(rt); } void del(int &rt,int x,int l,int r){ if(!rt) return ; if(l == r){ pushdown(rt); return ; } if(x <= mid) del(lson(rt),x,l,mid); else del(rson(rt),x,mid+1,r); pushup(rt); } int query_sum(int &rt,int L,int R,int l,int r){ if(!rt) return 0; if(L <= l && r <= R){ return sum(rt); } int ans=0; if(L <= mid) ans+=query_sum(lson(rt),L,R,l,mid); if(R > mid) ans+=query_sum(rson(rt),L,R,mid+1,r); return ans; } int query_max(int &rt,int L,int R,int l,int r){ if(!rt) return 0; if(L <= l && r <= R){ return val(rt); } int ans=0; if(L <= mid) ans=max(ans,query_max(lson(rt),L,R,l,mid)); if(R > mid) ans=max(ans,query_max(rson(rt),L,R,mid+1,r)); return ans; }
int main() { n=read(),q=read();int x,y,k,ans; for(int i=1;i<=n;i++) w[i]=read(),c[i]=read(); for(int i=1;i<n;i++){ x=read(),y=read(); add(x,y);add(y,x); } dfs1(1,0);dfs2(1,1); for(int i=1;i<=n;i++) update(T[c[mp[i]]],i,1,n,w[mp[i]]); char opt[50]; while(q--){ scanf("%s",opt); if(opt[1]=='C'){ x=read(),k=read(); del(T[c[x]],id[x],1,n); c[x]=k; update(T[c[x]],id[x],1,n,w[x]); } if(opt[1]=='W'){ x=read(),k=read(); del(T[c[x]],id[x],1,n); w[x]=k; update(T[c[x]],id[x],1,n,w[x]); } if(opt[1]=='S'){ x=read(),y=read(); k=c[x];ans=0; while(top[x]!=top[y]){ if(dep[top[x]]<dep[top[y]]) swap(x,y); ans+=query_sum(T[k],id[top[x]],id[x],1,n); x=fa[top[x]]; } if(dep[x]>dep[y]) swap(x,y); ans+=query_sum(T[k],id[x],id[y],1,n); printf("%d\n",ans); } if(opt[1]=='M'){ x=read(),y=read(); k=c[x];ans=0; while(top[x]!=top[y]){ if(dep[top[x]]<dep[top[y]]) swap(x,y); ans=max(ans,query_max(T[k],id[top[x]],id[x],1,n)); x=fa[top[x]]; } if(dep[x]>dep[y]) swap(x,y); ans=max(ans,query_max(T[k],id[x],id[y],1,n)); printf("%d\n",ans); } } return 0; }
|