题意:中文题。
析:就是直接维护一个最大值和一个和,用线段树维护即可,这个题很简单,但是我卡了一晚上,就是在定位的时候,位置直接反过来了,但是样例全过了。。。真是。。。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e16; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 30000 + 10; const int mod = 1e9 + 7; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } struct Edge{ int to, next; }; Edge edge[maxn<<1]; int head[maxn], cnt, fa[maxn]; int dep[maxn], top[maxn], num[maxn]; int son[maxn], p[maxn], pos, a[maxn], fp[maxn]; int sum[maxn<<2], maxv[maxn<<2]; void init(){ memset(son, -1, sizeof son); cnt = 0; pos = 1; memset(head, -1, sizeof head); } void add_edge(int u, int v){ edge[cnt].to = v; edge[cnt].next = head[u]; head[u] = cnt++; } void dfs1(int u, int f, int d){ dep[u] = d; fa[u] = f; num[u] = 1; for(int i = head[u]; ~i; i = edge[i].next){ int v = edge[i].to; if(v == f) continue; dfs1(v, u, d+1); num[u] += num[v]; if(son[u] == -1 || num[v] > num[son[u]]) son[u] = v; } } void dfs2(int u, int sp){ top[u] = sp; p[u] = pos++; fp[p[u]] = u; if(son[u] == -1) return ; dfs2(son[u], sp); for(int i = head[u]; ~i; i = edge[i].next){ int v = edge[i].to; if(v == fa[u] || v == son[u]) continue; dfs2(v, v); } } void push_up(int rt){ sum[rt] = sum[rt<<1] + sum[rt<<1|1]; maxv[rt] = max(maxv[rt<<1], maxv[rt<<1|1]); } void build(int l, int r, int rt){ if(l == r){ sum[rt] = maxv[rt] = a[fp[l]]; return ; } int m = l + r >> 1; build(lson); build(rson); push_up(rt); } void update(int M, int val, int l, int r, int rt){ if(l == r){ sum[rt] = maxv[rt] = val; return ; } int m = l + r >> 1; if(M <= m) update(M, val, lson); else update(M, val, rson); push_up(rt); } int queryMax(int L, int R, int l, int r, int rt){ if(L <= l && r <= R) return maxv[rt]; int m = l + r >> 1; int ans = -INF; if(L <= m) ans = queryMax(L, R, lson); if(R > m) ans = max(ans, queryMax(L, R, rson)); return ans; } int querySum(int L, int R, int l, int r, int rt){ if(L <= l && r <= R) return sum[rt]; int m = l + r >> 1; int ans = 0; if(L <= m) ans = querySum(L, R, lson); if(R > m) ans += querySum(L, R, rson);; return ans; } int solve(int u, int v, bool ok){ int f1 = top[u], f2 = top[v]; int ans = -INF; if(ok) ans = 0; while(f1 != f2){ if(dep[f1] < dep[f2]){ swap(f1, f2); swap(u, v); } if(ok) ans += querySum(p[f1], p[u], 1, n, 1); else ans = max(ans, queryMax(p[f1], p[u], 1, n, 1)); u = fa[f1]; f1 = top[u]; } if(dep[u] > dep[v]) swap(u, v); if(ok) return ans + querySum(p[u], p[v], 1, n, 1); return max(ans, queryMax(p[u], p[v], 1, n, 1)); } int main(){ while(scanf("%d", &n) == 1){ init(); for(int i = 1; i < n; ++i){ int u, v; scanf("%d %d", &u, &v); add_edge(u, v); add_edge(v, u); } for(int i = 1; i <= n; ++i) scanf("%d", a+i); dfs1(1, 0, 0); dfs2(1, 1); build(1, n, 1); scanf("%d", &m); while(m--){ char op[10]; int u, v; scanf("%s %d %d", op, &u, &v); if(op[0] == 'C') update(p[u], v, 1, n, 1); else printf("%d ", solve(u, v, op[1] == 'S')); } } return 0; }