#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Line {
ll k = 0, b = 0;
Line(ll k = 0, ll b = 0) : k(k), b(b) {}
ll eval(ll x) {
return k * x + b;
}
friend Line operator+(const Line &a, const Line &c) {
return {a.k + c.k, a.b + c.b};
}
};
constexpr int Q = 1e5 + 1, N = 500;
array<int, 3> e[Q];
int fa[N], sz[N];
void init() {
iota(fa, fa + N, 0);
fill_n(sz, N, 1);
}
int leader(int x) {
return x == fa[x] ? x : fa[x] = leader(fa[x]);
}
bool unite(int x, int y) {
x = leader(x), y = leader(y);
if (x == y) {
return false;
}
if (sz[x] < sz[y]) {
swap(x, y);
}
fa[y] = x;
sz[x] += sz[y];
return true;
}
bool same(int x, int y) {
return leader(x) == leader(y);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> e[i][1] >> e[i][2] >> e[i][0];
e[i][1] -= 1, e[i][2] -= 1;
}
sort(e, e + m);
vector<array<int, 3>> mst;
vector<pair<int, Line>> events;
for (int i = m - 1; i >= 0; --i) {
init();
auto [wi, x, y] = e[i];
events.emplace_back(wi, Line(2, -2 * wi));
mst.insert(mst.begin(), e[i]);
for (int j = 1; j < mst.size(); ++j) {
auto [w, a, b] = mst[j];
unite(a, b);
if (same(x, y)) {
int mid = (wi + w + 1) / 2;
events.emplace_back(mid, Line(-1, wi));
events.emplace_back(mid, Line(-1, w));
mst.erase(mst.begin() + j);
break;
}
}
}
init();
for (int i = 0; i < m; ++i) {
auto [w, a, b] = e[i];
if (unite(a, b)) {
events.emplace_back(-1, Line(-1, w));
}
}
sort(events.begin(), events.end(), [&](auto &x, auto &y) {
return x.first < y.first;
});
Line res{};
int q;
cin >> q;
int i = 0;
while (q--) {
int x;
cin >> x;
while (i < events.size() && events[i].first <= x) {
res = res + events[i++].second;
}
cout << res.eval(x) << '\n';
}
return 0;
}
Compilation message
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:81:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for (int j = 1; j < mst.size(); ++j) {
| ~~^~~~~~~~~~~~
reconstruction.cpp:124:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, Line> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | while (i < events.size() && events[i].first <= x) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
328 KB |
Output is correct |
12 |
Correct |
0 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
328 KB |
Output is correct |
12 |
Correct |
0 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
328 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
779 ms |
15560 KB |
Output is correct |
21 |
Correct |
414 ms |
15752 KB |
Output is correct |
22 |
Correct |
410 ms |
15544 KB |
Output is correct |
23 |
Correct |
443 ms |
15688 KB |
Output is correct |
24 |
Correct |
608 ms |
15700 KB |
Output is correct |
25 |
Correct |
702 ms |
15544 KB |
Output is correct |
26 |
Correct |
755 ms |
15624 KB |
Output is correct |
27 |
Correct |
782 ms |
15684 KB |
Output is correct |
28 |
Correct |
738 ms |
15672 KB |
Output is correct |
29 |
Correct |
344 ms |
15588 KB |
Output is correct |
30 |
Correct |
737 ms |
15668 KB |
Output is correct |
31 |
Correct |
748 ms |
15844 KB |
Output is correct |
32 |
Correct |
768 ms |
15660 KB |
Output is correct |
33 |
Correct |
818 ms |
15644 KB |
Output is correct |
34 |
Correct |
341 ms |
15712 KB |
Output is correct |
35 |
Correct |
773 ms |
15544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
649 ms |
31152 KB |
Output is correct |
5 |
Correct |
642 ms |
31044 KB |
Output is correct |
6 |
Correct |
642 ms |
31100 KB |
Output is correct |
7 |
Correct |
384 ms |
32968 KB |
Output is correct |
8 |
Correct |
323 ms |
33112 KB |
Output is correct |
9 |
Correct |
308 ms |
33128 KB |
Output is correct |
10 |
Correct |
647 ms |
31296 KB |
Output is correct |
11 |
Correct |
333 ms |
33184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
328 KB |
Output is correct |
12 |
Correct |
0 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
328 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
189 ms |
22488 KB |
Output is correct |
21 |
Correct |
184 ms |
22536 KB |
Output is correct |
22 |
Correct |
206 ms |
22456 KB |
Output is correct |
23 |
Correct |
234 ms |
22524 KB |
Output is correct |
24 |
Correct |
189 ms |
22388 KB |
Output is correct |
25 |
Correct |
185 ms |
22408 KB |
Output is correct |
26 |
Correct |
185 ms |
22376 KB |
Output is correct |
27 |
Correct |
188 ms |
22488 KB |
Output is correct |
28 |
Correct |
189 ms |
22420 KB |
Output is correct |
29 |
Correct |
185 ms |
22408 KB |
Output is correct |
30 |
Correct |
203 ms |
22508 KB |
Output is correct |
31 |
Correct |
213 ms |
22448 KB |
Output is correct |
32 |
Correct |
225 ms |
23012 KB |
Output is correct |
33 |
Correct |
200 ms |
22328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
328 KB |
Output is correct |
12 |
Correct |
0 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
328 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
779 ms |
15560 KB |
Output is correct |
21 |
Correct |
414 ms |
15752 KB |
Output is correct |
22 |
Correct |
410 ms |
15544 KB |
Output is correct |
23 |
Correct |
443 ms |
15688 KB |
Output is correct |
24 |
Correct |
608 ms |
15700 KB |
Output is correct |
25 |
Correct |
702 ms |
15544 KB |
Output is correct |
26 |
Correct |
755 ms |
15624 KB |
Output is correct |
27 |
Correct |
782 ms |
15684 KB |
Output is correct |
28 |
Correct |
738 ms |
15672 KB |
Output is correct |
29 |
Correct |
344 ms |
15588 KB |
Output is correct |
30 |
Correct |
737 ms |
15668 KB |
Output is correct |
31 |
Correct |
748 ms |
15844 KB |
Output is correct |
32 |
Correct |
768 ms |
15660 KB |
Output is correct |
33 |
Correct |
818 ms |
15644 KB |
Output is correct |
34 |
Correct |
341 ms |
15712 KB |
Output is correct |
35 |
Correct |
773 ms |
15544 KB |
Output is correct |
36 |
Correct |
757 ms |
15796 KB |
Output is correct |
37 |
Correct |
393 ms |
15856 KB |
Output is correct |
38 |
Correct |
414 ms |
15828 KB |
Output is correct |
39 |
Correct |
479 ms |
15860 KB |
Output is correct |
40 |
Correct |
585 ms |
15772 KB |
Output is correct |
41 |
Correct |
713 ms |
15896 KB |
Output is correct |
42 |
Correct |
743 ms |
15832 KB |
Output is correct |
43 |
Correct |
749 ms |
15944 KB |
Output is correct |
44 |
Correct |
730 ms |
15864 KB |
Output is correct |
45 |
Correct |
343 ms |
15760 KB |
Output is correct |
46 |
Correct |
758 ms |
15980 KB |
Output is correct |
47 |
Correct |
773 ms |
15880 KB |
Output is correct |
48 |
Correct |
755 ms |
15932 KB |
Output is correct |
49 |
Correct |
783 ms |
15724 KB |
Output is correct |
50 |
Correct |
335 ms |
15916 KB |
Output is correct |
51 |
Correct |
740 ms |
15932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
328 KB |
Output is correct |
12 |
Correct |
0 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
328 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
779 ms |
15560 KB |
Output is correct |
21 |
Correct |
414 ms |
15752 KB |
Output is correct |
22 |
Correct |
410 ms |
15544 KB |
Output is correct |
23 |
Correct |
443 ms |
15688 KB |
Output is correct |
24 |
Correct |
608 ms |
15700 KB |
Output is correct |
25 |
Correct |
702 ms |
15544 KB |
Output is correct |
26 |
Correct |
755 ms |
15624 KB |
Output is correct |
27 |
Correct |
782 ms |
15684 KB |
Output is correct |
28 |
Correct |
738 ms |
15672 KB |
Output is correct |
29 |
Correct |
344 ms |
15588 KB |
Output is correct |
30 |
Correct |
737 ms |
15668 KB |
Output is correct |
31 |
Correct |
748 ms |
15844 KB |
Output is correct |
32 |
Correct |
768 ms |
15660 KB |
Output is correct |
33 |
Correct |
818 ms |
15644 KB |
Output is correct |
34 |
Correct |
341 ms |
15712 KB |
Output is correct |
35 |
Correct |
773 ms |
15544 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
649 ms |
31152 KB |
Output is correct |
40 |
Correct |
642 ms |
31044 KB |
Output is correct |
41 |
Correct |
642 ms |
31100 KB |
Output is correct |
42 |
Correct |
384 ms |
32968 KB |
Output is correct |
43 |
Correct |
323 ms |
33112 KB |
Output is correct |
44 |
Correct |
308 ms |
33128 KB |
Output is correct |
45 |
Correct |
647 ms |
31296 KB |
Output is correct |
46 |
Correct |
333 ms |
33184 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
189 ms |
22488 KB |
Output is correct |
49 |
Correct |
184 ms |
22536 KB |
Output is correct |
50 |
Correct |
206 ms |
22456 KB |
Output is correct |
51 |
Correct |
234 ms |
22524 KB |
Output is correct |
52 |
Correct |
189 ms |
22388 KB |
Output is correct |
53 |
Correct |
185 ms |
22408 KB |
Output is correct |
54 |
Correct |
185 ms |
22376 KB |
Output is correct |
55 |
Correct |
188 ms |
22488 KB |
Output is correct |
56 |
Correct |
189 ms |
22420 KB |
Output is correct |
57 |
Correct |
185 ms |
22408 KB |
Output is correct |
58 |
Correct |
203 ms |
22508 KB |
Output is correct |
59 |
Correct |
213 ms |
22448 KB |
Output is correct |
60 |
Correct |
225 ms |
23012 KB |
Output is correct |
61 |
Correct |
200 ms |
22328 KB |
Output is correct |
62 |
Correct |
757 ms |
15796 KB |
Output is correct |
63 |
Correct |
393 ms |
15856 KB |
Output is correct |
64 |
Correct |
414 ms |
15828 KB |
Output is correct |
65 |
Correct |
479 ms |
15860 KB |
Output is correct |
66 |
Correct |
585 ms |
15772 KB |
Output is correct |
67 |
Correct |
713 ms |
15896 KB |
Output is correct |
68 |
Correct |
743 ms |
15832 KB |
Output is correct |
69 |
Correct |
749 ms |
15944 KB |
Output is correct |
70 |
Correct |
730 ms |
15864 KB |
Output is correct |
71 |
Correct |
343 ms |
15760 KB |
Output is correct |
72 |
Correct |
758 ms |
15980 KB |
Output is correct |
73 |
Correct |
773 ms |
15880 KB |
Output is correct |
74 |
Correct |
755 ms |
15932 KB |
Output is correct |
75 |
Correct |
783 ms |
15724 KB |
Output is correct |
76 |
Correct |
335 ms |
15916 KB |
Output is correct |
77 |
Correct |
740 ms |
15932 KB |
Output is correct |
78 |
Correct |
923 ms |
30004 KB |
Output is correct |
79 |
Correct |
555 ms |
32052 KB |
Output is correct |
80 |
Correct |
559 ms |
31084 KB |
Output is correct |
81 |
Correct |
628 ms |
31104 KB |
Output is correct |
82 |
Correct |
744 ms |
30116 KB |
Output is correct |
83 |
Correct |
864 ms |
30100 KB |
Output is correct |
84 |
Correct |
914 ms |
29996 KB |
Output is correct |
85 |
Correct |
914 ms |
30072 KB |
Output is correct |
86 |
Correct |
893 ms |
30148 KB |
Output is correct |
87 |
Correct |
505 ms |
31868 KB |
Output is correct |
88 |
Correct |
911 ms |
30048 KB |
Output is correct |
89 |
Correct |
932 ms |
30244 KB |
Output is correct |
90 |
Correct |
904 ms |
30284 KB |
Output is correct |
91 |
Correct |
900 ms |
30076 KB |
Output is correct |
92 |
Correct |
498 ms |
32992 KB |
Output is correct |
93 |
Correct |
902 ms |
31380 KB |
Output is correct |