/**
* author: wxhtzdy
* created: 25.04.2023 16:05:28
**/
#include <bits/stdc++.h>
using namespace std;
struct segtree {
vector<long long> mx;
vector<long long> lzy;
segtree(int n) {
mx.resize(4 * n);
lzy.resize(4 * n);
fill(mx.begin(), mx.end(), 0);
fill(lzy.begin(), lzy.end(), 0);
}
void push(int v) {
if (2 * v + 1 < mx.size()) {
mx[2 * v] += lzy[v];
mx[2 * v + 1] += lzy[v];
lzy[2 * v] += lzy[v];
lzy[2 * v + 1] += lzy[v];
}
lzy[v] = 0;
}
void modify(int v, int l, int r, int ql, int qr, int x) {
if (l > qr || r < ql || l > r) {
return;
}
if (ql <= l && r <= qr) {
lzy[v] += x;
mx[v] += x;
push(v);
return;
}
push(v);
int mid = l + r >> 1;
modify(2 * v, l, mid, ql, qr, x);
modify(2 * v + 1, mid + 1, r, ql, qr, x);
mx[v] = max(mx[2 * v], mx[2 * v + 1]);
}
long long query(int v, int l, int r, int ql, int qr) {
if (l > qr || r < ql || l > r) {
return 0;
}
if (ql <= l && r <= qr) {
return mx[v];
}
push(v);
int mid = l + r >> 1;
long long L = query(2 * v, l, mid, ql, qr);
long long R = query(2 * v + 1, mid + 1, r, ql, qr);
mx[v] = max(mx[2 * v], mx[2 * v + 1]);
return max(L, R);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
long long W;
cin >> n >> q >> W;
vector<vector<pair<int, int>>> g(n);
vector<int> u(n), v(n);
vector<long long> w(n);
for (int i = 0; i < n - 1; i++) {
cin >> u[i] >> v[i] >> w[i];
--u[i]; --v[i];
g[u[i]].emplace_back(v[i], i);
g[v[i]].emplace_back(u[i], i);
}
vector<bool> rem(n);
vector<int> sz(n);
function<void(int, int)> DfsSz = [&](int v, int pv) {
sz[v] = 1;
for (auto& p : g[v]) {
int u = p.first;
if (u == pv || rem[u]) {
continue;
}
DfsSz(u, v);
sz[v] += sz[u];
}
};
function<int(int, int, int)> Cen = [&](int v, int pv, int n) {
for (auto& p : g[v]) {
int u = p.first;
if (u == pv || rem[u] || sz[u] * 2 < n) {
continue;
}
return Cen(u, v, n);
}
return v;
};
int T;
vector<vector<int>> tin(n);
vector<vector<int>> tout(n);
vector<vector<int>> ids(n);
vector<vector<int>> sub(n);
function<void(int, int, int, int)> Dfs = [&](int v, int pv, int cp, int sp) {
tin[v].push_back(++T);
sub[v].push_back(sp);
for (auto& p : g[v]) {
int u = p.first;
int i = p.second;
if (u == pv || rem[u]) {
continue;
}
ids[i].push_back(cp);
Dfs(u, v, cp, (v == cp ? u : sp));
}
tout[v].push_back(T);
};
vector<segtree> st(n, segtree(1));
vector<int> len(n);
vector<vector<int>> ch(n);
vector<int> dep(n);
function<void(int, int)> Decompose = [&](int v, int d) {
DfsSz(v, v);
v = Cen(v, v, sz[v]);
dep[v] = d;
T = -1;
Dfs(v, v, v, v);
len[v] = T;
st[v] = segtree(T + 1);
rem[v] = true;
for (auto& p : g[v]) {
int u = p.first;
if (!rem[u]) {
ch[v].push_back(u);
Decompose(u, d + 1);
}
}
};
Decompose(0, 0);
for (int i = 0; i < n - 1; i++) {
for (int cp : ids[i]) {
if (tin[u[i]][dep[cp]] > tin[v[i]][dep[cp]]) {
swap(u[i], v[i]);
}
st[cp].modify(1, 0, len[cp], tin[v[i]][dep[cp]], tout[v[i]][dep[cp]], w[i]);
}
}
multiset<long long> all;
vector<multiset<long long>> my(n);
auto Calc = [&](int v) {
long long s = 0;
auto it = my[v].end();
for (int rep = 0; rep < 2; rep++) {
if (it == my[v].begin()) {
break;
}
it = prev(it);
s += *it;
}
return s;
};
for (int i = 0; i < n; i++) {
my[i].insert(0);
for (int j : ch[i]) {
my[i].insert(st[i].query(1, 0, len[i], tin[j][dep[i]], tout[j][dep[i]]));
}
all.insert(Calc(i));
}
long long last = 0;
while (q--) {
int d, e;
cin >> d >> e;
d = (d + last) % (n - 1);
e = (e + last) % W;
for (int cp : ids[d]) {
all.erase(all.find(Calc(cp)));
if (tin[u[d]][dep[cp]] > tin[v[d]][dep[cp]]) {
swap(u[d], v[d]);
}
my[cp].erase(my[cp].find(st[cp].query(1, 0, len[cp], tin[sub[v[d]][dep[cp]]][dep[cp]], tout[sub[v[d]][dep[cp]]][dep[cp]])));
st[cp].modify(1, 0, len[cp], tin[v[d]][dep[cp]], tout[v[d]][dep[cp]], e - w[d]);
my[cp].insert(st[cp].query(1, 0, len[cp], tin[sub[v[d]][dep[cp]]][dep[cp]], tout[sub[v[d]][dep[cp]]][dep[cp]]));
all.insert(Calc(cp));
}
w[d] = e;
last = *prev(all.end());
cout << last << '\n';
}
return 0;
}
Compilation message
diameter.cpp: In member function 'void segtree::push(int)':
diameter.cpp:19:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | if (2 * v + 1 < mx.size()) {
| ~~~~~~~~~~^~~~~~~~~~~
diameter.cpp: In member function 'void segtree::modify(int, int, int, int, int, int)':
diameter.cpp:38:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | int mid = l + r >> 1;
| ~~^~~
diameter.cpp: In member function 'long long int segtree::query(int, int, int, int, int)':
diameter.cpp:51:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
51 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
21 ms |
1372 KB |
Output is correct |
20 |
Correct |
23 ms |
1436 KB |
Output is correct |
21 |
Correct |
28 ms |
1532 KB |
Output is correct |
22 |
Correct |
28 ms |
1692 KB |
Output is correct |
23 |
Correct |
47 ms |
5856 KB |
Output is correct |
24 |
Correct |
62 ms |
6888 KB |
Output is correct |
25 |
Correct |
71 ms |
7336 KB |
Output is correct |
26 |
Correct |
66 ms |
8348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
12 ms |
340 KB |
Output is correct |
5 |
Correct |
59 ms |
652 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
3 ms |
596 KB |
Output is correct |
10 |
Correct |
20 ms |
708 KB |
Output is correct |
11 |
Correct |
95 ms |
1148 KB |
Output is correct |
12 |
Correct |
9 ms |
3884 KB |
Output is correct |
13 |
Correct |
10 ms |
3924 KB |
Output is correct |
14 |
Correct |
11 ms |
4004 KB |
Output is correct |
15 |
Correct |
41 ms |
4020 KB |
Output is correct |
16 |
Correct |
149 ms |
4436 KB |
Output is correct |
17 |
Correct |
207 ms |
73104 KB |
Output is correct |
18 |
Correct |
204 ms |
73216 KB |
Output is correct |
19 |
Correct |
205 ms |
73160 KB |
Output is correct |
20 |
Correct |
250 ms |
73140 KB |
Output is correct |
21 |
Correct |
550 ms |
73796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1492 KB |
Output is correct |
2 |
Correct |
39 ms |
1560 KB |
Output is correct |
3 |
Correct |
170 ms |
1832 KB |
Output is correct |
4 |
Correct |
308 ms |
2000 KB |
Output is correct |
5 |
Correct |
65 ms |
15344 KB |
Output is correct |
6 |
Correct |
110 ms |
15436 KB |
Output is correct |
7 |
Correct |
342 ms |
15832 KB |
Output is correct |
8 |
Correct |
602 ms |
16124 KB |
Output is correct |
9 |
Correct |
322 ms |
83464 KB |
Output is correct |
10 |
Correct |
410 ms |
83520 KB |
Output is correct |
11 |
Correct |
775 ms |
83788 KB |
Output is correct |
12 |
Correct |
1271 ms |
84048 KB |
Output is correct |
13 |
Correct |
692 ms |
172896 KB |
Output is correct |
14 |
Correct |
794 ms |
173100 KB |
Output is correct |
15 |
Correct |
1310 ms |
173348 KB |
Output is correct |
16 |
Correct |
1839 ms |
173732 KB |
Output is correct |
17 |
Correct |
3050 ms |
173620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3224 ms |
148808 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
21 ms |
1372 KB |
Output is correct |
20 |
Correct |
23 ms |
1436 KB |
Output is correct |
21 |
Correct |
28 ms |
1532 KB |
Output is correct |
22 |
Correct |
28 ms |
1692 KB |
Output is correct |
23 |
Correct |
47 ms |
5856 KB |
Output is correct |
24 |
Correct |
62 ms |
6888 KB |
Output is correct |
25 |
Correct |
71 ms |
7336 KB |
Output is correct |
26 |
Correct |
66 ms |
8348 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
12 ms |
340 KB |
Output is correct |
31 |
Correct |
59 ms |
652 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
596 KB |
Output is correct |
35 |
Correct |
3 ms |
596 KB |
Output is correct |
36 |
Correct |
20 ms |
708 KB |
Output is correct |
37 |
Correct |
95 ms |
1148 KB |
Output is correct |
38 |
Correct |
9 ms |
3884 KB |
Output is correct |
39 |
Correct |
10 ms |
3924 KB |
Output is correct |
40 |
Correct |
11 ms |
4004 KB |
Output is correct |
41 |
Correct |
41 ms |
4020 KB |
Output is correct |
42 |
Correct |
149 ms |
4436 KB |
Output is correct |
43 |
Correct |
207 ms |
73104 KB |
Output is correct |
44 |
Correct |
204 ms |
73216 KB |
Output is correct |
45 |
Correct |
205 ms |
73160 KB |
Output is correct |
46 |
Correct |
250 ms |
73140 KB |
Output is correct |
47 |
Correct |
550 ms |
73796 KB |
Output is correct |
48 |
Correct |
7 ms |
1492 KB |
Output is correct |
49 |
Correct |
39 ms |
1560 KB |
Output is correct |
50 |
Correct |
170 ms |
1832 KB |
Output is correct |
51 |
Correct |
308 ms |
2000 KB |
Output is correct |
52 |
Correct |
65 ms |
15344 KB |
Output is correct |
53 |
Correct |
110 ms |
15436 KB |
Output is correct |
54 |
Correct |
342 ms |
15832 KB |
Output is correct |
55 |
Correct |
602 ms |
16124 KB |
Output is correct |
56 |
Correct |
322 ms |
83464 KB |
Output is correct |
57 |
Correct |
410 ms |
83520 KB |
Output is correct |
58 |
Correct |
775 ms |
83788 KB |
Output is correct |
59 |
Correct |
1271 ms |
84048 KB |
Output is correct |
60 |
Correct |
692 ms |
172896 KB |
Output is correct |
61 |
Correct |
794 ms |
173100 KB |
Output is correct |
62 |
Correct |
1310 ms |
173348 KB |
Output is correct |
63 |
Correct |
1839 ms |
173732 KB |
Output is correct |
64 |
Correct |
3050 ms |
173620 KB |
Output is correct |
65 |
Incorrect |
3224 ms |
148808 KB |
Output isn't correct |
66 |
Halted |
0 ms |
0 KB |
- |