#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
#define long unsigned long
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define lb lower_bound
#define ub upper_bound
#define sz(v) int((v).size())
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
const int N = 1e5+7;
set <pair <int, ll>> graph[N];
vector <pair <pii, ll>> edges;
ll df, vf;
int n, q;
ll w;
void find_furthest(int v, int p, ll dist = 0) {
if (dist > df) {
df = dist;
vf = v;
}
for (auto to : graph[v]) {
if (to.first == p) continue;
find_furthest(to.first, v, dist+to.second);
}
}
void subtask3() {
for (auto to : edges) {
if (to.first.first != 1) {
return;
}
}
set <pair <ll, int>, greater <pair <ll, int>>> st;
for (auto to : edges) {
st.insert(mp(to.second, to.first.second));
}
ll last = 0;
while (q--) {
ll d, e;
cin >> d >> e;
d = (d + last) % (n - 1);
e = (e + last) % w;
auto &c = edges[d].second;
auto b = edges[d].first.second;
st.erase(st.find(mp(c, b)));
c = e;
st.insert(mp(c, b));
ll ans = 0;
ll cnt = 0;
for (auto to : st) {
ans += to.first;
if (++cnt > 1) break;
}
cout << ans << endl;
last = ans;
}
exit(0);
}
void subtask4() {
vector <ll> ans_for(n+1);
vector <pll> mx_child(n+1), dist_child(n+1);
set <pair <ll, int>, greater <pair <ll, int>>> st;
for (auto to : edges) {
auto a = to.first.first;
auto b = to.first.second;
if (!(a*2 != b || a*2+1 != b)) {
return;
}
if (a*2 == b) dist_child[a].first = to.second;
else dist_child[a].second = to.second;
}
for (int i = 1; i <= n; i++) {
if (sz(graph[i]) == 1) {
int x = i;
while (x) {
if (x*2 <= n) mx_child[x].first = max(mx_child[x*2].first, mx_child[x*2].second)+dist_child[x].first;
if (x*2+1 <= n) mx_child[x].second = max(mx_child[x*2+1].first, mx_child[x*2+1].second)+dist_child[x].second;
x >>= 1;
}
}
}
for (int i = 1; i <= n; i++) {
st.insert(mp(mx_child[i].first+mx_child[i].second, i));
}
ll last = 0;
while (q--) {
ll d, e;
cin >> d >> e;
d = (d + last) % (n - 1);
e = (e + last) % w;
auto a = edges[d].first.first, b = edges[d].first.second;
int x = (b >> 1);
if (a*2 == b) dist_child[a].first = e;
else dist_child[a].second = e;
while (x) {
st.erase(st.find(mp(mx_child[x].first+mx_child[x].second, x)));
if (x*2 <= n) mx_child[x].first = max(mx_child[x*2].first, mx_child[x*2].second)+dist_child[x].first;
if (x*2+1 <= n) mx_child[x].second = max(mx_child[x*2+1].first, mx_child[x*2+1].second)+dist_child[x].second;
st.insert(mp(mx_child[x].first+mx_child[x].second, x));
x >>= 1;
}
cout << (*st.begin()).first << endl;
last = (*st.begin()).first;
}
exit(0);
}
ll tree[N*4], lz[N*4];
int depth[N], timer;
pii range[N];
void push(int v, int l, int r) {
if (lz[v] != 0) {
tree[v] += lz[v];
if (l != r) {
lz[v] += lz[v*2];
lz[v] += lz[v*2+1];
}
lz[v] = 0;
}
}
void update(int v, int l, int r, int ql, int qr, ll val) {
push(v, l, r);
if (ql > r || l > qr) return;
if (ql <= l && r <= qr) {
lz[v] += val;
push(v, l, r);
return;
}
int mid = (l+r) >> 1;
update(v*2, l, mid, ql, qr, val);
update(v*2+1, mid+1, r, ql, qr, val);
tree[v] = max(tree[v*2], tree[v*2+1]);
}
ll get(int v, int l, int r, int ql, int qr) {
push(v, l, r);
if (ql > r || l > qr) return -9e18;
if (ql <= l && r <= qr) return tree[v];
int mid = (l+r) >> 1;
return max(get(v*2, l, mid, ql, qr), get(v*2+1, mid+1, r, ql, qr));
}
void dfs(int v = 1, int p = 1, ll dist = 0) {
range[v].first = ++timer;
update(1, 1, n, timer, timer, dist);
for (auto to : graph[v]) {
if (to.first == p) continue;
depth[to.first] = depth[v] + 1;
dfs(to.first, v, dist+to.second);
}
range[v].second = timer;
}
void solve() {
cin >> n >> q >> w;
for (int i = 0; i < n-1; i++) {
int a, b;
ll c;
cin >> a >> b >> c;
if (a > b) swap(a, b);
edges.pb(mp(mp(a, b), c));
graph[a].insert(mp(b, c));
graph[b].insert(mp(a, c));
}
if (n <= 5000) {
ll last = 0;
while (q--) {
ll d, e;
cin >> d >> e;
d = (d + last) % (n - 1);
e = (e + last) % w;
auto a = edges[d].first.first, b = edges[d].first.second;
auto &c = edges[d].second;
graph[a].erase(graph[a].find(mp(b, c)));
graph[b].erase(graph[b].find(mp(a, c)));
c = e;
graph[a].insert(mp(b, c));
graph[b].insert(mp(a, c));
df = vf = 0;
find_furthest(1, 1);
df = 0;
find_furthest(vf, vf);
cout << df << endl;
last = df;
}
return;
}
subtask3();
subtask4();
dfs();
ll last = 0;
while (q--) {
ll d, e;
cin >> d >> e;
d = (d + last) % (n - 1);
e = (e + last) % w;
auto a = edges[d].first.first, b = edges[d].first.second;
auto &c = edges[d].second;
if (depth[a] < depth[b]) swap(a, b);
update(1, 1, n, range[b].first, range[b].second, e-c);
c = e;
auto res = get(1, 1, n, 1, n);
cout << res << endl;
last = res;
}
}
int main() {
do_not_disturb
int t = 1;
//~ cin >> t;
while (t--) {
solve();
}
return 0;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
5 ms |
5008 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
4 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
5 ms |
5008 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
4 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
224 ms |
5200 KB |
Output is correct |
20 |
Correct |
257 ms |
5284 KB |
Output is correct |
21 |
Correct |
284 ms |
5168 KB |
Output is correct |
22 |
Correct |
286 ms |
5232 KB |
Output is correct |
23 |
Correct |
1761 ms |
5852 KB |
Output is correct |
24 |
Correct |
1872 ms |
5836 KB |
Output is correct |
25 |
Correct |
2017 ms |
5804 KB |
Output is correct |
26 |
Correct |
2461 ms |
6120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
5 ms |
4948 KB |
Output is correct |
4 |
Correct |
20 ms |
5108 KB |
Output is correct |
5 |
Correct |
89 ms |
5428 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
6 ms |
5076 KB |
Output is correct |
9 |
Correct |
40 ms |
5076 KB |
Output is correct |
10 |
Correct |
312 ms |
5228 KB |
Output is correct |
11 |
Correct |
1581 ms |
5740 KB |
Output is correct |
12 |
Correct |
6 ms |
5972 KB |
Output is correct |
13 |
Correct |
6 ms |
6036 KB |
Output is correct |
14 |
Correct |
7 ms |
5972 KB |
Output is correct |
15 |
Correct |
16 ms |
6124 KB |
Output is correct |
16 |
Correct |
55 ms |
6488 KB |
Output is correct |
17 |
Correct |
112 ms |
25416 KB |
Output is correct |
18 |
Correct |
91 ms |
25376 KB |
Output is correct |
19 |
Correct |
91 ms |
25308 KB |
Output is correct |
20 |
Correct |
133 ms |
25380 KB |
Output is correct |
21 |
Correct |
232 ms |
25872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
5176 KB |
Output is correct |
2 |
Correct |
300 ms |
5212 KB |
Output is correct |
3 |
Correct |
1485 ms |
5520 KB |
Output is correct |
4 |
Correct |
2950 ms |
5740 KB |
Output is correct |
5 |
Correct |
11 ms |
7376 KB |
Output is correct |
6 |
Correct |
29 ms |
7752 KB |
Output is correct |
7 |
Correct |
94 ms |
7736 KB |
Output is correct |
8 |
Correct |
179 ms |
7960 KB |
Output is correct |
9 |
Correct |
42 ms |
17160 KB |
Output is correct |
10 |
Correct |
66 ms |
17216 KB |
Output is correct |
11 |
Correct |
148 ms |
17464 KB |
Output is correct |
12 |
Correct |
250 ms |
17728 KB |
Output is correct |
13 |
Correct |
80 ms |
29224 KB |
Output is correct |
14 |
Correct |
99 ms |
29336 KB |
Output is correct |
15 |
Correct |
204 ms |
29496 KB |
Output is correct |
16 |
Correct |
336 ms |
29880 KB |
Output is correct |
17 |
Correct |
729 ms |
29800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
726 ms |
30896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
5 ms |
5008 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
4 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
224 ms |
5200 KB |
Output is correct |
20 |
Correct |
257 ms |
5284 KB |
Output is correct |
21 |
Correct |
284 ms |
5168 KB |
Output is correct |
22 |
Correct |
286 ms |
5232 KB |
Output is correct |
23 |
Correct |
1761 ms |
5852 KB |
Output is correct |
24 |
Correct |
1872 ms |
5836 KB |
Output is correct |
25 |
Correct |
2017 ms |
5804 KB |
Output is correct |
26 |
Correct |
2461 ms |
6120 KB |
Output is correct |
27 |
Correct |
3 ms |
4948 KB |
Output is correct |
28 |
Correct |
3 ms |
4948 KB |
Output is correct |
29 |
Correct |
5 ms |
4948 KB |
Output is correct |
30 |
Correct |
20 ms |
5108 KB |
Output is correct |
31 |
Correct |
89 ms |
5428 KB |
Output is correct |
32 |
Correct |
3 ms |
4948 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
6 ms |
5076 KB |
Output is correct |
35 |
Correct |
40 ms |
5076 KB |
Output is correct |
36 |
Correct |
312 ms |
5228 KB |
Output is correct |
37 |
Correct |
1581 ms |
5740 KB |
Output is correct |
38 |
Correct |
6 ms |
5972 KB |
Output is correct |
39 |
Correct |
6 ms |
6036 KB |
Output is correct |
40 |
Correct |
7 ms |
5972 KB |
Output is correct |
41 |
Correct |
16 ms |
6124 KB |
Output is correct |
42 |
Correct |
55 ms |
6488 KB |
Output is correct |
43 |
Correct |
112 ms |
25416 KB |
Output is correct |
44 |
Correct |
91 ms |
25376 KB |
Output is correct |
45 |
Correct |
91 ms |
25308 KB |
Output is correct |
46 |
Correct |
133 ms |
25380 KB |
Output is correct |
47 |
Correct |
232 ms |
25872 KB |
Output is correct |
48 |
Correct |
30 ms |
5176 KB |
Output is correct |
49 |
Correct |
300 ms |
5212 KB |
Output is correct |
50 |
Correct |
1485 ms |
5520 KB |
Output is correct |
51 |
Correct |
2950 ms |
5740 KB |
Output is correct |
52 |
Correct |
11 ms |
7376 KB |
Output is correct |
53 |
Correct |
29 ms |
7752 KB |
Output is correct |
54 |
Correct |
94 ms |
7736 KB |
Output is correct |
55 |
Correct |
179 ms |
7960 KB |
Output is correct |
56 |
Correct |
42 ms |
17160 KB |
Output is correct |
57 |
Correct |
66 ms |
17216 KB |
Output is correct |
58 |
Correct |
148 ms |
17464 KB |
Output is correct |
59 |
Correct |
250 ms |
17728 KB |
Output is correct |
60 |
Correct |
80 ms |
29224 KB |
Output is correct |
61 |
Correct |
99 ms |
29336 KB |
Output is correct |
62 |
Correct |
204 ms |
29496 KB |
Output is correct |
63 |
Correct |
336 ms |
29880 KB |
Output is correct |
64 |
Correct |
729 ms |
29800 KB |
Output is correct |
65 |
Incorrect |
726 ms |
30896 KB |
Output isn't correct |
66 |
Halted |
0 ms |
0 KB |
- |