#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
struct Segtree {
int sz;
vector<ll> vals, lazy;
void push_lazy(int node, int l, int r) {
vals[node] += lazy[node];
if (l != r) {
lazy[node * 2] += lazy[node];
lazy[node * 2 + 1] += lazy[node];
}
lazy[node] = 0;
}
void update(int a, int b, ll val, int node, int l, int r) {
push_lazy(node, l, r);
if (l > b || r < a) return;
if (l >= a && r <= b) {
lazy[node] = val;
push_lazy(node, l, r);
} else {
int mid = (l + r) / 2;
update(a, b, val, node * 2, l, mid);
update(a, b, val, node * 2 + 1, mid + 1, r);
vals[node] = max(vals[node * 2], vals[node * 2 + 1]);
}
}
ll query(int a, int b, int node, int l, int r) {
push_lazy(node, l, r);
if (l > b || r < a) return 0;
if (l >= a && r <= b) return vals[node];
int mid = (l + r) / 2;
return max(query(a, b, node * 2, l, mid),
query(a, b, node * 2 + 1, mid + 1, r));
}
void build(vector<ll> &dists, int node, int l, int r) {
if (l == r)
vals[node] = dists[l];
else {
int mid = (l + r) / 2;
build(dists, node * 2, l, mid);
build(dists, node * 2 + 1, mid + 1, r);
vals[node] = max(vals[node * 2], vals[node * 2 + 1]);
}
}
void init(vector<ll> &dists) {
sz = dists.size();
vals.resize(4 * sz), lazy.resize(4 * sz);
build(dists, 1, 1, sz);
}
} segtree[100001];
ll e_weight[100001];
pair<int, int> e_nodes[100001];
vector<pair<int, int>> graph[100001];
bool processed[100001];
int subtree[100001], c_par[100001], c_level[100001];
int tin[18][100001], tout[18][100001], timer;
vector<int> c_timers[100001];
vector<ll> dists;
multiset<ll> ms_all, ms_centroid[100001];
void get_subtree_sizes(int node, int parent = 0) {
subtree[node] = 1;
for (pair<int, int> i : graph[node])
if (i.first != parent && !processed[i.first]) {
get_subtree_sizes(i.first, node);
subtree[node] += subtree[i.first];
}
}
int get_centroid(int node, int parent, int tree_size) {
for (pair<int, int> i : graph[node])
if (i.first != parent && !processed[i.first] &&
subtree[i.first] > tree_size)
return get_centroid(i.first, node, tree_size);
return node;
}
ll get_dists(int node, int parent, int level, ll curr_dist) {
tin[level][node] = ++timer;
dists.push_back(curr_dist);
ll ret = curr_dist;
for (pair<int, int> i : graph[node])
if (i.first != parent && !processed[i.first])
ret = max(ret, get_dists(i.first, node, level,
curr_dist + e_weight[i.second]));
tout[level][node] = timer;
return ret;
}
void centroid_decomp(int node = 1, int prv_centroid = 0, int level = 0) {
get_subtree_sizes(node);
int centroid = get_centroid(node, 0, subtree[node] / 2);
c_par[centroid] = prv_centroid;
c_level[centroid] = level;
dists.clear();
timer = 0;
ms_centroid[centroid].insert(0), ms_centroid[centroid].insert(0);
dists.push_back(0);
for (pair<int, int> i : graph[centroid])
if (!processed[i.first]) {
ms_centroid[centroid].insert(
get_dists(i.first, centroid, level, e_weight[i.second]));
c_timers[centroid].push_back(timer);
}
tin[level][centroid] = 0, tout[level][centroid] = timer + 1;
segtree[centroid].init(dists);
ms_all.insert(*ms_centroid[centroid].rbegin() +
*next(ms_centroid[centroid].rbegin()));
processed[centroid] = true;
for (pair<int, int> i : graph[centroid])
if (!processed[i.first]) centroid_decomp(i.first, centroid, level + 1);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, q;
ll w;
cin >> n >> q >> w;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v >> e_weight[i];
graph[u].push_back({v, i});
graph[v].push_back({u, i});
e_nodes[i] = {u, v};
}
centroid_decomp();
ll last = 0;
while (q--) {
int d;
ll e;
cin >> d >> e;
d = (d + last) % (n - 1), e = (e + last) % w;
ll delta = e - e_weight[d];
int u = e_nodes[d].first, v = e_nodes[d].second;
int node = (c_level[u] < c_level[v] ? u : v);
while (node) {
auto lb = lower_bound(
c_timers[node].begin(), c_timers[node].end(),
min(tout[c_level[node]][u], tout[c_level[node]][v]));
ms_all.erase(ms_all.find(*ms_centroid[node].rbegin() +
*next(ms_centroid[node].rbegin())));
ms_centroid[node].erase(ms_centroid[node].find(segtree[node].query(
*prev(lb) + 1, *lb, 1, 1, segtree[node].sz)));
segtree[node].update(
max(tin[c_level[node]][u], tin[c_level[node]][v]),
min(tout[c_level[node]][u], tout[c_level[node]][v]), delta, 1,
1, segtree[node].sz);
ms_centroid[node].insert(segtree[node].query(*prev(lb) + 1, *lb, 1,
1, segtree[node].sz));
ms_all.insert(*ms_centroid[node].rbegin() +
*next(ms_centroid[node].rbegin()));
node = c_par[node];
}
e_weight[d] = e;
last = *ms_all.rbegin();
cout << last << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
15340 KB |
Output is correct |
2 |
Correct |
10 ms |
15340 KB |
Output is correct |
3 |
Correct |
10 ms |
15340 KB |
Output is correct |
4 |
Correct |
10 ms |
15340 KB |
Output is correct |
5 |
Correct |
10 ms |
15340 KB |
Output is correct |
6 |
Correct |
10 ms |
15340 KB |
Output is correct |
7 |
Correct |
10 ms |
15212 KB |
Output is correct |
8 |
Correct |
10 ms |
15340 KB |
Output is correct |
9 |
Correct |
10 ms |
15340 KB |
Output is correct |
10 |
Correct |
10 ms |
15340 KB |
Output is correct |
11 |
Correct |
10 ms |
15340 KB |
Output is correct |
12 |
Correct |
10 ms |
15276 KB |
Output is correct |
13 |
Correct |
10 ms |
15340 KB |
Output is correct |
14 |
Correct |
10 ms |
15340 KB |
Output is correct |
15 |
Correct |
10 ms |
15360 KB |
Output is correct |
16 |
Correct |
10 ms |
15340 KB |
Output is correct |
17 |
Correct |
11 ms |
15340 KB |
Output is correct |
18 |
Correct |
11 ms |
15340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
15340 KB |
Output is correct |
2 |
Correct |
10 ms |
15340 KB |
Output is correct |
3 |
Correct |
10 ms |
15340 KB |
Output is correct |
4 |
Correct |
10 ms |
15340 KB |
Output is correct |
5 |
Correct |
10 ms |
15340 KB |
Output is correct |
6 |
Correct |
10 ms |
15340 KB |
Output is correct |
7 |
Correct |
10 ms |
15212 KB |
Output is correct |
8 |
Correct |
10 ms |
15340 KB |
Output is correct |
9 |
Correct |
10 ms |
15340 KB |
Output is correct |
10 |
Correct |
10 ms |
15340 KB |
Output is correct |
11 |
Correct |
10 ms |
15340 KB |
Output is correct |
12 |
Correct |
10 ms |
15276 KB |
Output is correct |
13 |
Correct |
10 ms |
15340 KB |
Output is correct |
14 |
Correct |
10 ms |
15340 KB |
Output is correct |
15 |
Correct |
10 ms |
15360 KB |
Output is correct |
16 |
Correct |
10 ms |
15340 KB |
Output is correct |
17 |
Correct |
11 ms |
15340 KB |
Output is correct |
18 |
Correct |
11 ms |
15340 KB |
Output is correct |
19 |
Correct |
35 ms |
16108 KB |
Output is correct |
20 |
Correct |
39 ms |
16236 KB |
Output is correct |
21 |
Correct |
42 ms |
16364 KB |
Output is correct |
22 |
Correct |
46 ms |
16492 KB |
Output is correct |
23 |
Correct |
57 ms |
19692 KB |
Output is correct |
24 |
Correct |
74 ms |
20388 KB |
Output is correct |
25 |
Correct |
82 ms |
20716 KB |
Output is correct |
26 |
Correct |
86 ms |
21504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
15232 KB |
Output is correct |
2 |
Correct |
10 ms |
15212 KB |
Output is correct |
3 |
Correct |
12 ms |
15340 KB |
Output is correct |
4 |
Correct |
24 ms |
15468 KB |
Output is correct |
5 |
Correct |
80 ms |
16236 KB |
Output is correct |
6 |
Correct |
10 ms |
15232 KB |
Output is correct |
7 |
Correct |
10 ms |
15468 KB |
Output is correct |
8 |
Correct |
11 ms |
15468 KB |
Output is correct |
9 |
Correct |
13 ms |
15468 KB |
Output is correct |
10 |
Correct |
34 ms |
15724 KB |
Output is correct |
11 |
Correct |
126 ms |
16620 KB |
Output is correct |
12 |
Correct |
20 ms |
17644 KB |
Output is correct |
13 |
Correct |
18 ms |
17516 KB |
Output is correct |
14 |
Correct |
19 ms |
17516 KB |
Output is correct |
15 |
Correct |
53 ms |
17772 KB |
Output is correct |
16 |
Correct |
193 ms |
18540 KB |
Output is correct |
17 |
Correct |
168 ms |
59744 KB |
Output is correct |
18 |
Correct |
167 ms |
59744 KB |
Output is correct |
19 |
Correct |
170 ms |
59744 KB |
Output is correct |
20 |
Correct |
236 ms |
59872 KB |
Output is correct |
21 |
Correct |
602 ms |
60256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
16236 KB |
Output is correct |
2 |
Correct |
52 ms |
16364 KB |
Output is correct |
3 |
Correct |
220 ms |
17004 KB |
Output is correct |
4 |
Correct |
408 ms |
17388 KB |
Output is correct |
5 |
Correct |
45 ms |
26860 KB |
Output is correct |
6 |
Correct |
123 ms |
26860 KB |
Output is correct |
7 |
Correct |
484 ms |
27372 KB |
Output is correct |
8 |
Correct |
920 ms |
28012 KB |
Output is correct |
9 |
Correct |
185 ms |
79980 KB |
Output is correct |
10 |
Correct |
326 ms |
80108 KB |
Output is correct |
11 |
Correct |
981 ms |
80364 KB |
Output is correct |
12 |
Correct |
1826 ms |
80604 KB |
Output is correct |
13 |
Correct |
369 ms |
151008 KB |
Output is correct |
14 |
Correct |
562 ms |
151008 KB |
Output is correct |
15 |
Correct |
1377 ms |
151392 KB |
Output is correct |
16 |
Correct |
2423 ms |
151648 KB |
Output is correct |
17 |
Correct |
4487 ms |
151836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3588 ms |
128096 KB |
Output is correct |
2 |
Correct |
3699 ms |
131020 KB |
Output is correct |
3 |
Correct |
3548 ms |
129564 KB |
Output is correct |
4 |
Correct |
3712 ms |
131436 KB |
Output is correct |
5 |
Correct |
3614 ms |
125708 KB |
Output is correct |
6 |
Correct |
3433 ms |
102776 KB |
Output is correct |
7 |
Correct |
4804 ms |
155008 KB |
Output is correct |
8 |
Correct |
4808 ms |
155080 KB |
Output is correct |
9 |
Correct |
4730 ms |
155404 KB |
Output is correct |
10 |
Correct |
4769 ms |
154812 KB |
Output is correct |
11 |
Correct |
4773 ms |
149120 KB |
Output is correct |
12 |
Correct |
4531 ms |
117180 KB |
Output is correct |
13 |
Correct |
4981 ms |
167584 KB |
Output is correct |
14 |
Execution timed out |
5031 ms |
167492 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
15340 KB |
Output is correct |
2 |
Correct |
10 ms |
15340 KB |
Output is correct |
3 |
Correct |
10 ms |
15340 KB |
Output is correct |
4 |
Correct |
10 ms |
15340 KB |
Output is correct |
5 |
Correct |
10 ms |
15340 KB |
Output is correct |
6 |
Correct |
10 ms |
15340 KB |
Output is correct |
7 |
Correct |
10 ms |
15212 KB |
Output is correct |
8 |
Correct |
10 ms |
15340 KB |
Output is correct |
9 |
Correct |
10 ms |
15340 KB |
Output is correct |
10 |
Correct |
10 ms |
15340 KB |
Output is correct |
11 |
Correct |
10 ms |
15340 KB |
Output is correct |
12 |
Correct |
10 ms |
15276 KB |
Output is correct |
13 |
Correct |
10 ms |
15340 KB |
Output is correct |
14 |
Correct |
10 ms |
15340 KB |
Output is correct |
15 |
Correct |
10 ms |
15360 KB |
Output is correct |
16 |
Correct |
10 ms |
15340 KB |
Output is correct |
17 |
Correct |
11 ms |
15340 KB |
Output is correct |
18 |
Correct |
11 ms |
15340 KB |
Output is correct |
19 |
Correct |
35 ms |
16108 KB |
Output is correct |
20 |
Correct |
39 ms |
16236 KB |
Output is correct |
21 |
Correct |
42 ms |
16364 KB |
Output is correct |
22 |
Correct |
46 ms |
16492 KB |
Output is correct |
23 |
Correct |
57 ms |
19692 KB |
Output is correct |
24 |
Correct |
74 ms |
20388 KB |
Output is correct |
25 |
Correct |
82 ms |
20716 KB |
Output is correct |
26 |
Correct |
86 ms |
21504 KB |
Output is correct |
27 |
Correct |
11 ms |
15232 KB |
Output is correct |
28 |
Correct |
10 ms |
15212 KB |
Output is correct |
29 |
Correct |
12 ms |
15340 KB |
Output is correct |
30 |
Correct |
24 ms |
15468 KB |
Output is correct |
31 |
Correct |
80 ms |
16236 KB |
Output is correct |
32 |
Correct |
10 ms |
15232 KB |
Output is correct |
33 |
Correct |
10 ms |
15468 KB |
Output is correct |
34 |
Correct |
11 ms |
15468 KB |
Output is correct |
35 |
Correct |
13 ms |
15468 KB |
Output is correct |
36 |
Correct |
34 ms |
15724 KB |
Output is correct |
37 |
Correct |
126 ms |
16620 KB |
Output is correct |
38 |
Correct |
20 ms |
17644 KB |
Output is correct |
39 |
Correct |
18 ms |
17516 KB |
Output is correct |
40 |
Correct |
19 ms |
17516 KB |
Output is correct |
41 |
Correct |
53 ms |
17772 KB |
Output is correct |
42 |
Correct |
193 ms |
18540 KB |
Output is correct |
43 |
Correct |
168 ms |
59744 KB |
Output is correct |
44 |
Correct |
167 ms |
59744 KB |
Output is correct |
45 |
Correct |
170 ms |
59744 KB |
Output is correct |
46 |
Correct |
236 ms |
59872 KB |
Output is correct |
47 |
Correct |
602 ms |
60256 KB |
Output is correct |
48 |
Correct |
16 ms |
16236 KB |
Output is correct |
49 |
Correct |
52 ms |
16364 KB |
Output is correct |
50 |
Correct |
220 ms |
17004 KB |
Output is correct |
51 |
Correct |
408 ms |
17388 KB |
Output is correct |
52 |
Correct |
45 ms |
26860 KB |
Output is correct |
53 |
Correct |
123 ms |
26860 KB |
Output is correct |
54 |
Correct |
484 ms |
27372 KB |
Output is correct |
55 |
Correct |
920 ms |
28012 KB |
Output is correct |
56 |
Correct |
185 ms |
79980 KB |
Output is correct |
57 |
Correct |
326 ms |
80108 KB |
Output is correct |
58 |
Correct |
981 ms |
80364 KB |
Output is correct |
59 |
Correct |
1826 ms |
80604 KB |
Output is correct |
60 |
Correct |
369 ms |
151008 KB |
Output is correct |
61 |
Correct |
562 ms |
151008 KB |
Output is correct |
62 |
Correct |
1377 ms |
151392 KB |
Output is correct |
63 |
Correct |
2423 ms |
151648 KB |
Output is correct |
64 |
Correct |
4487 ms |
151836 KB |
Output is correct |
65 |
Correct |
3588 ms |
128096 KB |
Output is correct |
66 |
Correct |
3699 ms |
131020 KB |
Output is correct |
67 |
Correct |
3548 ms |
129564 KB |
Output is correct |
68 |
Correct |
3712 ms |
131436 KB |
Output is correct |
69 |
Correct |
3614 ms |
125708 KB |
Output is correct |
70 |
Correct |
3433 ms |
102776 KB |
Output is correct |
71 |
Correct |
4804 ms |
155008 KB |
Output is correct |
72 |
Correct |
4808 ms |
155080 KB |
Output is correct |
73 |
Correct |
4730 ms |
155404 KB |
Output is correct |
74 |
Correct |
4769 ms |
154812 KB |
Output is correct |
75 |
Correct |
4773 ms |
149120 KB |
Output is correct |
76 |
Correct |
4531 ms |
117180 KB |
Output is correct |
77 |
Correct |
4981 ms |
167584 KB |
Output is correct |
78 |
Execution timed out |
5031 ms |
167492 KB |
Time limit exceeded |
79 |
Halted |
0 ms |
0 KB |
- |