#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
// #define int long long
#define ll long long
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define debug(x) [](decltype(x) _x){cerr << #x << " = " << _x << endl; return _x;}(x);
using pii = pair<int, int>;
const int inf = 1 << 30;
ll pc = 0;
ll pl;
ll now() {
return chrono::system_clock().now().time_since_epoch().count();
}
void pstart() {
pl = now();
}
void pend() {
pc += now() - pl;
}
struct dsu {
short p[500];
dsu(int n) {
for(int i = 0; i < 500; i++) {
p[i] = -1;
}
}
int find(int x) {
return p[x] < 0 ? x : p[x] = find(p[x]);
}
void merge(int x, int y) {
x = find(x);
y = find(y);
if(x == y) return;
if(p[x] < p[y]) swap(x, y);
p[y] += p[x];
p[x] = y;
}
};
struct my_hash {
size_t operator()(int x) const {
return x * 31;
}
};
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// ll start = now();
int n, m; cin >> n >> m;
struct edge {
short a, b;
int w;
};
vector<edge> el(m);
for(int i = 0; i < m; i++) {
cin >> el[i].a >> el[i].b >> el[i].w;
el[i].a--; el[i].b--;
}
sort(all(el), [&](edge a, edge b) {
return a.w < b.w;
});
int q; cin >> q;
vector<int> x(q);
for(int i = 0; i < q; i++) {
cin >> x[i];
}
auto sx = x;
sort(all(sx));
gp_hash_table<int, ll, my_hash> ans;
int cnt = 0;
function<void(int, int, vector<int>)> dnc = [&](int ql, int qr, vector<int> es) {
if(ql == qr) return;
if(es.size() == n - 1) {
cnt += qr - ql;
for(int i = ql; i < qr; i++) {
ll tot = 0;
for(auto j : es) {
tot += abs(el[j].w - sx[i]);
}
ans[sx[i]] = tot;
}
return;
}
int qm = (ql + qr) / 2;
int qw = sx[qm];
// debug(qw);
// if(qw == 8) {
// for(auto e : el) {
// cerr << e.a << " " << e.b << " " << e.w << endl;
// }
// cerr << endl;
// }
int loc = 0, eqc = 0, hic = 0;
for(auto i : es) {
if(el[i].w < qw) loc++;
else if(el[i].w > qw) hic++;
}
vector<int> lo(es.begin(), es.begin() + loc), eq(es.begin() + loc, es.end() - hic), hi(es.end() - hic, es.end());
reverse(all(lo));
int ls = lo.size(), hs = hi.size();
vector<int> lou, equ, hiu;
lou.reserve(128);
equ.reserve(128);
hiu.reserve(128);
dsu uf(n);
int ec = 0;
for(auto i : eq) {
if(uf.find(el[i].a) != uf.find(el[i].b)) {
uf.merge(el[i].a, el[i].b);
equ.push_back(i);
ec++;
}
}
int loi = 0, hii = 0;
ll tot = 0;
while(ec < n - 1) {
bool dlo = false;
if(hii == hi.size()) dlo = true;
else if(loi == lo.size()) dlo = false;
else dlo = qw - el[lo[loi]].w < el[hi[hii]].w - qw;
if(dlo) {
int i = lo[loi];
loi++;
if(uf.find(el[i].a) != uf.find(el[i].b)) {
uf.merge(el[i].a, el[i].b);
// pstart();
lou.push_back(i);
// pend();
tot += qw - el[i].w;
ec++;
}
} else {
int i = hi[hii];
hii++;
if(uf.find(el[i].a) != uf.find(el[i].b)) {
uf.merge(el[i].a, el[i].b);
// pstart();
hiu.push_back(i);
// pend();
tot += el[i].w - qw;
ec++;
}
}
}
ans[qw] = tot;
reverse(all(lo));
reverse(all(hi));
lo.reserve(lo.size() + equ.size() + hiu.size());
hi.reserve(hi.size() + equ.size() + lou.size());
for(auto i : equ) {
lo.push_back(i);
hi.push_back(i);
// if(qw == 8) cerr << i.a << " " << i.b << " " << i.w << endl;
}
for(auto i : lou) {
hi.push_back(i);
// if(qw == 8) cerr << i.a << " " << i.b << " " << i.w << endl;
}
for(auto i : hiu) {
lo.push_back(i);
// if(qw == 8) cerr << i.a << " " << i.b << " " << i.w << endl;
}
reverse(all(hi));
dnc(ql, qm, move(lo));
dnc(qm + 1, qr, move(hi));
// dnc(ql, qm, el);
// dnc(qm + 1, qr, el);
};
vector<int> id(m);
iota(all(id), 0);
dnc(0, q, id);
for(auto i : x) {
cout << ans[i] << "\n";
}
// ll end = now();
// debug((double)pc / (end - start));
// debug(cnt);
}
Compilation message
reconstruction.cpp: In lambda function:
reconstruction.cpp:84:18: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
84 | if(es.size() == n - 1) {
| ~~~~~~~~~~^~~~~~~~
reconstruction.cpp:129:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
129 | if(hii == hi.size()) dlo = true;
| ~~~~^~~~~~~~~~~~
reconstruction.cpp:130:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
130 | else if(loi == lo.size()) dlo = false;
| ~~~~^~~~~~~~~~~~
reconstruction.cpp:104:18: warning: unused variable 'eqc' [-Wunused-variable]
104 | int loc = 0, eqc = 0, hic = 0;
| ^~~
reconstruction.cpp:111:9: warning: unused variable 'ls' [-Wunused-variable]
111 | int ls = lo.size(), hs = hi.size();
| ^~
reconstruction.cpp:111:25: warning: unused variable 'hs' [-Wunused-variable]
111 | int ls = lo.size(), hs = hi.size();
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
27 ms |
4700 KB |
Output is correct |
21 |
Correct |
29 ms |
4972 KB |
Output is correct |
22 |
Correct |
29 ms |
4812 KB |
Output is correct |
23 |
Correct |
27 ms |
4812 KB |
Output is correct |
24 |
Correct |
27 ms |
4680 KB |
Output is correct |
25 |
Correct |
27 ms |
4628 KB |
Output is correct |
26 |
Correct |
25 ms |
4564 KB |
Output is correct |
27 |
Correct |
33 ms |
4608 KB |
Output is correct |
28 |
Correct |
25 ms |
4688 KB |
Output is correct |
29 |
Correct |
24 ms |
4896 KB |
Output is correct |
30 |
Correct |
26 ms |
4900 KB |
Output is correct |
31 |
Correct |
25 ms |
4592 KB |
Output is correct |
32 |
Correct |
29 ms |
4912 KB |
Output is correct |
33 |
Correct |
26 ms |
4944 KB |
Output is correct |
34 |
Correct |
22 ms |
4956 KB |
Output is correct |
35 |
Correct |
27 ms |
5068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
452 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
4463 ms |
105960 KB |
Output is correct |
5 |
Correct |
4302 ms |
105820 KB |
Output is correct |
6 |
Correct |
4439 ms |
105760 KB |
Output is correct |
7 |
Correct |
3647 ms |
107600 KB |
Output is correct |
8 |
Correct |
3539 ms |
107856 KB |
Output is correct |
9 |
Correct |
3713 ms |
107932 KB |
Output is correct |
10 |
Correct |
669 ms |
105652 KB |
Output is correct |
11 |
Correct |
710 ms |
107620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
856 ms |
104292 KB |
Output is correct |
21 |
Correct |
828 ms |
104280 KB |
Output is correct |
22 |
Correct |
776 ms |
104220 KB |
Output is correct |
23 |
Correct |
801 ms |
104452 KB |
Output is correct |
24 |
Correct |
784 ms |
104280 KB |
Output is correct |
25 |
Correct |
818 ms |
104280 KB |
Output is correct |
26 |
Correct |
784 ms |
104304 KB |
Output is correct |
27 |
Correct |
709 ms |
104180 KB |
Output is correct |
28 |
Correct |
815 ms |
104048 KB |
Output is correct |
29 |
Correct |
776 ms |
104072 KB |
Output is correct |
30 |
Correct |
779 ms |
104196 KB |
Output is correct |
31 |
Correct |
790 ms |
104020 KB |
Output is correct |
32 |
Correct |
719 ms |
104832 KB |
Output is correct |
33 |
Correct |
618 ms |
104032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
27 ms |
4700 KB |
Output is correct |
21 |
Correct |
29 ms |
4972 KB |
Output is correct |
22 |
Correct |
29 ms |
4812 KB |
Output is correct |
23 |
Correct |
27 ms |
4812 KB |
Output is correct |
24 |
Correct |
27 ms |
4680 KB |
Output is correct |
25 |
Correct |
27 ms |
4628 KB |
Output is correct |
26 |
Correct |
25 ms |
4564 KB |
Output is correct |
27 |
Correct |
33 ms |
4608 KB |
Output is correct |
28 |
Correct |
25 ms |
4688 KB |
Output is correct |
29 |
Correct |
24 ms |
4896 KB |
Output is correct |
30 |
Correct |
26 ms |
4900 KB |
Output is correct |
31 |
Correct |
25 ms |
4592 KB |
Output is correct |
32 |
Correct |
29 ms |
4912 KB |
Output is correct |
33 |
Correct |
26 ms |
4944 KB |
Output is correct |
34 |
Correct |
22 ms |
4956 KB |
Output is correct |
35 |
Correct |
27 ms |
5068 KB |
Output is correct |
36 |
Correct |
328 ms |
7296 KB |
Output is correct |
37 |
Correct |
287 ms |
7032 KB |
Output is correct |
38 |
Correct |
291 ms |
7300 KB |
Output is correct |
39 |
Correct |
339 ms |
7064 KB |
Output is correct |
40 |
Correct |
323 ms |
7288 KB |
Output is correct |
41 |
Correct |
337 ms |
7292 KB |
Output is correct |
42 |
Correct |
331 ms |
7064 KB |
Output is correct |
43 |
Correct |
327 ms |
7148 KB |
Output is correct |
44 |
Correct |
136 ms |
7048 KB |
Output is correct |
45 |
Correct |
39 ms |
7520 KB |
Output is correct |
46 |
Correct |
353 ms |
7288 KB |
Output is correct |
47 |
Correct |
327 ms |
7364 KB |
Output is correct |
48 |
Correct |
314 ms |
7724 KB |
Output is correct |
49 |
Correct |
314 ms |
9120 KB |
Output is correct |
50 |
Correct |
36 ms |
11264 KB |
Output is correct |
51 |
Correct |
42 ms |
8648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
27 ms |
4700 KB |
Output is correct |
21 |
Correct |
29 ms |
4972 KB |
Output is correct |
22 |
Correct |
29 ms |
4812 KB |
Output is correct |
23 |
Correct |
27 ms |
4812 KB |
Output is correct |
24 |
Correct |
27 ms |
4680 KB |
Output is correct |
25 |
Correct |
27 ms |
4628 KB |
Output is correct |
26 |
Correct |
25 ms |
4564 KB |
Output is correct |
27 |
Correct |
33 ms |
4608 KB |
Output is correct |
28 |
Correct |
25 ms |
4688 KB |
Output is correct |
29 |
Correct |
24 ms |
4896 KB |
Output is correct |
30 |
Correct |
26 ms |
4900 KB |
Output is correct |
31 |
Correct |
25 ms |
4592 KB |
Output is correct |
32 |
Correct |
29 ms |
4912 KB |
Output is correct |
33 |
Correct |
26 ms |
4944 KB |
Output is correct |
34 |
Correct |
22 ms |
4956 KB |
Output is correct |
35 |
Correct |
27 ms |
5068 KB |
Output is correct |
36 |
Correct |
0 ms |
452 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
4463 ms |
105960 KB |
Output is correct |
40 |
Correct |
4302 ms |
105820 KB |
Output is correct |
41 |
Correct |
4439 ms |
105760 KB |
Output is correct |
42 |
Correct |
3647 ms |
107600 KB |
Output is correct |
43 |
Correct |
3539 ms |
107856 KB |
Output is correct |
44 |
Correct |
3713 ms |
107932 KB |
Output is correct |
45 |
Correct |
669 ms |
105652 KB |
Output is correct |
46 |
Correct |
710 ms |
107620 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
856 ms |
104292 KB |
Output is correct |
49 |
Correct |
828 ms |
104280 KB |
Output is correct |
50 |
Correct |
776 ms |
104220 KB |
Output is correct |
51 |
Correct |
801 ms |
104452 KB |
Output is correct |
52 |
Correct |
784 ms |
104280 KB |
Output is correct |
53 |
Correct |
818 ms |
104280 KB |
Output is correct |
54 |
Correct |
784 ms |
104304 KB |
Output is correct |
55 |
Correct |
709 ms |
104180 KB |
Output is correct |
56 |
Correct |
815 ms |
104048 KB |
Output is correct |
57 |
Correct |
776 ms |
104072 KB |
Output is correct |
58 |
Correct |
779 ms |
104196 KB |
Output is correct |
59 |
Correct |
790 ms |
104020 KB |
Output is correct |
60 |
Correct |
719 ms |
104832 KB |
Output is correct |
61 |
Correct |
618 ms |
104032 KB |
Output is correct |
62 |
Correct |
328 ms |
7296 KB |
Output is correct |
63 |
Correct |
287 ms |
7032 KB |
Output is correct |
64 |
Correct |
291 ms |
7300 KB |
Output is correct |
65 |
Correct |
339 ms |
7064 KB |
Output is correct |
66 |
Correct |
323 ms |
7288 KB |
Output is correct |
67 |
Correct |
337 ms |
7292 KB |
Output is correct |
68 |
Correct |
331 ms |
7064 KB |
Output is correct |
69 |
Correct |
327 ms |
7148 KB |
Output is correct |
70 |
Correct |
136 ms |
7048 KB |
Output is correct |
71 |
Correct |
39 ms |
7520 KB |
Output is correct |
72 |
Correct |
353 ms |
7288 KB |
Output is correct |
73 |
Correct |
327 ms |
7364 KB |
Output is correct |
74 |
Correct |
314 ms |
7724 KB |
Output is correct |
75 |
Correct |
314 ms |
9120 KB |
Output is correct |
76 |
Correct |
36 ms |
11264 KB |
Output is correct |
77 |
Correct |
42 ms |
8648 KB |
Output is correct |
78 |
Correct |
4864 ms |
104828 KB |
Output is correct |
79 |
Correct |
4215 ms |
106764 KB |
Output is correct |
80 |
Correct |
4195 ms |
106028 KB |
Output is correct |
81 |
Correct |
4383 ms |
105832 KB |
Output is correct |
82 |
Correct |
4541 ms |
104808 KB |
Output is correct |
83 |
Correct |
4690 ms |
104808 KB |
Output is correct |
84 |
Correct |
4810 ms |
104748 KB |
Output is correct |
85 |
Correct |
4909 ms |
104728 KB |
Output is correct |
86 |
Correct |
913 ms |
105100 KB |
Output is correct |
87 |
Correct |
735 ms |
106064 KB |
Output is correct |
88 |
Correct |
4486 ms |
104844 KB |
Output is correct |
89 |
Correct |
4535 ms |
104848 KB |
Output is correct |
90 |
Correct |
3066 ms |
105344 KB |
Output is correct |
91 |
Correct |
3017 ms |
104884 KB |
Output is correct |
92 |
Correct |
722 ms |
114368 KB |
Output is correct |
93 |
Correct |
635 ms |
105480 KB |
Output is correct |