#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> u(m);
vector<int> v(m);
vector<int> w(m);
for (int i = 0; i < m; i++) {
cin >> u[i] >> v[i] >> w[i];
--u[i]; --v[i];
}
{
vector<int> order(m);
iota(order.begin(), order.end(), 0);
sort(order.begin(), order.end(), [&](int i, int j) {
return w[i] < w[j];
});
vector<int> new_u(m);
vector<int> new_v(m);
vector<int> new_w(m);
for (int i = 0; i < m; i++) {
new_u[i] = u[order[i]];
new_v[i] = v[order[i]];
new_w[i] = w[order[i]];
}
u = new_u;
v = new_v;
w = new_w;
}
vector<int> par(n);
vector<int> sz(n);
auto Init = [&]() {
for (int i = 0; i < n; i++) {
par[i] = i;
sz[i] = 1;
}
};
function<int(int)> Get = [&](int x) {
return par[x] == x ? x : par[x] = Get(par[x]);
};
auto Unite = [&](int x, int y) {
x = Get(x);
y = Get(y);
if (x == y) {
return;
}
if (sz[x] < sz[y]) {
swap(x, y);
}
sz[x] += sz[y];
par[y] = x;
};
auto Same = [&](int x, int y) {
return Get(x) == Get(y);
};
vector<tuple<int, int, int>> vec;
for (int i = 0; i < m; i++) {
int from = -1, to = 1e9;
{
Init();
int ptr = i - 1;
while (ptr >= 0) {
Unite(u[ptr], v[ptr]);
if (Same(u[i], v[i])) {
break;
}
ptr -= 1;
}
if (ptr >= 0) {
from = (w[i] + w[ptr]) / 2 + 1;
}
if (from <= w[i]) {
vec.emplace_back(from, -1, +w[i]);
vec.emplace_back(w[i], +1, -w[i]);
}
}
{
Init();
int ptr = i + 1;
while (ptr < m) {
Unite(u[ptr], v[ptr]);
if (Same(u[i], v[i])) {
break;
}
ptr += 1;
}
if (ptr < m) {
to = (w[i] + w[ptr]) / 2;
}
vec.emplace_back(w[i], +1, -w[i]);
vec.emplace_back(to + 1, -1, +w[i]);
}
}
sort(vec.begin(), vec.end());
int coeff = 0;
long long add = 0;
int ptr = 0;
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
while (ptr < (int) vec.size() && get<0>(vec[ptr]) <= x) {
coeff += get<1>(vec[ptr]);
add += get<2>(vec[ptr]);
ptr += 1;
}
cout << coeff * 1LL * x + add << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
456 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
456 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
3168 ms |
10160 KB |
Output is correct |
21 |
Correct |
2404 ms |
10364 KB |
Output is correct |
22 |
Correct |
2552 ms |
10920 KB |
Output is correct |
23 |
Correct |
2813 ms |
10820 KB |
Output is correct |
24 |
Correct |
3147 ms |
9840 KB |
Output is correct |
25 |
Correct |
3072 ms |
10692 KB |
Output is correct |
26 |
Correct |
3186 ms |
11828 KB |
Output is correct |
27 |
Correct |
3180 ms |
9780 KB |
Output is correct |
28 |
Correct |
3162 ms |
11080 KB |
Output is correct |
29 |
Correct |
3105 ms |
6592 KB |
Output is correct |
30 |
Correct |
3178 ms |
9892 KB |
Output is correct |
31 |
Correct |
3127 ms |
10832 KB |
Output is correct |
32 |
Correct |
3183 ms |
11372 KB |
Output is correct |
33 |
Correct |
3129 ms |
10796 KB |
Output is correct |
34 |
Correct |
3096 ms |
7596 KB |
Output is correct |
35 |
Correct |
3109 ms |
10888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
2946 ms |
29756 KB |
Output is correct |
5 |
Correct |
2896 ms |
29116 KB |
Output is correct |
6 |
Correct |
2960 ms |
28880 KB |
Output is correct |
7 |
Correct |
1518 ms |
30788 KB |
Output is correct |
8 |
Correct |
1220 ms |
31148 KB |
Output is correct |
9 |
Correct |
1068 ms |
31140 KB |
Output is correct |
10 |
Correct |
2882 ms |
29420 KB |
Output is correct |
11 |
Correct |
1152 ms |
31892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
456 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
164 ms |
22356 KB |
Output is correct |
21 |
Correct |
168 ms |
22612 KB |
Output is correct |
22 |
Correct |
168 ms |
22356 KB |
Output is correct |
23 |
Correct |
164 ms |
22352 KB |
Output is correct |
24 |
Correct |
166 ms |
22556 KB |
Output is correct |
25 |
Correct |
174 ms |
22352 KB |
Output is correct |
26 |
Correct |
176 ms |
22588 KB |
Output is correct |
27 |
Correct |
178 ms |
22356 KB |
Output is correct |
28 |
Correct |
166 ms |
22352 KB |
Output is correct |
29 |
Correct |
169 ms |
22588 KB |
Output is correct |
30 |
Correct |
175 ms |
22440 KB |
Output is correct |
31 |
Correct |
171 ms |
22208 KB |
Output is correct |
32 |
Correct |
175 ms |
23076 KB |
Output is correct |
33 |
Correct |
173 ms |
22352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
456 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
3168 ms |
10160 KB |
Output is correct |
21 |
Correct |
2404 ms |
10364 KB |
Output is correct |
22 |
Correct |
2552 ms |
10920 KB |
Output is correct |
23 |
Correct |
2813 ms |
10820 KB |
Output is correct |
24 |
Correct |
3147 ms |
9840 KB |
Output is correct |
25 |
Correct |
3072 ms |
10692 KB |
Output is correct |
26 |
Correct |
3186 ms |
11828 KB |
Output is correct |
27 |
Correct |
3180 ms |
9780 KB |
Output is correct |
28 |
Correct |
3162 ms |
11080 KB |
Output is correct |
29 |
Correct |
3105 ms |
6592 KB |
Output is correct |
30 |
Correct |
3178 ms |
9892 KB |
Output is correct |
31 |
Correct |
3127 ms |
10832 KB |
Output is correct |
32 |
Correct |
3183 ms |
11372 KB |
Output is correct |
33 |
Correct |
3129 ms |
10796 KB |
Output is correct |
34 |
Correct |
3096 ms |
7596 KB |
Output is correct |
35 |
Correct |
3109 ms |
10888 KB |
Output is correct |
36 |
Correct |
3159 ms |
11356 KB |
Output is correct |
37 |
Correct |
2389 ms |
11208 KB |
Output is correct |
38 |
Correct |
2523 ms |
9748 KB |
Output is correct |
39 |
Correct |
2852 ms |
11164 KB |
Output is correct |
40 |
Correct |
3184 ms |
10152 KB |
Output is correct |
41 |
Correct |
3111 ms |
11264 KB |
Output is correct |
42 |
Correct |
3109 ms |
10736 KB |
Output is correct |
43 |
Correct |
3156 ms |
11372 KB |
Output is correct |
44 |
Correct |
3168 ms |
10412 KB |
Output is correct |
45 |
Correct |
3117 ms |
7176 KB |
Output is correct |
46 |
Correct |
3122 ms |
10144 KB |
Output is correct |
47 |
Correct |
3165 ms |
9864 KB |
Output is correct |
48 |
Correct |
3170 ms |
10452 KB |
Output is correct |
49 |
Correct |
3164 ms |
11800 KB |
Output is correct |
50 |
Correct |
3088 ms |
7544 KB |
Output is correct |
51 |
Correct |
3118 ms |
9976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
456 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
3168 ms |
10160 KB |
Output is correct |
21 |
Correct |
2404 ms |
10364 KB |
Output is correct |
22 |
Correct |
2552 ms |
10920 KB |
Output is correct |
23 |
Correct |
2813 ms |
10820 KB |
Output is correct |
24 |
Correct |
3147 ms |
9840 KB |
Output is correct |
25 |
Correct |
3072 ms |
10692 KB |
Output is correct |
26 |
Correct |
3186 ms |
11828 KB |
Output is correct |
27 |
Correct |
3180 ms |
9780 KB |
Output is correct |
28 |
Correct |
3162 ms |
11080 KB |
Output is correct |
29 |
Correct |
3105 ms |
6592 KB |
Output is correct |
30 |
Correct |
3178 ms |
9892 KB |
Output is correct |
31 |
Correct |
3127 ms |
10832 KB |
Output is correct |
32 |
Correct |
3183 ms |
11372 KB |
Output is correct |
33 |
Correct |
3129 ms |
10796 KB |
Output is correct |
34 |
Correct |
3096 ms |
7596 KB |
Output is correct |
35 |
Correct |
3109 ms |
10888 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
2946 ms |
29756 KB |
Output is correct |
40 |
Correct |
2896 ms |
29116 KB |
Output is correct |
41 |
Correct |
2960 ms |
28880 KB |
Output is correct |
42 |
Correct |
1518 ms |
30788 KB |
Output is correct |
43 |
Correct |
1220 ms |
31148 KB |
Output is correct |
44 |
Correct |
1068 ms |
31140 KB |
Output is correct |
45 |
Correct |
2882 ms |
29420 KB |
Output is correct |
46 |
Correct |
1152 ms |
31892 KB |
Output is correct |
47 |
Correct |
1 ms |
344 KB |
Output is correct |
48 |
Correct |
164 ms |
22356 KB |
Output is correct |
49 |
Correct |
168 ms |
22612 KB |
Output is correct |
50 |
Correct |
168 ms |
22356 KB |
Output is correct |
51 |
Correct |
164 ms |
22352 KB |
Output is correct |
52 |
Correct |
166 ms |
22556 KB |
Output is correct |
53 |
Correct |
174 ms |
22352 KB |
Output is correct |
54 |
Correct |
176 ms |
22588 KB |
Output is correct |
55 |
Correct |
178 ms |
22356 KB |
Output is correct |
56 |
Correct |
166 ms |
22352 KB |
Output is correct |
57 |
Correct |
169 ms |
22588 KB |
Output is correct |
58 |
Correct |
175 ms |
22440 KB |
Output is correct |
59 |
Correct |
171 ms |
22208 KB |
Output is correct |
60 |
Correct |
175 ms |
23076 KB |
Output is correct |
61 |
Correct |
173 ms |
22352 KB |
Output is correct |
62 |
Correct |
3159 ms |
11356 KB |
Output is correct |
63 |
Correct |
2389 ms |
11208 KB |
Output is correct |
64 |
Correct |
2523 ms |
9748 KB |
Output is correct |
65 |
Correct |
2852 ms |
11164 KB |
Output is correct |
66 |
Correct |
3184 ms |
10152 KB |
Output is correct |
67 |
Correct |
3111 ms |
11264 KB |
Output is correct |
68 |
Correct |
3109 ms |
10736 KB |
Output is correct |
69 |
Correct |
3156 ms |
11372 KB |
Output is correct |
70 |
Correct |
3168 ms |
10412 KB |
Output is correct |
71 |
Correct |
3117 ms |
7176 KB |
Output is correct |
72 |
Correct |
3122 ms |
10144 KB |
Output is correct |
73 |
Correct |
3165 ms |
9864 KB |
Output is correct |
74 |
Correct |
3170 ms |
10452 KB |
Output is correct |
75 |
Correct |
3164 ms |
11800 KB |
Output is correct |
76 |
Correct |
3088 ms |
7544 KB |
Output is correct |
77 |
Correct |
3118 ms |
9976 KB |
Output is correct |
78 |
Correct |
3303 ms |
28136 KB |
Output is correct |
79 |
Correct |
2504 ms |
30144 KB |
Output is correct |
80 |
Correct |
2721 ms |
30304 KB |
Output is correct |
81 |
Correct |
2945 ms |
29256 KB |
Output is correct |
82 |
Correct |
3307 ms |
27996 KB |
Output is correct |
83 |
Correct |
3261 ms |
28848 KB |
Output is correct |
84 |
Correct |
3273 ms |
28944 KB |
Output is correct |
85 |
Correct |
3256 ms |
28012 KB |
Output is correct |
86 |
Correct |
3256 ms |
27840 KB |
Output is correct |
87 |
Correct |
3243 ms |
27856 KB |
Output is correct |
88 |
Correct |
3250 ms |
27760 KB |
Output is correct |
89 |
Correct |
3244 ms |
28076 KB |
Output is correct |
90 |
Correct |
3290 ms |
28188 KB |
Output is correct |
91 |
Correct |
3261 ms |
28216 KB |
Output is correct |
92 |
Correct |
3267 ms |
28576 KB |
Output is correct |
93 |
Correct |
3256 ms |
28776 KB |
Output is correct |