#include "bits/extc++.h"
using namespace std;
template <typename T>
void dbgh(const T& t) {
cerr << t << endl;
}
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t << " | ";
dbgh(u...);
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
<< ": "; \
dbgh(__VA_ARGS__)
#else
#define cerr \
if (false) \
cerr
#define dbg(...)
#endif
#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())
struct DSU {
vector<int> p;
DSU(int n) : p(n, -1) {}
int find(int u) {
return p[u] < 0 ? u : (p[u] = find(p[u]));
}
bool merge(int u, int v) {
u = find(u);
v = find(v);
if (u == v) {
return false;
}
if (p[u] < p[v]) {
swap(u, v);
}
p[v] += p[u];
p[u] = v;
return true;
}
bool conn(int u, int v) {
return find(u) == find(v);
}
};
void solve() {
int n, m;
cin >> n >> m;
array<int, 3> edges[m];
for (auto& [w, u, v] : edges) {
cin >> u >> v >> w;
u--;
v--;
}
sort(edges, edges + m);
vector<array<int, 3>> changes;
for (int i = 0; i < m; i++) {
auto& [w, u, v] = edges[i];
changes.push_back({w, 2, -2 * w});
{
int j;
DSU dsu(n);
for (j = i - 1; j >= 0; j--) {
dsu.merge(edges[j][1], edges[j][2]);
if (dsu.conn(u, v)) {
break;
}
}
int a = -1, b = w;
if (j != -1) {
a += -1;
b += edges[j][0];
}
changes.push_back({j == -1 ? -1 : (w + edges[j][0] + 1) / 2, a, b});
}
}
sort(begin(changes), end(changes));
int q;
cin >> q;
long ans[q];
vector<pair<int, int>> queries;
for (int i = 0; i < q; i++) {
int x;
cin >> x;
queries.emplace_back(x, i);
}
sort(begin(queries), end(queries));
long ca = 0, cb = 0;
int j = 0;
for (auto& [x, qi] : queries) {
while (j < sz(changes) && changes[j][0] <= x) {
ca += changes[j][1];
cb += changes[j][2];
j++;
}
ans[qi] = ca * x + cb;
}
for (auto& a : ans) {
cout << a << endl;
}
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cin.exceptions(ios::failbit);
solve();
}
# |
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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
790 ms |
4692 KB |
Output is correct |
21 |
Correct |
551 ms |
4576 KB |
Output is correct |
22 |
Correct |
599 ms |
4696 KB |
Output is correct |
23 |
Correct |
679 ms |
4792 KB |
Output is correct |
24 |
Correct |
758 ms |
4624 KB |
Output is correct |
25 |
Correct |
774 ms |
4676 KB |
Output is correct |
26 |
Correct |
797 ms |
4944 KB |
Output is correct |
27 |
Correct |
796 ms |
4620 KB |
Output is correct |
28 |
Correct |
780 ms |
4696 KB |
Output is correct |
29 |
Correct |
527 ms |
4712 KB |
Output is correct |
30 |
Correct |
793 ms |
4680 KB |
Output is correct |
31 |
Correct |
781 ms |
4596 KB |
Output is correct |
32 |
Correct |
790 ms |
4676 KB |
Output is correct |
33 |
Correct |
783 ms |
4648 KB |
Output is correct |
34 |
Correct |
368 ms |
4676 KB |
Output is correct |
35 |
Correct |
772 ms |
4660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
864 ms |
36120 KB |
Output is correct |
5 |
Correct |
863 ms |
35548 KB |
Output is correct |
6 |
Correct |
873 ms |
35476 KB |
Output is correct |
7 |
Correct |
521 ms |
37392 KB |
Output is correct |
8 |
Correct |
439 ms |
37456 KB |
Output is correct |
9 |
Correct |
399 ms |
37544 KB |
Output is correct |
10 |
Correct |
842 ms |
35632 KB |
Output is correct |
11 |
Correct |
451 ms |
37472 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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
226 ms |
30732 KB |
Output is correct |
21 |
Correct |
212 ms |
28588 KB |
Output is correct |
22 |
Correct |
204 ms |
28436 KB |
Output is correct |
23 |
Correct |
206 ms |
28472 KB |
Output is correct |
24 |
Correct |
209 ms |
28380 KB |
Output is correct |
25 |
Correct |
213 ms |
28504 KB |
Output is correct |
26 |
Correct |
211 ms |
28316 KB |
Output is correct |
27 |
Correct |
208 ms |
28376 KB |
Output is correct |
28 |
Correct |
210 ms |
28384 KB |
Output is correct |
29 |
Correct |
207 ms |
28468 KB |
Output is correct |
30 |
Correct |
221 ms |
29920 KB |
Output is correct |
31 |
Correct |
210 ms |
28344 KB |
Output is correct |
32 |
Correct |
211 ms |
28980 KB |
Output is correct |
33 |
Correct |
209 ms |
28216 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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
790 ms |
4692 KB |
Output is correct |
21 |
Correct |
551 ms |
4576 KB |
Output is correct |
22 |
Correct |
599 ms |
4696 KB |
Output is correct |
23 |
Correct |
679 ms |
4792 KB |
Output is correct |
24 |
Correct |
758 ms |
4624 KB |
Output is correct |
25 |
Correct |
774 ms |
4676 KB |
Output is correct |
26 |
Correct |
797 ms |
4944 KB |
Output is correct |
27 |
Correct |
796 ms |
4620 KB |
Output is correct |
28 |
Correct |
780 ms |
4696 KB |
Output is correct |
29 |
Correct |
527 ms |
4712 KB |
Output is correct |
30 |
Correct |
793 ms |
4680 KB |
Output is correct |
31 |
Correct |
781 ms |
4596 KB |
Output is correct |
32 |
Correct |
790 ms |
4676 KB |
Output is correct |
33 |
Correct |
783 ms |
4648 KB |
Output is correct |
34 |
Correct |
368 ms |
4676 KB |
Output is correct |
35 |
Correct |
772 ms |
4660 KB |
Output is correct |
36 |
Correct |
790 ms |
4692 KB |
Output is correct |
37 |
Correct |
568 ms |
4684 KB |
Output is correct |
38 |
Correct |
585 ms |
4760 KB |
Output is correct |
39 |
Correct |
660 ms |
4668 KB |
Output is correct |
40 |
Correct |
760 ms |
4748 KB |
Output is correct |
41 |
Correct |
793 ms |
4764 KB |
Output is correct |
42 |
Correct |
780 ms |
4672 KB |
Output is correct |
43 |
Correct |
784 ms |
4668 KB |
Output is correct |
44 |
Correct |
775 ms |
4824 KB |
Output is correct |
45 |
Correct |
537 ms |
4708 KB |
Output is correct |
46 |
Correct |
783 ms |
4688 KB |
Output is correct |
47 |
Correct |
796 ms |
4748 KB |
Output is correct |
48 |
Correct |
790 ms |
4612 KB |
Output is correct |
49 |
Correct |
809 ms |
4696 KB |
Output is correct |
50 |
Correct |
381 ms |
4676 KB |
Output is correct |
51 |
Correct |
804 ms |
4656 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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
790 ms |
4692 KB |
Output is correct |
21 |
Correct |
551 ms |
4576 KB |
Output is correct |
22 |
Correct |
599 ms |
4696 KB |
Output is correct |
23 |
Correct |
679 ms |
4792 KB |
Output is correct |
24 |
Correct |
758 ms |
4624 KB |
Output is correct |
25 |
Correct |
774 ms |
4676 KB |
Output is correct |
26 |
Correct |
797 ms |
4944 KB |
Output is correct |
27 |
Correct |
796 ms |
4620 KB |
Output is correct |
28 |
Correct |
780 ms |
4696 KB |
Output is correct |
29 |
Correct |
527 ms |
4712 KB |
Output is correct |
30 |
Correct |
793 ms |
4680 KB |
Output is correct |
31 |
Correct |
781 ms |
4596 KB |
Output is correct |
32 |
Correct |
790 ms |
4676 KB |
Output is correct |
33 |
Correct |
783 ms |
4648 KB |
Output is correct |
34 |
Correct |
368 ms |
4676 KB |
Output is correct |
35 |
Correct |
772 ms |
4660 KB |
Output is correct |
36 |
Correct |
0 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 |
864 ms |
36120 KB |
Output is correct |
40 |
Correct |
863 ms |
35548 KB |
Output is correct |
41 |
Correct |
873 ms |
35476 KB |
Output is correct |
42 |
Correct |
521 ms |
37392 KB |
Output is correct |
43 |
Correct |
439 ms |
37456 KB |
Output is correct |
44 |
Correct |
399 ms |
37544 KB |
Output is correct |
45 |
Correct |
842 ms |
35632 KB |
Output is correct |
46 |
Correct |
451 ms |
37472 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
226 ms |
30732 KB |
Output is correct |
49 |
Correct |
212 ms |
28588 KB |
Output is correct |
50 |
Correct |
204 ms |
28436 KB |
Output is correct |
51 |
Correct |
206 ms |
28472 KB |
Output is correct |
52 |
Correct |
209 ms |
28380 KB |
Output is correct |
53 |
Correct |
213 ms |
28504 KB |
Output is correct |
54 |
Correct |
211 ms |
28316 KB |
Output is correct |
55 |
Correct |
208 ms |
28376 KB |
Output is correct |
56 |
Correct |
210 ms |
28384 KB |
Output is correct |
57 |
Correct |
207 ms |
28468 KB |
Output is correct |
58 |
Correct |
221 ms |
29920 KB |
Output is correct |
59 |
Correct |
210 ms |
28344 KB |
Output is correct |
60 |
Correct |
211 ms |
28980 KB |
Output is correct |
61 |
Correct |
209 ms |
28216 KB |
Output is correct |
62 |
Correct |
790 ms |
4692 KB |
Output is correct |
63 |
Correct |
568 ms |
4684 KB |
Output is correct |
64 |
Correct |
585 ms |
4760 KB |
Output is correct |
65 |
Correct |
660 ms |
4668 KB |
Output is correct |
66 |
Correct |
760 ms |
4748 KB |
Output is correct |
67 |
Correct |
793 ms |
4764 KB |
Output is correct |
68 |
Correct |
780 ms |
4672 KB |
Output is correct |
69 |
Correct |
784 ms |
4668 KB |
Output is correct |
70 |
Correct |
775 ms |
4824 KB |
Output is correct |
71 |
Correct |
537 ms |
4708 KB |
Output is correct |
72 |
Correct |
783 ms |
4688 KB |
Output is correct |
73 |
Correct |
796 ms |
4748 KB |
Output is correct |
74 |
Correct |
790 ms |
4612 KB |
Output is correct |
75 |
Correct |
809 ms |
4696 KB |
Output is correct |
76 |
Correct |
381 ms |
4676 KB |
Output is correct |
77 |
Correct |
804 ms |
4656 KB |
Output is correct |
78 |
Correct |
988 ms |
31700 KB |
Output is correct |
79 |
Correct |
765 ms |
33768 KB |
Output is correct |
80 |
Correct |
810 ms |
32700 KB |
Output is correct |
81 |
Correct |
850 ms |
32564 KB |
Output is correct |
82 |
Correct |
958 ms |
31792 KB |
Output is correct |
83 |
Correct |
966 ms |
31648 KB |
Output is correct |
84 |
Correct |
978 ms |
31616 KB |
Output is correct |
85 |
Correct |
977 ms |
31588 KB |
Output is correct |
86 |
Correct |
979 ms |
31604 KB |
Output is correct |
87 |
Correct |
720 ms |
33188 KB |
Output is correct |
88 |
Correct |
975 ms |
31684 KB |
Output is correct |
89 |
Correct |
982 ms |
31500 KB |
Output is correct |
90 |
Correct |
976 ms |
31760 KB |
Output is correct |
91 |
Correct |
962 ms |
31632 KB |
Output is correct |
92 |
Correct |
564 ms |
34468 KB |
Output is correct |
93 |
Correct |
977 ms |
32784 KB |
Output is correct |