#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define ins insert
#define arr3 array<int, 3>
const int inf = 1e9 + 5;
const int N = 5e2;
struct dsu{
int sz[N + 1], p[N + 1];
int n;
void init(int ns){
n = ns;
for (int i = 1; i <= n; i++){
p[i] = i;
sz[i] = 1;
}
}
int get(int v){
if (p[v] != v){
p[v] = get(p[v]);
}
return p[v];
}
void unite(int x, int y){
x = get(x); y = get(y);
if (x == y) return;
if (sz[x] > sz[y]) swap(x, y);
p[x] = y;
sz[y] += sz[x];
}
bool same(int x, int y){
return get(x) == get(y);
}
void clear(){
for (int i = 1; i <= n; i++){
p[i] = i;
sz[i] = 1;
}
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m; cin>>n>>m;
vector<arr3> ed;
for (int i = 1; i <= m; i++){
int a, b, w; cin>>a>>b>>w;
ed.pb({a, b, w});
}
int q; cin>>q;
vector<int> x(q + 1);
vector<ll> out(q + 1);
for (int i = 1; i <= q; i++){
cin>>x[i];
}
auto cmp = [&](arr3& x, arr3& y){
return x[2] < y[2];
};
sort(ed.begin(), ed.end(), cmp);
vector<int> ws;
for (int i = 0; i < m; i++) ws.pb(ed[i][2]);
vector<int> :: iterator it1, it2;
dsu T[m];
for (int i = 0; i < m; i++) T[i].init(n);
vector<ll> p1(q + 2);
vector<int> p2(q + 2);
for (int i = m - 1; i >= 0; i--){
auto [a, b, w] = ed[i];
int l = i, r = m - 1;
while (l + 1 < r){
int k = (l + r) / 2;
if (T[k].same(a, b)){
r = k - 1;
}
else {
l = k;
}
}
if (!T[r].same(a, b)) l = r;
if (l == (m - 1)) l = inf;
else l = (ws[l + 1] + w - 1) / 2;
it1 = lower_bound(x.begin() + 1, x.end(), w);
it2 = upper_bound(x.begin() + 1, x.end(), l);
l = (int) (it1 - x.begin()); r = (int) (it2 - x.begin());
if (l > r) continue;
p1[l] -= w; p1[r] += w;
p2[l]++; p2[r]--;
for (int j = i; j < m; j++){
if (T[j].same(a, b)) break;
T[j].unite(a, b);
}
}
for (int i = 0; i < m; i++) T[i].clear();
for (int i = 0; i < m; i++){
auto [a, b, w] = ed[i];
int l = 0, r = i;
while (l + 1 < r){
int k = (l + r) / 2;
if (T[k].same(a, b)){
l = k + 1;
}
else {
r = k;
}
}
if (!T[l].same(a, b)) r = l;
if (!r) r = 0;
else r = (ws[r - 1] + w + 1) / 2;
it1 = lower_bound(x.begin() + 1, x.end(), r);
it2 = lower_bound(x.begin() + 1, x.end(), w);
l = (int) (it1 - x.begin()); r = (int)(it2 - x.begin());
if (l > r) continue;
p1[l] += w; p1[r] -= w;
p2[l]--; p2[r]++;
for (int j = i; j >= 0; j--){
if (T[j].same(a, b)) break;
T[j].unite(a, b);
}
}
ll s1 = 0; int s2 = 0;
for (int i = 1; i <= q; i++){
s1 += p1[i];
s2 += p2[i];
cout<<1LL * x[i] * s2 + s1<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 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 |
1 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 |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 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 |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 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 |
1 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 |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 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 |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1242 ms |
396688 KB |
Output is correct |
21 |
Correct |
1337 ms |
396724 KB |
Output is correct |
22 |
Correct |
1452 ms |
396740 KB |
Output is correct |
23 |
Correct |
1249 ms |
396740 KB |
Output is correct |
24 |
Correct |
1307 ms |
396744 KB |
Output is correct |
25 |
Correct |
1320 ms |
396564 KB |
Output is correct |
26 |
Correct |
1254 ms |
396736 KB |
Output is correct |
27 |
Correct |
1316 ms |
396744 KB |
Output is correct |
28 |
Correct |
1275 ms |
396596 KB |
Output is correct |
29 |
Correct |
1261 ms |
396736 KB |
Output is correct |
30 |
Correct |
1337 ms |
396756 KB |
Output is correct |
31 |
Correct |
1286 ms |
396624 KB |
Output is correct |
32 |
Correct |
1273 ms |
396736 KB |
Output is correct |
33 |
Correct |
1279 ms |
396736 KB |
Output is correct |
34 |
Correct |
1214 ms |
396740 KB |
Output is correct |
35 |
Correct |
1194 ms |
396608 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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1271 ms |
440624 KB |
Output is correct |
5 |
Correct |
1310 ms |
440768 KB |
Output is correct |
6 |
Correct |
1337 ms |
440976 KB |
Output is correct |
7 |
Correct |
1453 ms |
442736 KB |
Output is correct |
8 |
Correct |
1421 ms |
443052 KB |
Output is correct |
9 |
Correct |
1342 ms |
442940 KB |
Output is correct |
10 |
Correct |
1257 ms |
441076 KB |
Output is correct |
11 |
Correct |
1354 ms |
442924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 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 |
1 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 |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 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 |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
164 ms |
49444 KB |
Output is correct |
21 |
Correct |
169 ms |
49492 KB |
Output is correct |
22 |
Correct |
155 ms |
49488 KB |
Output is correct |
23 |
Correct |
151 ms |
49488 KB |
Output is correct |
24 |
Correct |
151 ms |
49416 KB |
Output is correct |
25 |
Correct |
150 ms |
49380 KB |
Output is correct |
26 |
Correct |
145 ms |
49492 KB |
Output is correct |
27 |
Correct |
151 ms |
49444 KB |
Output is correct |
28 |
Correct |
143 ms |
49340 KB |
Output is correct |
29 |
Correct |
148 ms |
49308 KB |
Output is correct |
30 |
Correct |
137 ms |
49492 KB |
Output is correct |
31 |
Correct |
152 ms |
49232 KB |
Output is correct |
32 |
Correct |
149 ms |
49816 KB |
Output is correct |
33 |
Correct |
157 ms |
49196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 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 |
1 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 |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 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 |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1242 ms |
396688 KB |
Output is correct |
21 |
Correct |
1337 ms |
396724 KB |
Output is correct |
22 |
Correct |
1452 ms |
396740 KB |
Output is correct |
23 |
Correct |
1249 ms |
396740 KB |
Output is correct |
24 |
Correct |
1307 ms |
396744 KB |
Output is correct |
25 |
Correct |
1320 ms |
396564 KB |
Output is correct |
26 |
Correct |
1254 ms |
396736 KB |
Output is correct |
27 |
Correct |
1316 ms |
396744 KB |
Output is correct |
28 |
Correct |
1275 ms |
396596 KB |
Output is correct |
29 |
Correct |
1261 ms |
396736 KB |
Output is correct |
30 |
Correct |
1337 ms |
396756 KB |
Output is correct |
31 |
Correct |
1286 ms |
396624 KB |
Output is correct |
32 |
Correct |
1273 ms |
396736 KB |
Output is correct |
33 |
Correct |
1279 ms |
396736 KB |
Output is correct |
34 |
Correct |
1214 ms |
396740 KB |
Output is correct |
35 |
Correct |
1194 ms |
396608 KB |
Output is correct |
36 |
Correct |
1289 ms |
397228 KB |
Output is correct |
37 |
Correct |
1340 ms |
397436 KB |
Output is correct |
38 |
Correct |
1437 ms |
397232 KB |
Output is correct |
39 |
Correct |
1313 ms |
397216 KB |
Output is correct |
40 |
Correct |
1304 ms |
397228 KB |
Output is correct |
41 |
Correct |
1363 ms |
397232 KB |
Output is correct |
42 |
Correct |
1373 ms |
397252 KB |
Output is correct |
43 |
Correct |
1391 ms |
397248 KB |
Output is correct |
44 |
Correct |
1301 ms |
397252 KB |
Output is correct |
45 |
Correct |
1209 ms |
397244 KB |
Output is correct |
46 |
Correct |
1278 ms |
397388 KB |
Output is correct |
47 |
Correct |
1299 ms |
397224 KB |
Output is correct |
48 |
Correct |
1302 ms |
397316 KB |
Output is correct |
49 |
Correct |
1311 ms |
397220 KB |
Output is correct |
50 |
Correct |
1305 ms |
397508 KB |
Output is correct |
51 |
Correct |
1296 ms |
397252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 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 |
1 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 |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 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 |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1242 ms |
396688 KB |
Output is correct |
21 |
Correct |
1337 ms |
396724 KB |
Output is correct |
22 |
Correct |
1452 ms |
396740 KB |
Output is correct |
23 |
Correct |
1249 ms |
396740 KB |
Output is correct |
24 |
Correct |
1307 ms |
396744 KB |
Output is correct |
25 |
Correct |
1320 ms |
396564 KB |
Output is correct |
26 |
Correct |
1254 ms |
396736 KB |
Output is correct |
27 |
Correct |
1316 ms |
396744 KB |
Output is correct |
28 |
Correct |
1275 ms |
396596 KB |
Output is correct |
29 |
Correct |
1261 ms |
396736 KB |
Output is correct |
30 |
Correct |
1337 ms |
396756 KB |
Output is correct |
31 |
Correct |
1286 ms |
396624 KB |
Output is correct |
32 |
Correct |
1273 ms |
396736 KB |
Output is correct |
33 |
Correct |
1279 ms |
396736 KB |
Output is correct |
34 |
Correct |
1214 ms |
396740 KB |
Output is correct |
35 |
Correct |
1194 ms |
396608 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1271 ms |
440624 KB |
Output is correct |
40 |
Correct |
1310 ms |
440768 KB |
Output is correct |
41 |
Correct |
1337 ms |
440976 KB |
Output is correct |
42 |
Correct |
1453 ms |
442736 KB |
Output is correct |
43 |
Correct |
1421 ms |
443052 KB |
Output is correct |
44 |
Correct |
1342 ms |
442940 KB |
Output is correct |
45 |
Correct |
1257 ms |
441076 KB |
Output is correct |
46 |
Correct |
1354 ms |
442924 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
164 ms |
49444 KB |
Output is correct |
49 |
Correct |
169 ms |
49492 KB |
Output is correct |
50 |
Correct |
155 ms |
49488 KB |
Output is correct |
51 |
Correct |
151 ms |
49488 KB |
Output is correct |
52 |
Correct |
151 ms |
49416 KB |
Output is correct |
53 |
Correct |
150 ms |
49380 KB |
Output is correct |
54 |
Correct |
145 ms |
49492 KB |
Output is correct |
55 |
Correct |
151 ms |
49444 KB |
Output is correct |
56 |
Correct |
143 ms |
49340 KB |
Output is correct |
57 |
Correct |
148 ms |
49308 KB |
Output is correct |
58 |
Correct |
137 ms |
49492 KB |
Output is correct |
59 |
Correct |
152 ms |
49232 KB |
Output is correct |
60 |
Correct |
149 ms |
49816 KB |
Output is correct |
61 |
Correct |
157 ms |
49196 KB |
Output is correct |
62 |
Correct |
1289 ms |
397228 KB |
Output is correct |
63 |
Correct |
1340 ms |
397436 KB |
Output is correct |
64 |
Correct |
1437 ms |
397232 KB |
Output is correct |
65 |
Correct |
1313 ms |
397216 KB |
Output is correct |
66 |
Correct |
1304 ms |
397228 KB |
Output is correct |
67 |
Correct |
1363 ms |
397232 KB |
Output is correct |
68 |
Correct |
1373 ms |
397252 KB |
Output is correct |
69 |
Correct |
1391 ms |
397248 KB |
Output is correct |
70 |
Correct |
1301 ms |
397252 KB |
Output is correct |
71 |
Correct |
1209 ms |
397244 KB |
Output is correct |
72 |
Correct |
1278 ms |
397388 KB |
Output is correct |
73 |
Correct |
1299 ms |
397224 KB |
Output is correct |
74 |
Correct |
1302 ms |
397316 KB |
Output is correct |
75 |
Correct |
1311 ms |
397220 KB |
Output is correct |
76 |
Correct |
1305 ms |
397508 KB |
Output is correct |
77 |
Correct |
1296 ms |
397252 KB |
Output is correct |
78 |
Correct |
1483 ms |
439888 KB |
Output is correct |
79 |
Correct |
1557 ms |
441956 KB |
Output is correct |
80 |
Correct |
1509 ms |
440912 KB |
Output is correct |
81 |
Correct |
1538 ms |
440980 KB |
Output is correct |
82 |
Correct |
1449 ms |
439980 KB |
Output is correct |
83 |
Correct |
1579 ms |
439984 KB |
Output is correct |
84 |
Correct |
1463 ms |
439952 KB |
Output is correct |
85 |
Correct |
1481 ms |
439836 KB |
Output is correct |
86 |
Correct |
1306 ms |
440000 KB |
Output is correct |
87 |
Correct |
1348 ms |
441540 KB |
Output is correct |
88 |
Correct |
1474 ms |
439956 KB |
Output is correct |
89 |
Correct |
1500 ms |
440000 KB |
Output is correct |
90 |
Correct |
1449 ms |
440072 KB |
Output is correct |
91 |
Correct |
1409 ms |
439732 KB |
Output is correct |
92 |
Correct |
1417 ms |
443056 KB |
Output is correct |
93 |
Correct |
1376 ms |
441024 KB |
Output is correct |