#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;
struct dsu{
vector<int> sz, p;
int n;
void init(int ns){
n = ns;
sz.resize(n + 1);
p.resize(n + 1);
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 it;
dsu T[m];
for (int i = 0; i < m; i++) T[i].init(n);
for (int i = m - 1; i >= 0; i--){
auto [a, b, w] = ed[i];
auto check = [&](int x){
int ww = w + 2 * (x - w);
it = upper_bound(ws.begin(), ws.end(), ww); it--;
int j = (int) (it - ws.begin());
return T[j].same(a, b);
};
int l = w, r = inf;
while (l + 1 < r){
int m = (l + r) / 2;
if (check(m)){
r = m - 1;
}
else {
l = m;
}
}
if (!check(r)) l = r;
if (check(l)) continue;
for (int j = 1; j <= q; j++){
if (w <= x[j] && x[j] <= l){
out[j] += abs(x[j] - w);
}
}
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];
auto check = [&](int x){
int ww = 2 * x + 1 - w;
it = lower_bound(ws.begin(), ws.end(), ww);
int j = (int) (it - ws.begin());
return T[j].same(a, b);
};
int l = 0, r = w - 1;
while (l + 1 < r){
int m = (l + r) / 2;
if (check(m)){
l = m + 1;
}
else {
r = m;
}
}
if (!check(l)) r = l;
if (check(r)) continue;
for (int j = 1; j <= q; j++){
if (r <= x[j] && x[j] < w){
out[j] += abs(x[j] - w);
}
}
for (int j = i; j >= 0; j--){
if (T[j].same(a, b)) break;
T[j].unite(a, b);
}
}
for (int i = 1; i <= q; i++){
cout<<out[i]<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 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 |
344 KB |
Output is correct |
20 |
Correct |
1757 ms |
403688 KB |
Output is correct |
21 |
Correct |
1948 ms |
403676 KB |
Output is correct |
22 |
Correct |
1858 ms |
403728 KB |
Output is correct |
23 |
Correct |
1666 ms |
403908 KB |
Output is correct |
24 |
Correct |
1803 ms |
403720 KB |
Output is correct |
25 |
Correct |
1736 ms |
403732 KB |
Output is correct |
26 |
Correct |
1622 ms |
403728 KB |
Output is correct |
27 |
Correct |
1685 ms |
403652 KB |
Output is correct |
28 |
Correct |
1400 ms |
403724 KB |
Output is correct |
29 |
Correct |
1899 ms |
403648 KB |
Output is correct |
30 |
Correct |
1722 ms |
403912 KB |
Output is correct |
31 |
Correct |
1708 ms |
403728 KB |
Output is correct |
32 |
Correct |
1820 ms |
403732 KB |
Output is correct |
33 |
Correct |
1722 ms |
403520 KB |
Output is correct |
34 |
Correct |
282 ms |
403648 KB |
Output is correct |
35 |
Correct |
1602 ms |
403724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
5110 ms |
424928 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 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 |
344 KB |
Output is correct |
20 |
Correct |
1269 ms |
37948 KB |
Output is correct |
21 |
Correct |
1235 ms |
37968 KB |
Output is correct |
22 |
Correct |
1252 ms |
37972 KB |
Output is correct |
23 |
Correct |
1235 ms |
37972 KB |
Output is correct |
24 |
Correct |
1239 ms |
37716 KB |
Output is correct |
25 |
Correct |
1315 ms |
37716 KB |
Output is correct |
26 |
Correct |
1248 ms |
37712 KB |
Output is correct |
27 |
Correct |
1318 ms |
37972 KB |
Output is correct |
28 |
Correct |
1248 ms |
37796 KB |
Output is correct |
29 |
Correct |
1300 ms |
37652 KB |
Output is correct |
30 |
Correct |
1273 ms |
37972 KB |
Output is correct |
31 |
Correct |
1254 ms |
37564 KB |
Output is correct |
32 |
Correct |
779 ms |
38308 KB |
Output is correct |
33 |
Correct |
1268 ms |
37716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 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 |
344 KB |
Output is correct |
20 |
Correct |
1757 ms |
403688 KB |
Output is correct |
21 |
Correct |
1948 ms |
403676 KB |
Output is correct |
22 |
Correct |
1858 ms |
403728 KB |
Output is correct |
23 |
Correct |
1666 ms |
403908 KB |
Output is correct |
24 |
Correct |
1803 ms |
403720 KB |
Output is correct |
25 |
Correct |
1736 ms |
403732 KB |
Output is correct |
26 |
Correct |
1622 ms |
403728 KB |
Output is correct |
27 |
Correct |
1685 ms |
403652 KB |
Output is correct |
28 |
Correct |
1400 ms |
403724 KB |
Output is correct |
29 |
Correct |
1899 ms |
403648 KB |
Output is correct |
30 |
Correct |
1722 ms |
403912 KB |
Output is correct |
31 |
Correct |
1708 ms |
403728 KB |
Output is correct |
32 |
Correct |
1820 ms |
403732 KB |
Output is correct |
33 |
Correct |
1722 ms |
403520 KB |
Output is correct |
34 |
Correct |
282 ms |
403648 KB |
Output is correct |
35 |
Correct |
1602 ms |
403724 KB |
Output is correct |
36 |
Correct |
3591 ms |
404152 KB |
Output is correct |
37 |
Correct |
3738 ms |
404164 KB |
Output is correct |
38 |
Correct |
3655 ms |
404152 KB |
Output is correct |
39 |
Correct |
3523 ms |
404316 KB |
Output is correct |
40 |
Correct |
3592 ms |
404140 KB |
Output is correct |
41 |
Correct |
3464 ms |
404160 KB |
Output is correct |
42 |
Correct |
3526 ms |
404148 KB |
Output is correct |
43 |
Correct |
3588 ms |
404148 KB |
Output is correct |
44 |
Correct |
3408 ms |
404148 KB |
Output is correct |
45 |
Correct |
2076 ms |
404156 KB |
Output is correct |
46 |
Correct |
3346 ms |
404328 KB |
Output is correct |
47 |
Correct |
3710 ms |
404152 KB |
Output is correct |
48 |
Correct |
3694 ms |
404164 KB |
Output is correct |
49 |
Correct |
3542 ms |
404164 KB |
Output is correct |
50 |
Correct |
317 ms |
404420 KB |
Output is correct |
51 |
Correct |
3387 ms |
404160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 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 |
344 KB |
Output is correct |
20 |
Correct |
1757 ms |
403688 KB |
Output is correct |
21 |
Correct |
1948 ms |
403676 KB |
Output is correct |
22 |
Correct |
1858 ms |
403728 KB |
Output is correct |
23 |
Correct |
1666 ms |
403908 KB |
Output is correct |
24 |
Correct |
1803 ms |
403720 KB |
Output is correct |
25 |
Correct |
1736 ms |
403732 KB |
Output is correct |
26 |
Correct |
1622 ms |
403728 KB |
Output is correct |
27 |
Correct |
1685 ms |
403652 KB |
Output is correct |
28 |
Correct |
1400 ms |
403724 KB |
Output is correct |
29 |
Correct |
1899 ms |
403648 KB |
Output is correct |
30 |
Correct |
1722 ms |
403912 KB |
Output is correct |
31 |
Correct |
1708 ms |
403728 KB |
Output is correct |
32 |
Correct |
1820 ms |
403732 KB |
Output is correct |
33 |
Correct |
1722 ms |
403520 KB |
Output is correct |
34 |
Correct |
282 ms |
403648 KB |
Output is correct |
35 |
Correct |
1602 ms |
403724 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Execution timed out |
5110 ms |
424928 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |