-
Notifications
You must be signed in to change notification settings - Fork 0
/
5052_Yaogesmaximumprofit.cc
238 lines (201 loc) · 4.32 KB
/
5052_Yaogesmaximumprofit.cc
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
*
* Author : fcbruce <fcbruce8964@gmail.com>
*
* Time : Fri 02 Oct 2015 08:00:05 PM CST
*
*/
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#ifdef _WIN32
#define lld "%I64d"
#else
#define lld "%lld"
#endif
const int INF = 0x3f3f3f3f;
const long long INFLL = 0x3f3f3f3f3f3f3f3fll;
const double PI = acos(-1.0);
const double eps = 1e-10;
typedef long long LL;
typedef int itn;
const int maxn = 50007;
const int maxm = 100007;
int fir[maxn], w[maxn];
int u[maxm], v[maxm], nex[maxm];
int e_max;
int size[maxn], fa[maxn], son[maxn], deep[maxn], pos[maxn], top[maxn];
itn cp;
struct Node
{
int add, max, min, profit[2];
//profit[0] from top to bottom
//profit[1] from bottom to top
Node()
{
max = 0;
min = INF;
profit[0] = profit[1] = 0;
}
void update(int v)
{
add += v;
min += v;
max += v;
}
Node update(Node l, Node r)
{
min = std::min(l.min, r.min);
max = std::max(l.max, r.max);
profit[0] = std::max(std::max(0, r.max - l.min), std::max(l.profit[0], r.profit[0]));
profit[1] = std::max(std::max(0, l.max - r.min), std::max(l.profit[1], r.profit[1]));
return *this;
}
}node[maxn << 2];
inline void add_edge(int s, int t)
{
int e = e_max++;
u[e] = s; v[e] = t;
nex[e] = fir[u[e]]; fir[u[e]] = e;
}
void dfs(int s, int f, int d)
{
size[s] = 1;
fa[s] = f;
deep[s] = d;
son[s] = -1;
for (int e = fir[s]; ~e; e = nex[e]) if (v[e] != f)
{
dfs(v[e], s, d + 1);
size[s] += size[v[e]];
if (son[s] == -1 || size[son[s]] < size[v[e]]) son[s] = v[e];
}
}
void split(int s, int tp)
{
pos[s] = cp++;
top[s] = tp;
if (son[s] != -1) split(son[s], tp);
for (int e = fir[s]; ~e; e = nex[e]) if (v[e] != fa[s] && v[e] != son[s])
split(v[e], v[e]);
}
inline void pushdown(int k)
{
if (node[k].add > 0)
{
node[k * 2 + 1].update(node[k].add);
node[k * 2 + 2].update(node[k].add);
node[k].add = 0;
}
}
inline void pushup(int k)
{
node[k].update(node[k * 2 + 1], node[k * 2 + 2]);
}
void update(int a, int b, int v, int k, int l, int r)
{
if (b <= l || r <= a) return ;
if (a <= l && r <= b)
{
node[k].update(v);
return ;
}
pushdown(k);
update(a, b, v, k * 2 + 1, l, l + r >> 1);
update(a, b, v, k * 2 + 2, l + r >> 1, r);
pushup(k);
}
Node query(int a, int b, int k, int l, int r)
{
if (b <= l || r <= a) return Node();
if (a <= l && r <= b) return node[k];
pushdown(k);
return Node().update(query(a, b, k * 2 + 1, l, l + r >> 1), query(a, b, k * 2 + 2, l + r >> 1, r));
}
void update(int x, int y, int v)
{
while (top[x] != top[y])
{
if (deep[top[x]] > deep[top[y]]) std::swap(x, y);
update(pos[top[y]], pos[y] + 1, v, 0, 0, cp);
y = fa[top[y]];
}
if (deep[x] > deep[y]) std::swap(x, y);
update(pos[x], pos[y] + 1, v, 0, 0, cp);
}
int query(int x, int y)
{
Node tmp[2];
//tmp[0] from top to bottom
//tmp[1] from bottom to top
int tag = 0;
while (top[x] != top[y])
{
if (deep[top[x]] > deep[top[y]])
{
std::swap(x, y);
tag ^= 1;
}
tmp[tag].update(query(pos[top[y]], pos[y] + 1, 0, 0, cp), tmp[tag]);
y = fa[top[y]];
}
if (deep[x] > deep[y])
{
std::swap(x, y);
tag ^= 1;
}
tmp[tag].update(query(pos[x], pos[y] + 1, 0, 0, cp), tmp[tag]);
return std::max(tmp[0].max - tmp[1].min, std::max(tmp[0].profit[0], tmp[1].profit[1]));
}
int main()
{
#ifdef FCBRUCE
freopen("/home/fcbruce/code/t", "r", stdin);
#endif // FCBRUCE
int T_T;
scanf("%d", &T_T);
while (T_T--)
{
memset(fir, -1, sizeof fir);
e_max = 0;
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", w + i);
for (int i = 1; i < n; i++)
{
int u, v;
scanf("%d%d", &u, &v);
add_edge(u, v);
add_edge(v, u);
}
cp = 0;
dfs(1, -1, 0);
split(1, 1);
memset(node, 0, sizeof node);
for (int i = 1; i <= n; i++) update(pos[i], pos[i] + 1, w[i], 0, 0, cp);
int Q;
scanf("%d", &Q);
while (Q--)
{
int x, y, v;
scanf("%d%d%d", &x, &y, &v);
printf("%d\n", query(x, y));
update(x, y, v);
}
}
return 0;
}