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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
#include<iostream> #include<algorithm> #include<cstdio> #include<string> #include<cstring> #include<cmath> #include<map> #include<set> #include<queue> #include<vector> #include<limits.h> #define IL inline #define re register #define LL long long #define ULL unsigned long long #ifdef TH #define debug printf("Now is %d\n",__LINE__); #else #define debug #endif #ifdef ONLINE_JUDGE char buf[1<<23],* p1=buf,* p2=buf,obuf[1<<23],* O=obuf; #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) #endif using namespace std;
#define N 100010 int n,q; int head[N],nxt[N<<1],ver[N<<1],cnt; void insert(int x,int y) { nxt[++cnt]=head[x]; head[x]=cnt; ver[cnt]=y; nxt[++cnt]=head[y]; head[y]=cnt; ver[cnt]=x; } int f[N],son[N],sze[N],dep[N]; void dfs1(int x) { dep[x]=dep[f[x]]+1; sze[x]=1; for(int i=head[x],v;i;i=nxt[i]) { v=ver[i]; if(v==f[x]) continue; f[v]=x; dfs1(v); if(sze[v]>sze[son[x]]) son[x]=v; sze[x]+=sze[v]; } } int top[N],dfn[N],dcnt,id[N]; void dfs2(int x,int tt) { top[x]=tt; dfn[++dcnt]=x; id[x]=dcnt; if(!son[x]) return; dfs2(son[x],tt); for(int i=head[x],v;i;i=nxt[i]) { v=ver[i]; if(v==f[x]||v==son[x]) continue; dfs2(v,v); } } int val[N],color[N]; namespace Seg { struct node { int ls,rs; int sum; int mx; node() { ls=rs=0; } #define ls(x) b[x].ls #define rs(x) b[x].rs #define sum(x) b[x].sum #define mx(x) b[x].mx }b[N*20]; int cnt; void upd(int x) { sum(x)=sum(ls(x))+sum(rs(x)); mx(x)=max(mx(ls(x)),mx(rs(x))); } void change(int &p,int l,int r,int x,int k) { if(!p) p=++cnt; if(l==r) { sum(p)=mx(p)=k; return; } int mid=(l+r)>>1; if(x<=mid) change(ls(p),l,mid,x,k); else change(rs(p),mid+1,r,x,k); upd(p); } int ask_sum(int p,int l,int r,int L,int R) { if(!p) return 0; if(L<=l&&r<=R) { return sum(p); } int mid=(l+r)>>1,ans=0; if(L<=mid) ans+=ask_sum(ls(p),l,mid,L,R); if(R>mid) ans+=ask_sum(rs(p),mid+1,r,L,R); return ans; } int ask_mx(int p,int l,int r,int L,int R) { if(!p) return 0; if(L<=l&&r<=R) { return mx(p); } int mid=(l+r)>>1,ans=0; if(L<=mid) ans=max(ans,ask_mx(ls(p),l,mid,L,R)); if(R>mid) ans=max(ans,ask_mx(rs(p),mid+1,r,L,R)); return ans; } }; int root[N]; int main() { n=read(); q=read(); Seg::cnt=0; for(int i=1;i<=n;i++) { val[i]=read(); color[i]=read(); } for(int i=1;i<n;i++) { insert(read(),read()); } dfs1(1); dfs2(1,1); for(int i=1;i<=n;i++) { Seg::change(root[color[i]],1,n,id[i],val[i]); } string op; int x,y; for(int i=1;i<=q;i++) { oi::cin>>op>>x>>y; if(op=="CC") { Seg::change(root[color[x]],1,n,id[x],0); color[x]=y; Seg::change(root[color[x]],1,n,id[x],val[x]); } else if(op=="CW") { Seg::change(root[color[x]],1,n,id[x],val[x]=y); } else if(op=="QS") { int ans=0,c=color[x]; while(top[x]!=top[y]) { if(dep[top[x]]<dep[top[y]]) swap(x,y); ans+=Seg::ask_sum(root[c],1,n,id[top[x]],id[x]); x=f[top[x]]; } if(dep[x]>dep[y]) swap(x,y); ans+=Seg::ask_sum(root[c],1,n,id[x],id[y]); write(ans); } else if(op=="QM") { int ans=0,c=color[x]; while(top[x]!=top[y]) { if(dep[top[x]]<dep[top[y]]) swap(x,y); ans=max(ans,Seg::ask_mx(root[c],1,n,id[top[x]],id[x])); x=f[top[x]]; } if(dep[x]>dep[y]) swap(x,y); ans=max(ans,Seg::ask_mx(root[c],1,n,id[x],id[y])); write(ans); } } return 0; }
|