树的统计

题面
思路:

树链剖分模板题,1操作直接修改,2把线段树维护信息加上区间最大值

代码:
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
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 3e4 + 10;
const int inf = 1 << 30;
int n,head[maxn],tot,dep[maxn],fa[maxn],son[maxn],a[maxn],top[maxn],cnt;
int id[maxn],size[maxn],m,w[maxn];
struct edge
{
int next,to;
}e[maxn << 1];
struct node
{
int val,maxx;
}tr[maxn << 2];
void add(int u,int v)
{
e[++tot] = (edge){head[u],v};
head[u] = tot;
}
void dfs1(int x,int f,int deep)
{
dep[x] = deep;
fa[x] = f;
size[x] = 1;
int maxson = -1;
for(int i = head[x] ; i ; i = e[i].next)
{
int v = e[i].to;
if(v == f)continue;
dfs1(v,x,deep + 1);
size[x] += size[v];
if(size[v] > maxson)son[x] = v,maxson = size[v];
}
}
void dfs2(int x,int topf)
{
id[x] = ++ cnt;
a[cnt] = w[x];
top[x] = topf;
if(!son[x])return;
dfs2(son[x],topf);
for(int i = head[x] ; i ; i = e[i].next)
{
int v = e[i].to;
if(v == fa[x] or v == son[x])continue;
dfs2(v,v);
}
}
void update(int root)
{
tr[root].val = (tr[root << 1].val + tr[root << 1 | 1].val);
tr[root].maxx = max(tr[root << 1].maxx,tr[root << 1 | 1].maxx);
}
void build(int root,int l,int r)
{
if(l == r){
tr[root].val = a[l];
tr[root].maxx = a[l];
return;
}
int mid = l + r >> 1;
build(root << 1,l,mid);
build(root << 1 | 1,mid + 1,r);
update(root);
}
int query_max(int root,int l,int r,int L,int R)
{
int ans = -inf;
if(l >= L and r <= R)return tr[root].maxx;
int mid = l + r >> 1;
if(L <= mid)ans = max(ans,query_max(root << 1,l,mid,L,R));
if(R > mid)ans = max(ans,query_max(root << 1 | 1,mid + 1,r,L,R));
return ans;
}
int query_sum(int root,int l,int r,int L,int R)
{
int ans = 0;
if(l >= L and r <= R)return tr[root].val;
int mid = l + r >> 1;
if(L <= mid)ans += query_sum(root << 1,l,mid,L,R);
if(R > mid)ans += query_sum(root << 1 | 1,mid + 1,r,L,R);
return ans;
}
int qmax(int x,int y)
{
int ans = -inf;
while(top[x] != top[y])
{
if(dep[top[x]] < dep[top[y]])swap(x,y);
ans = max(ans,query_max(1,1,n,id[top[x]],id[x]));
x = fa[top[x]];
}
if(dep[x] > dep[y])swap(x,y);
ans = max(ans,query_max(1,1,n,id[x],id[y]));
return ans;
}
int qsum(int x,int y)
{
int ans = 0;
while(top[x] != top[y])
{
if(dep[top[x]] < dep[top[y]])swap(x,y);
ans += query_sum(1,1,n,id[top[x]],id[x]);
x = fa[top[x]];
}
if(dep[x] > dep[y])swap(x,y);
ans += query_sum(1,1,n,id[x],id[y]);
return ans;
}
void change(int root,int l,int r,int x,int y)
{
if(l == x and r == x){
tr[root].val = y;
tr[root].maxx = y;
return;
}
int mid = l + r >> 1;
if(x <= mid)change(root << 1,l,mid,x,y);
if(x > mid)change(root << 1 | 1,mid + 1,r,x,y);
update(root);
}
signed main()
{
cin >> n;
for(int i = 1,u,v ; i < n ; ++ i)
{
cin >> u >> v;
add(u,v);add(v,u);//加边
}
for(int i = 1 ; i <= n ; ++ i)cin >> w[i];
dfs1(1,0,1);
dfs2(1,1);
build(1,1,n);
cin >> m;
while(m--)
{
string opt;int x,y;
cin >> opt >> x >> y;
if(opt == "CHANGE")change(1,1,n,id[x],y);
if(opt == "QMAX")cout << qmax(x,y) << endl;
if(opt == "QSUM")cout << qsum(x,y) << endl;
}
return 0;
}