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
| # include<bits/stdc++.h> using namespace std;
typedef long long int LL;
const int INF = (~(1<<31)); const int N = 30000+7; const double eps = 1e-7;
inline int read(){ int x=0,f=1;char ch = getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while('0'<=ch&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} return x*f; } inline int gmax(int &x,int y){if(y>x)x=y;} inline int gmin(int &x,int y){if(y<x)x=y;} inline int mmax(int x,int y){if(y>x)x=y;return x; } inline int mmin(int x,int y){if(y<x)x=y;return x; } /****************************************************/
int w[N],n; vector<int >G[N];
/*** tree cut */ int dep[N],fa[N],sz[N],son[N];
void dfs1(int u,int f,int d){ dep[u]=d,fa[u]=f,sz[u]=1; int gz=G[u].size(); for(int to,i=0;i<gz;i++){ to=G[u][i]; if(to==f)continue; dfs1(to,u,d+1); sz[u]+=sz[to]; if(!son[u]||sz[to]>sz[son[u]]) son[u]=to; } }
int top[N],tree[N],pre[N],cnt;
void dfs2(int u,int rt){ top[u]=rt,tree[u]=++cnt,pre[tree[u]]=u; if(!son[u])return ; dfs2(son[u],rt); int gz=G[u].size(); for(int i=0,to;i<gz;i++){ to=G[u][i]; if(to==son[u]||to==fa[u]) continue; dfs2(to,to); } }
void init(int n){ cnt = 0; for(int i=0;i<=n;i++) son[i]=0; }
void add(int u,int v){ G[u].push_back(v); G[v].push_back(u); }
/*** Segmeng begin */ int mx[N<<2],sum[N<<2];
# define ll (rt<<1) # define rr (rt<<1|1) # define lson (rt<<1),l,mid # define rson (rt<<1|1),(mid+1),r # define mid ((l+r)>>1)
void pushup(int rt){ sum[rt]=sum[ll]+sum[rr]; mx[rt]=mmax(mx[ll],mx[rr]); }
void build(int rt,int l,int r){ if(l==r){ sum[rt]=mx[rt]=w[pre[l]]; return; } build(lson); build(rson); pushup(rt); }
void update(int rt,int l,int r,int pos,int val){ if(l==r){ sum[rt]=mx[rt]=val; return ; } if(pos<=mid) update(lson,pos,val); else update(rson,pos,val); pushup(rt); return ; }
int query_max(int rt,int l,int r,int L,int R){ if(L<=l&&r<=R) return mx[rt]; int ans=-INF; if(L<=mid) gmax(ans,query_max(lson,L,R)); if(R> mid) gmax(ans,query_max(rson,L,R)); return ans; }
int query_sum(int rt,int l,int r,int L,int R){ if(L<=l&&r<=R) return sum[rt]; int ans=0; if(L<=mid) ans+=query_sum(lson,L,R); if(R> mid) ans+=query_sum(rson,L,R); return ans; } /**** Segment end; */ int find_max(int x,int y){ int ans = -INF; int fx=top[x],fy=top[y]; // printf("%d %d\n",fx,fy); while(fx!=fy){ if(dep[fx]<dep[fy]) swap(fx,fy),swap(x,y); gmax(ans,query_max(1,1,n,tree[fx],tree[x])); x=fa[fx];fx=top[x]; } if(dep[x]>dep[y]) swap(x,y); gmax(ans,query_max(1,1,n,tree[x],tree[y])); return ans; }
int find_sum(int x,int y){ int ans = 0; int fx=top[x],fy=top[y]; while(fx!=fy){ if(dep[fx]<dep[fy]) swap(fx,fy),swap(x,y); ans+=query_sum(1,1,n,tree[fx],tree[x]); x=fa[fx];fx=top[x]; } if(dep[x]>dep[y]) swap(x,y); ans+=query_sum(1,1,n,tree[x],tree[y]); return ans; }
void print(){ puts("i : ");for(int i=1;i<=n;i++) printf("%d ",i);puts(""); puts("w[i]: ");for(int i=1;i<=n;i++) printf("%d ",w[i]);puts(""); puts("dep[i]: ");for(int i=1;i<=n;i++) printf("%d ",dep[i]);puts(""); puts("tree[i]: ");for(int i=1;i<=n;i++) printf("%d ",tree[i]);puts(""); puts("tree_w_max[i]: ");for(int i=1;i<=n;i++) printf("%d ",query_max(1,1,n,i,i));puts(""); puts("tree_w_min[i]: ");for(int i=1;i<=n;i++) printf("%d ",query_sum(1,1,n,i,i));puts(""); // for(int i=1;i<=n;i++) printf("%d ",i); // for(int i=1;i<=n;i++) printf("%d ",i); // for(int i=1;i<=n;i++) printf("%d ",i);
}
int main(){ scanf("%d",&n); for(int i=1,u,v;i<n;i++){ scanf("%d%d",&u,&v); add(u,v); } for(int i=1;i<=n;i++) scanf("%d",&w[i]);
init(n); dfs1(1,0,1),dfs2(1,1); build(1,1,n);
// print();
// printf("%d\n",query_sum(1,1,n,1,3)); // printf("%d\n",query_sum(1,1,n,4,4));
int q,a,b; char str[10]; scanf("%d",&q); while(q--){ scanf("%s %d %d",str,&a,&b); if(str[0]=='C'){ update(1,1,n,tree[a],b); w[a]=b; } else if(str[1]=='M'){ printf("%d\n",find_max(a,b)); } else { printf("%d\n",find_sum(a,b)); } } return 0; }
|