#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
const int N = 3e5 + 5;
int n;
long long dis[N];
long long weight[N];
int cnt, in[N], out[N];
vector<pair<int, int>> g[N];
struct node {
long long mx = 0, mn = 0, mxmn = 0, mnmx = 0, ans = 0, lazy = 0;
}seg[N * 4];
node merge(node& lx, node& rx) {
node ret;
ret.mx = max(lx.mx, rx.mx);
ret.mn = min(lx.mn, rx.mn);
ret.mxmn = max({lx.mxmn, rx.mxmn, lx.mx - 2LL * rx.mn});
ret.mnmx = max({lx.mnmx, rx.mnmx, - 2LL * lx.mn + rx.mx});
ret.ans = max({lx.ans, rx.ans, lx.mx + rx.mnmx, lx.mxmn + rx.mx});
return ret;
}
void push(int nd, int l, int r) {
if (!seg[nd].lazy) {
return;
}
seg[nd].mx += seg[nd].lazy;
seg[nd].mn += seg[nd].lazy;
seg[nd].mxmn -= seg[nd].lazy;
seg[nd].mnmx -= seg[nd].lazy;
if (l != r) {
int mid = (l + r) / 2;
int rs = nd + ((mid - l + 1) * 2);
seg[nd + 1].lazy += seg[nd].lazy;
seg[rs].lazy += seg[nd].lazy;
}
seg[nd].lazy = 0;
}
void upd(int nd, int l, int r, int s, int e, long long v) {
push(nd, l, r);
if (l >= s && r <= e) {
seg[nd].lazy = v;
push(nd, l, r);
return;
}
int mid = (l + r) / 2;
int rs = nd + ((mid - l + 1) * 2);
if (mid >= e) {
upd(nd + 1, l, mid, s, e, v);
push(rs, mid + 1, r);
} else {
if (mid < s) {
push(nd + 1, l, mid);
upd(rs, mid + 1, r, s, e, v);
} else {
upd(nd + 1, l, mid, s, e, v);
upd(rs, mid + 1, r, s, e, v);
}
}
seg[nd] = merge(seg[nd + 1], seg[rs]);
}
void dfs(int v, int pe) {
cnt++;
for (auto [u, i] : g[v]) {
if (i == pe) {
continue;
}
dis[u] = dis[v] + weight[i];
in[i] = cnt;
dfs(u, i);
out[i] = cnt - 1;
upd(0, 0, 2 * n - 2, in[i], out[i], weight[i]);
cnt++;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int q;
long long lim;
cin >> n >> q >> lim;
for(int i = 0; i < n - 1; ++i) {
int u, v, w;
cin >> u >> v >> w;
weight[i] = w;
g[u].emplace_back(v, i);
g[v].emplace_back(u, i);
}
dfs(1, n);
long long last = 0;
while (q--) {
long long d, c;
cin >> d >> c;
d = (d + last) % (n - 1);
c = (c + last) % lim;
upd(0, 0, 2 * n - 2, in[d], out[d], -weight[d]);
weight[d] = c;
upd(0, 0, 2 * n - 2, in[d], out[d], +weight[d]);
last = max(seg[0].mx, seg[0].ans);
cout << last << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10584 KB |
Output is correct |
17 |
Correct |
2 ms |
10584 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10584 KB |
Output is correct |
17 |
Correct |
2 ms |
10584 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
7 ms |
11100 KB |
Output is correct |
20 |
Correct |
8 ms |
10968 KB |
Output is correct |
21 |
Correct |
7 ms |
10844 KB |
Output is correct |
22 |
Correct |
8 ms |
11040 KB |
Output is correct |
23 |
Correct |
9 ms |
11868 KB |
Output is correct |
24 |
Correct |
10 ms |
11868 KB |
Output is correct |
25 |
Correct |
11 ms |
11920 KB |
Output is correct |
26 |
Correct |
13 ms |
12284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
4 ms |
10844 KB |
Output is correct |
4 |
Correct |
9 ms |
10588 KB |
Output is correct |
5 |
Correct |
38 ms |
11064 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
3 ms |
10844 KB |
Output is correct |
10 |
Correct |
13 ms |
10892 KB |
Output is correct |
11 |
Correct |
57 ms |
11152 KB |
Output is correct |
12 |
Correct |
4 ms |
11864 KB |
Output is correct |
13 |
Correct |
4 ms |
11868 KB |
Output is correct |
14 |
Correct |
8 ms |
11868 KB |
Output is correct |
15 |
Correct |
21 ms |
11992 KB |
Output is correct |
16 |
Correct |
75 ms |
12196 KB |
Output is correct |
17 |
Correct |
65 ms |
36028 KB |
Output is correct |
18 |
Correct |
61 ms |
36040 KB |
Output is correct |
19 |
Correct |
64 ms |
36144 KB |
Output is correct |
20 |
Correct |
86 ms |
36364 KB |
Output is correct |
21 |
Correct |
184 ms |
36804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10844 KB |
Output is correct |
2 |
Correct |
11 ms |
10976 KB |
Output is correct |
3 |
Correct |
45 ms |
10996 KB |
Output is correct |
4 |
Correct |
87 ms |
11348 KB |
Output is correct |
5 |
Correct |
9 ms |
12888 KB |
Output is correct |
6 |
Correct |
23 ms |
13240 KB |
Output is correct |
7 |
Correct |
65 ms |
13148 KB |
Output is correct |
8 |
Correct |
119 ms |
13564 KB |
Output is correct |
9 |
Correct |
38 ms |
24396 KB |
Output is correct |
10 |
Correct |
50 ms |
24400 KB |
Output is correct |
11 |
Correct |
128 ms |
24824 KB |
Output is correct |
12 |
Correct |
174 ms |
24920 KB |
Output is correct |
13 |
Correct |
75 ms |
36144 KB |
Output is correct |
14 |
Correct |
89 ms |
36288 KB |
Output is correct |
15 |
Correct |
155 ms |
36784 KB |
Output is correct |
16 |
Correct |
227 ms |
36724 KB |
Output is correct |
17 |
Correct |
176 ms |
36804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
544 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10584 KB |
Output is correct |
17 |
Correct |
2 ms |
10584 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
7 ms |
11100 KB |
Output is correct |
20 |
Correct |
8 ms |
10968 KB |
Output is correct |
21 |
Correct |
7 ms |
10844 KB |
Output is correct |
22 |
Correct |
8 ms |
11040 KB |
Output is correct |
23 |
Correct |
9 ms |
11868 KB |
Output is correct |
24 |
Correct |
10 ms |
11868 KB |
Output is correct |
25 |
Correct |
11 ms |
11920 KB |
Output is correct |
26 |
Correct |
13 ms |
12284 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
4 ms |
10844 KB |
Output is correct |
30 |
Correct |
9 ms |
10588 KB |
Output is correct |
31 |
Correct |
38 ms |
11064 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
10844 KB |
Output is correct |
34 |
Correct |
2 ms |
10844 KB |
Output is correct |
35 |
Correct |
3 ms |
10844 KB |
Output is correct |
36 |
Correct |
13 ms |
10892 KB |
Output is correct |
37 |
Correct |
57 ms |
11152 KB |
Output is correct |
38 |
Correct |
4 ms |
11864 KB |
Output is correct |
39 |
Correct |
4 ms |
11868 KB |
Output is correct |
40 |
Correct |
8 ms |
11868 KB |
Output is correct |
41 |
Correct |
21 ms |
11992 KB |
Output is correct |
42 |
Correct |
75 ms |
12196 KB |
Output is correct |
43 |
Correct |
65 ms |
36028 KB |
Output is correct |
44 |
Correct |
61 ms |
36040 KB |
Output is correct |
45 |
Correct |
64 ms |
36144 KB |
Output is correct |
46 |
Correct |
86 ms |
36364 KB |
Output is correct |
47 |
Correct |
184 ms |
36804 KB |
Output is correct |
48 |
Correct |
3 ms |
10844 KB |
Output is correct |
49 |
Correct |
11 ms |
10976 KB |
Output is correct |
50 |
Correct |
45 ms |
10996 KB |
Output is correct |
51 |
Correct |
87 ms |
11348 KB |
Output is correct |
52 |
Correct |
9 ms |
12888 KB |
Output is correct |
53 |
Correct |
23 ms |
13240 KB |
Output is correct |
54 |
Correct |
65 ms |
13148 KB |
Output is correct |
55 |
Correct |
119 ms |
13564 KB |
Output is correct |
56 |
Correct |
38 ms |
24396 KB |
Output is correct |
57 |
Correct |
50 ms |
24400 KB |
Output is correct |
58 |
Correct |
128 ms |
24824 KB |
Output is correct |
59 |
Correct |
174 ms |
24920 KB |
Output is correct |
60 |
Correct |
75 ms |
36144 KB |
Output is correct |
61 |
Correct |
89 ms |
36288 KB |
Output is correct |
62 |
Correct |
155 ms |
36784 KB |
Output is correct |
63 |
Correct |
227 ms |
36724 KB |
Output is correct |
64 |
Correct |
176 ms |
36804 KB |
Output is correct |
65 |
Runtime error |
544 ms |
1048576 KB |
Execution killed with signal 9 |
66 |
Halted |
0 ms |
0 KB |
- |