#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<int, ll> pil;
#define ff first
#define ss second
const int N = 1e5 + 9;
int n, q;
ll w, c;
vector<int> g[N];
struct Edge {
int u, v;
ll w;
int operator()(int x) { return u ^ v ^ x; }
} e[N];
namespace subtask12 {
vector<ll> a[N];
void dfs(int u, int p) {
a[u].assign(2, 0);
for (int id : g[u]) if (p != id) {
int v = e[id](u);
dfs(v, id);
a[u].push_back(a[v][0] + e[id].w);
}
sort(all(a[u]), greater<ll>());
}
ll gogo() {
dfs(1, 0);
ll ans(0);
for (int i = 1; i <= n; ++i)
ans = max(ans, a[i][0] + a[i][1]);
return ans;
}
void solve() {
int d;
ll ee, last(0); while (q--) {
cin >> d >> ee;
d = (d + last) % (n - 1);
ee = (ee + last) % w;
e[d + 1].w = ee;
last = gogo();
cout << last << '\n';
}
}
}
namespace subtask3 {
void solve() {
multiset<int, greater<int>> s;
for (int i = 1; i < n; ++i)
s.insert(e[i].w);
int d, ee, last(0); while (q--) {
cin >> d >> ee;
d = (d + last) % (n - 1);
ee = (ee + last) % w;
s.erase(s.find(e[d + 1].w));
s.insert(e[d + 1].w = ee);
last = *s.begin() + *next(s.begin());
cout << last << '\n';
}
}
}
namespace subtask4 {
bool check() {
for (int i = 1; i < n; ++i)
if (e[i].v / 2 != e[i].u) return false;
return true;
}
struct Node {
ll path, diam;
} seg[1 << 18];
ll wei[N];
void build(int u) {
if (u > n) return;
if (u * 2 > n) seg[u].path = seg[u].diam = wei[u];
else {
int u1 = u << 1, u2 = u1 | 1;
build(u1);
build(u2);
seg[u].path = max(seg[u1].path, seg[u2].path) + wei[u];
seg[u].diam = max({ seg[u1].diam, seg[u2].diam, seg[u1].path + seg[u2].path });
}
}
void upd(int u) {
if (u * 2 > n) seg[u].path = seg[u].diam = wei[u];
else {
int u1 = u << 1, u2 = u1 | 1;
seg[u].path = max(seg[u1].path, seg[u2].path) + wei[u];
}
for (u >>= 1; u; u >>= 1) {
int u1 = u << 1, u2 = u1 | 1;
seg[u].path = max(seg[u1].path, seg[u2].path) + wei[u];
seg[u].diam = max({ seg[u1].diam, seg[u2].diam, seg[u1].path + seg[u2].path });
}
}
void solve() {
wei[1] = 0;
for (int i = 1; i < n; ++i) wei[e[i].v] = e[i].w;
build(1);
int d;
ll ee, last(0); while (q--) {
cin >> d >> ee;
d = (d + last) % (n - 1);
ee = (ee + last) % w;
wei[e[d + 1].v] = ee;
upd(e[d + 1].v);
last = seg[1].diam;
cout << last << '\n';
}
}
}
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n >> q >> w;
for (int i = 1, u, v; i < n; ++i) {
cin >> e[i].u >> e[i].v >> e[i].w;
if (e[i].u > e[i].v) swap(e[i].u, e[i].v);
g[e[i].u].push_back(i);
g[e[i].v].push_back(i);
}
if (n <= 5000 && q <= 5000) subtask12::solve();
else if (g[1].size() == n - 1) subtask3::solve();
else if (subtask4::check()) subtask4::solve();
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
Compilation message
diameter.cpp: In function 'int main()':
diameter.cpp:136:21: warning: unused variable 'u' [-Wunused-variable]
136 | for (int i = 1, u, v; i < n; ++i) {
| ^
diameter.cpp:136:24: warning: unused variable 'v' [-Wunused-variable]
136 | for (int i = 1, u, v; i < n; ++i) {
| ^
diameter.cpp:144:26: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
144 | else if (g[1].size() == n - 1) subtask3::solve();
| ~~~~~~~~~~~~^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5008 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4968 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
5028 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 ms |
5020 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5008 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4968 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
5028 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 ms |
5020 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
196 ms |
5116 KB |
Output is correct |
20 |
Correct |
213 ms |
5116 KB |
Output is correct |
21 |
Correct |
194 ms |
5132 KB |
Output is correct |
22 |
Correct |
195 ms |
5208 KB |
Output is correct |
23 |
Correct |
1382 ms |
5500 KB |
Output is correct |
24 |
Correct |
1383 ms |
5604 KB |
Output is correct |
25 |
Correct |
1420 ms |
5656 KB |
Output is correct |
26 |
Correct |
1426 ms |
6020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
5 ms |
4940 KB |
Output is correct |
4 |
Correct |
8 ms |
5068 KB |
Output is correct |
5 |
Correct |
34 ms |
5444 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
10 ms |
5072 KB |
Output is correct |
9 |
Correct |
58 ms |
5072 KB |
Output is correct |
10 |
Correct |
11 ms |
5068 KB |
Output is correct |
11 |
Correct |
37 ms |
5472 KB |
Output is correct |
12 |
Correct |
4 ms |
5452 KB |
Output is correct |
13 |
Correct |
5 ms |
5520 KB |
Output is correct |
14 |
Correct |
7 ms |
5452 KB |
Output is correct |
15 |
Correct |
13 ms |
5536 KB |
Output is correct |
16 |
Correct |
54 ms |
5948 KB |
Output is correct |
17 |
Correct |
56 ms |
14736 KB |
Output is correct |
18 |
Correct |
53 ms |
14760 KB |
Output is correct |
19 |
Correct |
58 ms |
14796 KB |
Output is correct |
20 |
Correct |
78 ms |
16284 KB |
Output is correct |
21 |
Correct |
141 ms |
17596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
5068 KB |
Output is correct |
2 |
Correct |
6 ms |
5068 KB |
Output is correct |
3 |
Correct |
17 ms |
5324 KB |
Output is correct |
4 |
Correct |
29 ms |
5580 KB |
Output is correct |
5 |
Correct |
6 ms |
5708 KB |
Output is correct |
6 |
Correct |
12 ms |
5756 KB |
Output is correct |
7 |
Correct |
19 ms |
5964 KB |
Output is correct |
8 |
Correct |
33 ms |
6228 KB |
Output is correct |
9 |
Correct |
19 ms |
8556 KB |
Output is correct |
10 |
Correct |
22 ms |
8520 KB |
Output is correct |
11 |
Correct |
34 ms |
8756 KB |
Output is correct |
12 |
Correct |
48 ms |
9076 KB |
Output is correct |
13 |
Correct |
45 ms |
11972 KB |
Output is correct |
14 |
Correct |
42 ms |
12088 KB |
Output is correct |
15 |
Correct |
52 ms |
12260 KB |
Output is correct |
16 |
Correct |
66 ms |
12600 KB |
Output is correct |
17 |
Correct |
64 ms |
12612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
42 ms |
9744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5008 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4968 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
5028 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 ms |
5020 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
196 ms |
5116 KB |
Output is correct |
20 |
Correct |
213 ms |
5116 KB |
Output is correct |
21 |
Correct |
194 ms |
5132 KB |
Output is correct |
22 |
Correct |
195 ms |
5208 KB |
Output is correct |
23 |
Correct |
1382 ms |
5500 KB |
Output is correct |
24 |
Correct |
1383 ms |
5604 KB |
Output is correct |
25 |
Correct |
1420 ms |
5656 KB |
Output is correct |
26 |
Correct |
1426 ms |
6020 KB |
Output is correct |
27 |
Correct |
3 ms |
4940 KB |
Output is correct |
28 |
Correct |
3 ms |
4940 KB |
Output is correct |
29 |
Correct |
5 ms |
4940 KB |
Output is correct |
30 |
Correct |
8 ms |
5068 KB |
Output is correct |
31 |
Correct |
34 ms |
5444 KB |
Output is correct |
32 |
Correct |
3 ms |
4940 KB |
Output is correct |
33 |
Correct |
3 ms |
4940 KB |
Output is correct |
34 |
Correct |
10 ms |
5072 KB |
Output is correct |
35 |
Correct |
58 ms |
5072 KB |
Output is correct |
36 |
Correct |
11 ms |
5068 KB |
Output is correct |
37 |
Correct |
37 ms |
5472 KB |
Output is correct |
38 |
Correct |
4 ms |
5452 KB |
Output is correct |
39 |
Correct |
5 ms |
5520 KB |
Output is correct |
40 |
Correct |
7 ms |
5452 KB |
Output is correct |
41 |
Correct |
13 ms |
5536 KB |
Output is correct |
42 |
Correct |
54 ms |
5948 KB |
Output is correct |
43 |
Correct |
56 ms |
14736 KB |
Output is correct |
44 |
Correct |
53 ms |
14760 KB |
Output is correct |
45 |
Correct |
58 ms |
14796 KB |
Output is correct |
46 |
Correct |
78 ms |
16284 KB |
Output is correct |
47 |
Correct |
141 ms |
17596 KB |
Output is correct |
48 |
Correct |
31 ms |
5068 KB |
Output is correct |
49 |
Correct |
6 ms |
5068 KB |
Output is correct |
50 |
Correct |
17 ms |
5324 KB |
Output is correct |
51 |
Correct |
29 ms |
5580 KB |
Output is correct |
52 |
Correct |
6 ms |
5708 KB |
Output is correct |
53 |
Correct |
12 ms |
5756 KB |
Output is correct |
54 |
Correct |
19 ms |
5964 KB |
Output is correct |
55 |
Correct |
33 ms |
6228 KB |
Output is correct |
56 |
Correct |
19 ms |
8556 KB |
Output is correct |
57 |
Correct |
22 ms |
8520 KB |
Output is correct |
58 |
Correct |
34 ms |
8756 KB |
Output is correct |
59 |
Correct |
48 ms |
9076 KB |
Output is correct |
60 |
Correct |
45 ms |
11972 KB |
Output is correct |
61 |
Correct |
42 ms |
12088 KB |
Output is correct |
62 |
Correct |
52 ms |
12260 KB |
Output is correct |
63 |
Correct |
66 ms |
12600 KB |
Output is correct |
64 |
Correct |
64 ms |
12612 KB |
Output is correct |
65 |
Incorrect |
42 ms |
9744 KB |
Output isn't correct |
66 |
Halted |
0 ms |
0 KB |
- |