#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 11;
typedef pair<int, int> ii;
int n, q, w;
int a[N];
int cost[N];
ii E[N];
vector<int> adj[N], pos[N];
int top = 0;
void dfs(int u, int id_edge, int p = 0) {
for (int id : adj[u]) {
int x = E[id].first, y = E[id].second;
if (x == u) swap(x, y);
if (x == p) continue;
a[++top] = cost[id];
pos[id].push_back(top);
dfs(x, id, u);
}
if (p) {
a[++top] = -cost[id_edge];
pos[id_edge].push_back(top);
}
}
struct Node {
int pfix, sfix; // pfix get maximum prefix sum, sfix get maximum -suffix sum
int ans_pre, ans_suf; // f but ans_pre touch left, ans_suf touch right
int all; // f but touch left and touch right
int sum, f;
Node(){}
};
Node Seg[4 * N];
void merge(int node) {
Seg[node].pfix = max(Seg[node * 2].pfix, Seg[node * 2].sum + Seg[node * 2 + 1].pfix);
Seg[node].sfix = max(Seg[node * 2 + 1].sfix, -Seg[node * 2 + 1].sum + Seg[node * 2].sfix);
Seg[node].sum = Seg[node * 2].sum + Seg[node * 2 + 1].sum;
Seg[node].all = max(Seg[node * 2].all + Seg[node * 2 + 1].sum, -Seg[node * 2].sum + Seg[node * 2 + 1].all);
Seg[node].ans_pre = max(max(Seg[node * 2].ans_pre, Seg[node * 2].all + Seg[node * 2 + 1].pfix), -Seg[node * 2].sum + Seg[node * 2 + 1].ans_pre);
Seg[node].ans_suf = max(max(Seg[node * 2 + 1].ans_suf, Seg[node * 2 + 1].all + Seg[node * 2].sfix), Seg[node * 2 + 1].sum + Seg[node * 2].ans_suf);
Seg[node].f = max(Seg[node * 2].f, Seg[node * 2 + 1].f);
Seg[node].f = max(Seg[node].f, Seg[node * 2].ans_suf + Seg[node * 2 + 1].pfix);
Seg[node].f = max(Seg[node].f, Seg[node * 2].sfix + Seg[node * 2 + 1].ans_pre);
}
void build(int node, int l, int r) {
if (l == r) {
Seg[node].pfix = max(0, a[l]); Seg[node].sfix = max(0, -a[l]);
Seg[node].f = Seg[node].ans_pre = Seg[node].ans_suf = Seg[node].all = abs(a[l]);
Seg[node].sum = a[l];
return;
}
int mi = (l + r) / 2;
build(node * 2, l, mi);
build(node * 2 + 1, mi + 1, r);
merge(node);
return;
}
void upd(int node, int l, int r, int pos) {
if (l == r) {
Seg[node].pfix = max(0, a[l]); Seg[node].sfix = max(0, -a[l]);
Seg[node].f = Seg[node].ans_pre = Seg[node].ans_suf = Seg[node].all = abs(a[l]);
Seg[node].sum = a[l];
return;
}
int mi = (l + r) / 2;
if (pos <= mi) upd(node * 2, l, mi, pos);
else upd(node * 2 + 1, mi + 1, r, pos);
merge(node);
return;
}
int main(int argc, char** argv) {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> q >> w;
for (int i = 0; i < n - 1; i++) {
int u, v, c;
cin >> u >> v >> cost[i];
E[i] = ii(u, v);
adj[u].emplace_back(i);
adj[v].emplace_back(i);
}
dfs(1, -1);
build(1, 1, 2 * (n - 1));
int last = 0;
while (q--) {
int d, e; cin >> d >> e;
d = (d + last) % (n - 1);
e = (e + last) % w;
a[pos[d][0]] = e;
upd(1, 1, 2 * (n - 1), pos[d][0]);
a[pos[d][1]] = -e;
upd(1, 1, 2 * (n - 1), pos[d][1]);
last = Seg[1].f;
cout << last << '\n';
}
}
Compilation message
diameter.cpp: In function 'int main(int, char**)':
diameter.cpp:97:13: warning: unused variable 'c' [-Wunused-variable]
97 | int u, v, c;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12744 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12736 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12888 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
3 ms |
12636 KB |
Output is correct |
16 |
Correct |
2 ms |
12720 KB |
Output is correct |
17 |
Correct |
2 ms |
12636 KB |
Output is correct |
18 |
Correct |
2 ms |
12728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12744 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12736 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12888 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
3 ms |
12636 KB |
Output is correct |
16 |
Correct |
2 ms |
12720 KB |
Output is correct |
17 |
Correct |
2 ms |
12636 KB |
Output is correct |
18 |
Correct |
2 ms |
12728 KB |
Output is correct |
19 |
Correct |
5 ms |
12980 KB |
Output is correct |
20 |
Correct |
5 ms |
12892 KB |
Output is correct |
21 |
Correct |
5 ms |
12892 KB |
Output is correct |
22 |
Correct |
5 ms |
12892 KB |
Output is correct |
23 |
Correct |
7 ms |
15196 KB |
Output is correct |
24 |
Correct |
10 ms |
15280 KB |
Output is correct |
25 |
Correct |
7 ms |
15196 KB |
Output is correct |
26 |
Correct |
8 ms |
15560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
4 ms |
12636 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
9 ms |
12636 KB |
Output is correct |
5 |
Correct |
32 ms |
13060 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
11 ms |
12900 KB |
Output is correct |
11 |
Correct |
42 ms |
13136 KB |
Output is correct |
12 |
Correct |
5 ms |
15348 KB |
Output is correct |
13 |
Correct |
5 ms |
15296 KB |
Output is correct |
14 |
Correct |
6 ms |
15196 KB |
Output is correct |
15 |
Correct |
15 ms |
15252 KB |
Output is correct |
16 |
Correct |
54 ms |
15696 KB |
Output is correct |
17 |
Correct |
32 ms |
34760 KB |
Output is correct |
18 |
Correct |
32 ms |
34964 KB |
Output is correct |
19 |
Correct |
34 ms |
35024 KB |
Output is correct |
20 |
Correct |
46 ms |
35016 KB |
Output is correct |
21 |
Correct |
116 ms |
35528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
Output is correct |
2 |
Correct |
7 ms |
12892 KB |
Output is correct |
3 |
Correct |
24 ms |
13076 KB |
Output is correct |
4 |
Correct |
47 ms |
13428 KB |
Output is correct |
5 |
Correct |
7 ms |
15708 KB |
Output is correct |
6 |
Correct |
11 ms |
15708 KB |
Output is correct |
7 |
Correct |
32 ms |
15952 KB |
Output is correct |
8 |
Correct |
62 ms |
16352 KB |
Output is correct |
9 |
Correct |
21 ms |
24664 KB |
Output is correct |
10 |
Correct |
26 ms |
24696 KB |
Output is correct |
11 |
Correct |
53 ms |
24912 KB |
Output is correct |
12 |
Correct |
87 ms |
25144 KB |
Output is correct |
13 |
Correct |
37 ms |
34388 KB |
Output is correct |
14 |
Correct |
45 ms |
34644 KB |
Output is correct |
15 |
Correct |
78 ms |
34900 KB |
Output is correct |
16 |
Correct |
107 ms |
35048 KB |
Output is correct |
17 |
Correct |
115 ms |
35120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
11 ms |
25436 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12744 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12736 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12888 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
3 ms |
12636 KB |
Output is correct |
16 |
Correct |
2 ms |
12720 KB |
Output is correct |
17 |
Correct |
2 ms |
12636 KB |
Output is correct |
18 |
Correct |
2 ms |
12728 KB |
Output is correct |
19 |
Correct |
5 ms |
12980 KB |
Output is correct |
20 |
Correct |
5 ms |
12892 KB |
Output is correct |
21 |
Correct |
5 ms |
12892 KB |
Output is correct |
22 |
Correct |
5 ms |
12892 KB |
Output is correct |
23 |
Correct |
7 ms |
15196 KB |
Output is correct |
24 |
Correct |
10 ms |
15280 KB |
Output is correct |
25 |
Correct |
7 ms |
15196 KB |
Output is correct |
26 |
Correct |
8 ms |
15560 KB |
Output is correct |
27 |
Correct |
2 ms |
12632 KB |
Output is correct |
28 |
Correct |
4 ms |
12636 KB |
Output is correct |
29 |
Correct |
3 ms |
12636 KB |
Output is correct |
30 |
Correct |
9 ms |
12636 KB |
Output is correct |
31 |
Correct |
32 ms |
13060 KB |
Output is correct |
32 |
Correct |
2 ms |
12636 KB |
Output is correct |
33 |
Correct |
2 ms |
12636 KB |
Output is correct |
34 |
Correct |
2 ms |
12636 KB |
Output is correct |
35 |
Correct |
3 ms |
12636 KB |
Output is correct |
36 |
Correct |
11 ms |
12900 KB |
Output is correct |
37 |
Correct |
42 ms |
13136 KB |
Output is correct |
38 |
Correct |
5 ms |
15348 KB |
Output is correct |
39 |
Correct |
5 ms |
15296 KB |
Output is correct |
40 |
Correct |
6 ms |
15196 KB |
Output is correct |
41 |
Correct |
15 ms |
15252 KB |
Output is correct |
42 |
Correct |
54 ms |
15696 KB |
Output is correct |
43 |
Correct |
32 ms |
34760 KB |
Output is correct |
44 |
Correct |
32 ms |
34964 KB |
Output is correct |
45 |
Correct |
34 ms |
35024 KB |
Output is correct |
46 |
Correct |
46 ms |
35016 KB |
Output is correct |
47 |
Correct |
116 ms |
35528 KB |
Output is correct |
48 |
Correct |
3 ms |
12892 KB |
Output is correct |
49 |
Correct |
7 ms |
12892 KB |
Output is correct |
50 |
Correct |
24 ms |
13076 KB |
Output is correct |
51 |
Correct |
47 ms |
13428 KB |
Output is correct |
52 |
Correct |
7 ms |
15708 KB |
Output is correct |
53 |
Correct |
11 ms |
15708 KB |
Output is correct |
54 |
Correct |
32 ms |
15952 KB |
Output is correct |
55 |
Correct |
62 ms |
16352 KB |
Output is correct |
56 |
Correct |
21 ms |
24664 KB |
Output is correct |
57 |
Correct |
26 ms |
24696 KB |
Output is correct |
58 |
Correct |
53 ms |
24912 KB |
Output is correct |
59 |
Correct |
87 ms |
25144 KB |
Output is correct |
60 |
Correct |
37 ms |
34388 KB |
Output is correct |
61 |
Correct |
45 ms |
34644 KB |
Output is correct |
62 |
Correct |
78 ms |
34900 KB |
Output is correct |
63 |
Correct |
107 ms |
35048 KB |
Output is correct |
64 |
Correct |
115 ms |
35120 KB |
Output is correct |
65 |
Runtime error |
11 ms |
25436 KB |
Execution killed with signal 11 |
66 |
Halted |
0 ms |
0 KB |
- |