#include <bits/stdc++.h>
using namespace std;
#define int int64_t
const int Z = 1e5, B = 17, INF = 1e18+1;
array<int, 2> operator+(const int &x, array<int, 2> y) {
y[0] += x;
return y;
}
struct SegmentTree {
int n, *add;
array<int, 2> *val;
int sL, sR, sV;
SegmentTree(int N) : n(2<<__lg(N-1)), add(new int[2*n] {}), val(new array<int, 2> [2*n] {}) {
for(int i = n - 1; i < 2*n; ++i) val[i][0] = -INF;
}
void assign(const int &i, const array<int, 2> &v) {
val[n+i-1] = v;
}
void build() {
for(int i = n-1; i--; ) val[i] = max(val[2*i+1], val[2*i+2]);
}
void update(int l, int r, int v) {
sL = l, sR = r + 1, sV = v;
upd(0, 0, n);
}
void upd(int x, int l, int r) {
if(sL <= l && r <= sR) {
val[x][0] += sV;
add[x] += sV;
return;
}
if(sR <= l || r <= sL) return;
int m = (l + r) / 2;
upd(2*x+1, l, m);
upd(2*x+2, m, r);
val[x] = add[x] + max(val[2*x+1], val[2*x+2]);
}
array<int, 2> queryOut(int l, int r) {
return max(query(0, l - 1), query(r + 1, n - 1));
}
array<int, 2> query(int l, int r) {
sL = l, sR = r + 1;
return qry(0, 0, n);
}
array<int, 2> qry(int x, int l, int r) {
if(sL <= l && r <= sR) return val[x];
if(sR <= l || r <= sL) return {-INF};
int m = (l + r) / 2;
return add[x] + max(qry(2*x+1, l, m), qry(2*x+2, m, r));
}
}* seg[Z];
int n, q, wLim, w[Z], sz[Z], par[Z];
array<int, 2> edge[Z];
vector<pair<int, int>> g[Z];
bool vis[Z];
void init(int u, int p) {
sz[u] = 1;
for(const auto &[v, e] : g[u]) if(v != p)
init(v, u), sz[u] += sz[v];
}
int r, h, curSize, curSub, dfsTimer, id[B][Z], L[B][Z], R[B][Z], sub[B][Z];
int find(int u) {
for(const auto &[v, e] : g[u]) if(!vis[v] && sz[v] > curSize / 2) {
sz[u] -= sz[v];
sz[v] = curSize;
return find(v);
}
return u;
}
void dfs(int u, int p, int d) {
L[h][u] = dfsTimer++;
for(const auto &[v, e] : g[u]) if(!vis[v] && v != p) {
if(u == r) curSub = v;
dfs(v, u, d + w[e]);
}
R[h][u] = dfsTimer-1;
sub[h][u] = u == r ? r : curSub;
id[h][u] = r;
seg[r]->assign(L[h][u], {d, u});
}
void decompose(int u, int _h) {
curSize = sz[u];
vis[u = r = find(u)] = 1;
h = _h, dfsTimer = 0;
seg[r] = new SegmentTree(sz[r]);
dfs(r, r, 0);
seg[r]->build();
for(const auto &[v, e] : g[u]) if(!vis[v])
decompose(v, _h + 1);
}
array<int, 3> diam {-1};
int _r;
void initDiam(int u, int p, int d) {
diam = max(diam, {d, _r, u});
for(const auto &[v, e] : g[u]) if(v != p)
initDiam(v, u, d + w[e]);
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> q >> wLim;
for(int i = 0; i + 1 < n; ++i) {
auto &[u, v] = edge[i];
cin >> u >> v >> w[i];
--u, --v;
g[u].emplace_back(v, i);
g[v].emplace_back(u, i);
}
for(int i = 0; i < B; ++i)
fill(id[i], id[i] + n, -1);
init(0, 0);
decompose(0, 0);
initDiam(_r = diam[2], -1, 0);
initDiam(_r = diam[2], -1, 0);
for(int last {}; q--; ) {
int qD, qE; cin >> qD >> qE;
(qD += last) %= n - 1;
(qE += last) %= wLim;
auto [u, v] = edge[qD];
for(int i = 0; i < B; ++i) if(id[i][u] >= 0 && id[i][u] == id[i][v]) {
if(L[i][u] > L[i][v]) swap(u, v);
seg[id[i][u]]->update(L[i][v], R[i][v], qE - w[qD]);
}
w[qD] = qE;
array<int, 2> chk = {diam[1], diam[2]};
diam[0] = -1;
for(const int &c : chk) {
array<int, 2> best {-1};
for(int i = B; i--; ) if(id[i][c] >= 0) {
auto cur = seg[id[i][c]]->queryOut(L[i][sub[i][c]], R[i][sub[i][c]]);
cur[0] += seg[id[i][c]]->query(L[i][c], L[i][c])[0];
best = max(best, cur);
}
diam = max(diam, {best[0], best[1], c});
}
cout << (last = diam[0]) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2768 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
2 ms |
2788 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
2 ms |
2900 KB |
Output is correct |
8 |
Correct |
2 ms |
2948 KB |
Output is correct |
9 |
Correct |
2 ms |
2900 KB |
Output is correct |
10 |
Correct |
2 ms |
2900 KB |
Output is correct |
11 |
Correct |
2 ms |
2948 KB |
Output is correct |
12 |
Correct |
2 ms |
2900 KB |
Output is correct |
13 |
Correct |
2 ms |
2948 KB |
Output is correct |
14 |
Correct |
2 ms |
2944 KB |
Output is correct |
15 |
Correct |
2 ms |
2900 KB |
Output is correct |
16 |
Correct |
2 ms |
2900 KB |
Output is correct |
17 |
Correct |
2 ms |
2900 KB |
Output is correct |
18 |
Correct |
2 ms |
2900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2768 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
2 ms |
2788 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
2 ms |
2900 KB |
Output is correct |
8 |
Correct |
2 ms |
2948 KB |
Output is correct |
9 |
Correct |
2 ms |
2900 KB |
Output is correct |
10 |
Correct |
2 ms |
2900 KB |
Output is correct |
11 |
Correct |
2 ms |
2948 KB |
Output is correct |
12 |
Correct |
2 ms |
2900 KB |
Output is correct |
13 |
Correct |
2 ms |
2948 KB |
Output is correct |
14 |
Correct |
2 ms |
2944 KB |
Output is correct |
15 |
Correct |
2 ms |
2900 KB |
Output is correct |
16 |
Correct |
2 ms |
2900 KB |
Output is correct |
17 |
Correct |
2 ms |
2900 KB |
Output is correct |
18 |
Correct |
2 ms |
2900 KB |
Output is correct |
19 |
Correct |
19 ms |
3912 KB |
Output is correct |
20 |
Correct |
21 ms |
3924 KB |
Output is correct |
21 |
Correct |
24 ms |
3980 KB |
Output is correct |
22 |
Correct |
23 ms |
4092 KB |
Output is correct |
23 |
Correct |
32 ms |
8200 KB |
Output is correct |
24 |
Correct |
41 ms |
9300 KB |
Output is correct |
25 |
Correct |
44 ms |
10060 KB |
Output is correct |
26 |
Correct |
44 ms |
11092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2816 KB |
Output is correct |
3 |
Correct |
3 ms |
2772 KB |
Output is correct |
4 |
Correct |
16 ms |
3088 KB |
Output is correct |
5 |
Correct |
68 ms |
3976 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
3028 KB |
Output is correct |
8 |
Correct |
2 ms |
3028 KB |
Output is correct |
9 |
Correct |
4 ms |
3088 KB |
Output is correct |
10 |
Correct |
24 ms |
3316 KB |
Output is correct |
11 |
Correct |
109 ms |
4300 KB |
Output is correct |
12 |
Correct |
5 ms |
5460 KB |
Output is correct |
13 |
Correct |
5 ms |
5476 KB |
Output is correct |
14 |
Correct |
7 ms |
5520 KB |
Output is correct |
15 |
Correct |
33 ms |
5844 KB |
Output is correct |
16 |
Correct |
145 ms |
6964 KB |
Output is correct |
17 |
Correct |
61 ms |
55484 KB |
Output is correct |
18 |
Correct |
63 ms |
55564 KB |
Output is correct |
19 |
Correct |
64 ms |
55592 KB |
Output is correct |
20 |
Correct |
103 ms |
55876 KB |
Output is correct |
21 |
Correct |
223 ms |
57140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3924 KB |
Output is correct |
2 |
Correct |
34 ms |
4044 KB |
Output is correct |
3 |
Correct |
163 ms |
4596 KB |
Output is correct |
4 |
Correct |
318 ms |
5188 KB |
Output is correct |
5 |
Correct |
20 ms |
15316 KB |
Output is correct |
6 |
Correct |
77 ms |
15400 KB |
Output is correct |
7 |
Correct |
328 ms |
16052 KB |
Output is correct |
8 |
Correct |
657 ms |
16852 KB |
Output is correct |
9 |
Correct |
91 ms |
72908 KB |
Output is correct |
10 |
Correct |
193 ms |
73044 KB |
Output is correct |
11 |
Correct |
573 ms |
73676 KB |
Output is correct |
12 |
Correct |
1102 ms |
74628 KB |
Output is correct |
13 |
Correct |
151 ms |
149592 KB |
Output is correct |
14 |
Correct |
259 ms |
149904 KB |
Output is correct |
15 |
Correct |
752 ms |
150596 KB |
Output is correct |
16 |
Correct |
1337 ms |
151352 KB |
Output is correct |
17 |
Correct |
1401 ms |
151236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1475 ms |
148408 KB |
Output is correct |
2 |
Correct |
1548 ms |
151868 KB |
Output is correct |
3 |
Correct |
1449 ms |
149496 KB |
Output is correct |
4 |
Correct |
1450 ms |
153504 KB |
Output is correct |
5 |
Correct |
1378 ms |
147660 KB |
Output is correct |
6 |
Correct |
1187 ms |
122556 KB |
Output is correct |
7 |
Correct |
2068 ms |
176984 KB |
Output is correct |
8 |
Correct |
2015 ms |
176908 KB |
Output is correct |
9 |
Correct |
1955 ms |
177016 KB |
Output is correct |
10 |
Correct |
1882 ms |
176528 KB |
Output is correct |
11 |
Correct |
1687 ms |
171296 KB |
Output is correct |
12 |
Correct |
1516 ms |
137908 KB |
Output is correct |
13 |
Correct |
2086 ms |
188604 KB |
Output is correct |
14 |
Correct |
2117 ms |
188712 KB |
Output is correct |
15 |
Correct |
2067 ms |
188788 KB |
Output is correct |
16 |
Correct |
1960 ms |
188216 KB |
Output is correct |
17 |
Correct |
1814 ms |
181200 KB |
Output is correct |
18 |
Correct |
1511 ms |
141812 KB |
Output is correct |
19 |
Correct |
2052 ms |
188780 KB |
Output is correct |
20 |
Correct |
2092 ms |
188780 KB |
Output is correct |
21 |
Correct |
2044 ms |
188872 KB |
Output is correct |
22 |
Correct |
1957 ms |
188388 KB |
Output is correct |
23 |
Correct |
1837 ms |
181216 KB |
Output is correct |
24 |
Correct |
1524 ms |
141780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2768 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
2 ms |
2788 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
2 ms |
2900 KB |
Output is correct |
8 |
Correct |
2 ms |
2948 KB |
Output is correct |
9 |
Correct |
2 ms |
2900 KB |
Output is correct |
10 |
Correct |
2 ms |
2900 KB |
Output is correct |
11 |
Correct |
2 ms |
2948 KB |
Output is correct |
12 |
Correct |
2 ms |
2900 KB |
Output is correct |
13 |
Correct |
2 ms |
2948 KB |
Output is correct |
14 |
Correct |
2 ms |
2944 KB |
Output is correct |
15 |
Correct |
2 ms |
2900 KB |
Output is correct |
16 |
Correct |
2 ms |
2900 KB |
Output is correct |
17 |
Correct |
2 ms |
2900 KB |
Output is correct |
18 |
Correct |
2 ms |
2900 KB |
Output is correct |
19 |
Correct |
19 ms |
3912 KB |
Output is correct |
20 |
Correct |
21 ms |
3924 KB |
Output is correct |
21 |
Correct |
24 ms |
3980 KB |
Output is correct |
22 |
Correct |
23 ms |
4092 KB |
Output is correct |
23 |
Correct |
32 ms |
8200 KB |
Output is correct |
24 |
Correct |
41 ms |
9300 KB |
Output is correct |
25 |
Correct |
44 ms |
10060 KB |
Output is correct |
26 |
Correct |
44 ms |
11092 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2816 KB |
Output is correct |
29 |
Correct |
3 ms |
2772 KB |
Output is correct |
30 |
Correct |
16 ms |
3088 KB |
Output is correct |
31 |
Correct |
68 ms |
3976 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
3 ms |
3028 KB |
Output is correct |
34 |
Correct |
2 ms |
3028 KB |
Output is correct |
35 |
Correct |
4 ms |
3088 KB |
Output is correct |
36 |
Correct |
24 ms |
3316 KB |
Output is correct |
37 |
Correct |
109 ms |
4300 KB |
Output is correct |
38 |
Correct |
5 ms |
5460 KB |
Output is correct |
39 |
Correct |
5 ms |
5476 KB |
Output is correct |
40 |
Correct |
7 ms |
5520 KB |
Output is correct |
41 |
Correct |
33 ms |
5844 KB |
Output is correct |
42 |
Correct |
145 ms |
6964 KB |
Output is correct |
43 |
Correct |
61 ms |
55484 KB |
Output is correct |
44 |
Correct |
63 ms |
55564 KB |
Output is correct |
45 |
Correct |
64 ms |
55592 KB |
Output is correct |
46 |
Correct |
103 ms |
55876 KB |
Output is correct |
47 |
Correct |
223 ms |
57140 KB |
Output is correct |
48 |
Correct |
6 ms |
3924 KB |
Output is correct |
49 |
Correct |
34 ms |
4044 KB |
Output is correct |
50 |
Correct |
163 ms |
4596 KB |
Output is correct |
51 |
Correct |
318 ms |
5188 KB |
Output is correct |
52 |
Correct |
20 ms |
15316 KB |
Output is correct |
53 |
Correct |
77 ms |
15400 KB |
Output is correct |
54 |
Correct |
328 ms |
16052 KB |
Output is correct |
55 |
Correct |
657 ms |
16852 KB |
Output is correct |
56 |
Correct |
91 ms |
72908 KB |
Output is correct |
57 |
Correct |
193 ms |
73044 KB |
Output is correct |
58 |
Correct |
573 ms |
73676 KB |
Output is correct |
59 |
Correct |
1102 ms |
74628 KB |
Output is correct |
60 |
Correct |
151 ms |
149592 KB |
Output is correct |
61 |
Correct |
259 ms |
149904 KB |
Output is correct |
62 |
Correct |
752 ms |
150596 KB |
Output is correct |
63 |
Correct |
1337 ms |
151352 KB |
Output is correct |
64 |
Correct |
1401 ms |
151236 KB |
Output is correct |
65 |
Correct |
1475 ms |
148408 KB |
Output is correct |
66 |
Correct |
1548 ms |
151868 KB |
Output is correct |
67 |
Correct |
1449 ms |
149496 KB |
Output is correct |
68 |
Correct |
1450 ms |
153504 KB |
Output is correct |
69 |
Correct |
1378 ms |
147660 KB |
Output is correct |
70 |
Correct |
1187 ms |
122556 KB |
Output is correct |
71 |
Correct |
2068 ms |
176984 KB |
Output is correct |
72 |
Correct |
2015 ms |
176908 KB |
Output is correct |
73 |
Correct |
1955 ms |
177016 KB |
Output is correct |
74 |
Correct |
1882 ms |
176528 KB |
Output is correct |
75 |
Correct |
1687 ms |
171296 KB |
Output is correct |
76 |
Correct |
1516 ms |
137908 KB |
Output is correct |
77 |
Correct |
2086 ms |
188604 KB |
Output is correct |
78 |
Correct |
2117 ms |
188712 KB |
Output is correct |
79 |
Correct |
2067 ms |
188788 KB |
Output is correct |
80 |
Correct |
1960 ms |
188216 KB |
Output is correct |
81 |
Correct |
1814 ms |
181200 KB |
Output is correct |
82 |
Correct |
1511 ms |
141812 KB |
Output is correct |
83 |
Correct |
2052 ms |
188780 KB |
Output is correct |
84 |
Correct |
2092 ms |
188780 KB |
Output is correct |
85 |
Correct |
2044 ms |
188872 KB |
Output is correct |
86 |
Correct |
1957 ms |
188388 KB |
Output is correct |
87 |
Correct |
1837 ms |
181216 KB |
Output is correct |
88 |
Correct |
1524 ms |
141780 KB |
Output is correct |
89 |
Correct |
1503 ms |
149444 KB |
Output is correct |
90 |
Correct |
1727 ms |
161504 KB |
Output is correct |
91 |
Correct |
1889 ms |
173240 KB |
Output is correct |
92 |
Correct |
2013 ms |
176460 KB |
Output is correct |
93 |
Correct |
2062 ms |
180880 KB |
Output is correct |
94 |
Correct |
2101 ms |
183384 KB |
Output is correct |
95 |
Correct |
2071 ms |
187952 KB |
Output is correct |
96 |
Correct |
2067 ms |
187980 KB |
Output is correct |
97 |
Correct |
2072 ms |
188160 KB |
Output is correct |
98 |
Correct |
2084 ms |
188016 KB |
Output is correct |
99 |
Correct |
2095 ms |
187876 KB |
Output is correct |
100 |
Correct |
2089 ms |
188184 KB |
Output is correct |