#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 12, MOD = (int)1e9 + 3;
#define int ll
int n, m, vis[N], timer = 0, p[N], f[N];
vector<array<int, 3>> e(N);
vector<vector<int>> tr;
vector<int> cur;
int id(int v, int u) {
vector<vector<pair<int,int>>> g(n + 1);
for(int i:cur) {
g[e[i][1]].push_back({e[i][2], i});
g[e[i][2]].push_back({e[i][1], i});
}
queue<int> q;
q.push(v);
++timer;
vis[v] = timer;
while(!q.empty()) {
int x = q.front();
q.pop();
// cout << x << ' ' << p[x] << '\n';
for(auto [to, i]:g[x]) {
// cout << to << "x\n";
if(vis[to] != timer) {
vis[to] = timer;
p[to] = x;
f[to] = i;
q.push(to);
}
}
}
int ret = 1e9;
while(u != v) {
ret = min(ret, f[u]);
u = p[u];
// cerr << u << ' ' << v << '\n';
}
return ret;
}
int P[N];
int get(int a) {
if(P[a] == a) return a;
return P[a] = get(P[a]);
}
bool merge(int a, int b) {
a = get(a);
b = get(b);
if(a == b) return false;
P[a] = b;
return true;
}
ll cost(vector<int> &x, int val) {
// cout << (int)x.size() << '\n';
ll ret = 0;
for(int i:x) {
ret += abs(val - e[i][0]);
}
return ret;
}
int l[N], r[N];
const int inf = (int)1e9;
void test() {
cin >> n >> m;
for(int i = 0;i < m; i++) {
cin >> e[i][1] >> e[i][2] >> e[i][0];
l[i] = 0, r[i] = inf;
}
iota(P + 1, P + n + 1, 1);
sort(e.begin(), e.begin() + m);
int lst = 0;
for(int i = 0;i < m; i++) {
if(merge(e[i][2], e[i][1])) {
cur.push_back(i);
continue;
}
int j = id(e[i][1], e[i][2]);
ll val = (e[i][0] + e[j][0] + 1) / 2;
l[i] = val;
r[j] = val - 1;
for(int k = 0; k < (int)cur.size(); k++) {
if(cur[k] == j) {
cur.erase(cur.begin() + k);
break;
}
}
cur.push_back(i);
}
vector<array<int, 3>> o;
for(int i = 0; i < m; i++) {
o.push_back({l[i], r[i], e[i][0]});
}
sort(o.begin(), o.end());
multiset<int> L, R;
multiset<pair<int, int>> st;
ll _l = 0, _r = 0;
int q;
cin >> q;
int it = 0;
while(q--) {
int x;
cin >> x;
ll res = 0;
auto add = [&](int val) {
if(val <= x) {
_l += val;
L.insert(val);
} else {
_r += val;
R.insert(val);
}
};
auto del = [&](int val) {
if(L.find(val) != L.end()) {
L.erase(L.find(val));
_l -= val;
} else {
R.erase(R.find(val));
_r -= val;
}
};
auto f = [&](multiset<int> &t, ll s) {
res += abs(s - x * 1ll * (int)t.size());
};
while(it < m && o[it][0] <= x) {
st.insert({o[it][1], o[it][2]});
add(o[it][2]);
it++;
}
while(!st.empty()) {
auto [d, e] = (*st.begin());
if(d < x) {
del(e);
st.erase(st.find({d,e}));
} else {
break;
}
}
while(!R.empty()) {
int val = (*R.begin());
// cerr << val << ' ' << x << '\n';
if(val <= x) {
R.erase(R.find(val));
_r -= val;
add(val);
} else {
break;
}
}
f(L, _l);
f(R, _r);
cout << res << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
return 0;
}
Compilation message
reconstruction.cpp: In function 'void test()':
reconstruction.cpp:75:9: warning: unused variable 'lst' [-Wunused-variable]
75 | int lst = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
23900 KB |
Output is correct |
2 |
Correct |
9 ms |
23976 KB |
Output is correct |
3 |
Correct |
10 ms |
23900 KB |
Output is correct |
4 |
Correct |
9 ms |
23900 KB |
Output is correct |
5 |
Correct |
9 ms |
23940 KB |
Output is correct |
6 |
Correct |
9 ms |
23900 KB |
Output is correct |
7 |
Correct |
9 ms |
23968 KB |
Output is correct |
8 |
Correct |
9 ms |
23896 KB |
Output is correct |
9 |
Correct |
10 ms |
23900 KB |
Output is correct |
10 |
Correct |
10 ms |
23900 KB |
Output is correct |
11 |
Correct |
8 ms |
23900 KB |
Output is correct |
12 |
Correct |
9 ms |
23900 KB |
Output is correct |
13 |
Correct |
9 ms |
23840 KB |
Output is correct |
14 |
Correct |
9 ms |
23900 KB |
Output is correct |
15 |
Correct |
9 ms |
23936 KB |
Output is correct |
16 |
Correct |
8 ms |
23900 KB |
Output is correct |
17 |
Correct |
8 ms |
23972 KB |
Output is correct |
18 |
Correct |
8 ms |
23956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
23900 KB |
Output is correct |
2 |
Correct |
9 ms |
23976 KB |
Output is correct |
3 |
Correct |
10 ms |
23900 KB |
Output is correct |
4 |
Correct |
9 ms |
23900 KB |
Output is correct |
5 |
Correct |
9 ms |
23940 KB |
Output is correct |
6 |
Correct |
9 ms |
23900 KB |
Output is correct |
7 |
Correct |
9 ms |
23968 KB |
Output is correct |
8 |
Correct |
9 ms |
23896 KB |
Output is correct |
9 |
Correct |
10 ms |
23900 KB |
Output is correct |
10 |
Correct |
10 ms |
23900 KB |
Output is correct |
11 |
Correct |
8 ms |
23900 KB |
Output is correct |
12 |
Correct |
9 ms |
23900 KB |
Output is correct |
13 |
Correct |
9 ms |
23840 KB |
Output is correct |
14 |
Correct |
9 ms |
23900 KB |
Output is correct |
15 |
Correct |
9 ms |
23936 KB |
Output is correct |
16 |
Correct |
8 ms |
23900 KB |
Output is correct |
17 |
Correct |
8 ms |
23972 KB |
Output is correct |
18 |
Correct |
8 ms |
23956 KB |
Output is correct |
19 |
Correct |
10 ms |
23900 KB |
Output is correct |
20 |
Correct |
4349 ms |
32308 KB |
Output is correct |
21 |
Correct |
4168 ms |
33328 KB |
Output is correct |
22 |
Correct |
4333 ms |
32296 KB |
Output is correct |
23 |
Correct |
4436 ms |
34084 KB |
Output is correct |
24 |
Correct |
4555 ms |
33580 KB |
Output is correct |
25 |
Correct |
4384 ms |
34252 KB |
Output is correct |
26 |
Correct |
4426 ms |
32308 KB |
Output is correct |
27 |
Correct |
4389 ms |
31440 KB |
Output is correct |
28 |
Correct |
4282 ms |
33580 KB |
Output is correct |
29 |
Correct |
3463 ms |
34504 KB |
Output is correct |
30 |
Correct |
4334 ms |
32972 KB |
Output is correct |
31 |
Correct |
4420 ms |
32972 KB |
Output is correct |
32 |
Correct |
4426 ms |
35024 KB |
Output is correct |
33 |
Correct |
4394 ms |
30520 KB |
Output is correct |
34 |
Correct |
2687 ms |
30624 KB |
Output is correct |
35 |
Correct |
4403 ms |
35120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
9 ms |
23900 KB |
Output is correct |
3 |
Correct |
10 ms |
23900 KB |
Output is correct |
4 |
Correct |
4485 ms |
50384 KB |
Output is correct |
5 |
Correct |
4527 ms |
50428 KB |
Output is correct |
6 |
Correct |
4582 ms |
50444 KB |
Output is correct |
7 |
Correct |
2701 ms |
52184 KB |
Output is correct |
8 |
Correct |
2291 ms |
52460 KB |
Output is correct |
9 |
Correct |
2233 ms |
52432 KB |
Output is correct |
10 |
Correct |
4572 ms |
55752 KB |
Output is correct |
11 |
Correct |
2328 ms |
57580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
23900 KB |
Output is correct |
2 |
Correct |
9 ms |
23976 KB |
Output is correct |
3 |
Correct |
10 ms |
23900 KB |
Output is correct |
4 |
Correct |
9 ms |
23900 KB |
Output is correct |
5 |
Correct |
9 ms |
23940 KB |
Output is correct |
6 |
Correct |
9 ms |
23900 KB |
Output is correct |
7 |
Correct |
9 ms |
23968 KB |
Output is correct |
8 |
Correct |
9 ms |
23896 KB |
Output is correct |
9 |
Correct |
10 ms |
23900 KB |
Output is correct |
10 |
Correct |
10 ms |
23900 KB |
Output is correct |
11 |
Correct |
8 ms |
23900 KB |
Output is correct |
12 |
Correct |
9 ms |
23900 KB |
Output is correct |
13 |
Correct |
9 ms |
23840 KB |
Output is correct |
14 |
Correct |
9 ms |
23900 KB |
Output is correct |
15 |
Correct |
9 ms |
23936 KB |
Output is correct |
16 |
Correct |
8 ms |
23900 KB |
Output is correct |
17 |
Correct |
8 ms |
23972 KB |
Output is correct |
18 |
Correct |
8 ms |
23956 KB |
Output is correct |
19 |
Correct |
9 ms |
23900 KB |
Output is correct |
20 |
Correct |
157 ms |
45944 KB |
Output is correct |
21 |
Correct |
184 ms |
46164 KB |
Output is correct |
22 |
Correct |
154 ms |
46040 KB |
Output is correct |
23 |
Correct |
157 ms |
45908 KB |
Output is correct |
24 |
Correct |
173 ms |
45904 KB |
Output is correct |
25 |
Correct |
167 ms |
45908 KB |
Output is correct |
26 |
Correct |
157 ms |
45936 KB |
Output is correct |
27 |
Correct |
160 ms |
45908 KB |
Output is correct |
28 |
Correct |
155 ms |
45908 KB |
Output is correct |
29 |
Correct |
157 ms |
45904 KB |
Output is correct |
30 |
Correct |
159 ms |
46164 KB |
Output is correct |
31 |
Correct |
161 ms |
45908 KB |
Output is correct |
32 |
Correct |
149 ms |
46484 KB |
Output is correct |
33 |
Correct |
159 ms |
45860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
23900 KB |
Output is correct |
2 |
Correct |
9 ms |
23976 KB |
Output is correct |
3 |
Correct |
10 ms |
23900 KB |
Output is correct |
4 |
Correct |
9 ms |
23900 KB |
Output is correct |
5 |
Correct |
9 ms |
23940 KB |
Output is correct |
6 |
Correct |
9 ms |
23900 KB |
Output is correct |
7 |
Correct |
9 ms |
23968 KB |
Output is correct |
8 |
Correct |
9 ms |
23896 KB |
Output is correct |
9 |
Correct |
10 ms |
23900 KB |
Output is correct |
10 |
Correct |
10 ms |
23900 KB |
Output is correct |
11 |
Correct |
8 ms |
23900 KB |
Output is correct |
12 |
Correct |
9 ms |
23900 KB |
Output is correct |
13 |
Correct |
9 ms |
23840 KB |
Output is correct |
14 |
Correct |
9 ms |
23900 KB |
Output is correct |
15 |
Correct |
9 ms |
23936 KB |
Output is correct |
16 |
Correct |
8 ms |
23900 KB |
Output is correct |
17 |
Correct |
8 ms |
23972 KB |
Output is correct |
18 |
Correct |
8 ms |
23956 KB |
Output is correct |
19 |
Correct |
10 ms |
23900 KB |
Output is correct |
20 |
Correct |
4349 ms |
32308 KB |
Output is correct |
21 |
Correct |
4168 ms |
33328 KB |
Output is correct |
22 |
Correct |
4333 ms |
32296 KB |
Output is correct |
23 |
Correct |
4436 ms |
34084 KB |
Output is correct |
24 |
Correct |
4555 ms |
33580 KB |
Output is correct |
25 |
Correct |
4384 ms |
34252 KB |
Output is correct |
26 |
Correct |
4426 ms |
32308 KB |
Output is correct |
27 |
Correct |
4389 ms |
31440 KB |
Output is correct |
28 |
Correct |
4282 ms |
33580 KB |
Output is correct |
29 |
Correct |
3463 ms |
34504 KB |
Output is correct |
30 |
Correct |
4334 ms |
32972 KB |
Output is correct |
31 |
Correct |
4420 ms |
32972 KB |
Output is correct |
32 |
Correct |
4426 ms |
35024 KB |
Output is correct |
33 |
Correct |
4394 ms |
30520 KB |
Output is correct |
34 |
Correct |
2687 ms |
30624 KB |
Output is correct |
35 |
Correct |
4403 ms |
35120 KB |
Output is correct |
36 |
Correct |
4394 ms |
30712 KB |
Output is correct |
37 |
Correct |
3965 ms |
30704 KB |
Output is correct |
38 |
Correct |
4336 ms |
30712 KB |
Output is correct |
39 |
Correct |
4460 ms |
30676 KB |
Output is correct |
40 |
Correct |
4663 ms |
30712 KB |
Output is correct |
41 |
Correct |
4394 ms |
30712 KB |
Output is correct |
42 |
Correct |
4485 ms |
30672 KB |
Output is correct |
43 |
Correct |
4301 ms |
30712 KB |
Output is correct |
44 |
Correct |
4411 ms |
30720 KB |
Output is correct |
45 |
Correct |
3311 ms |
31220 KB |
Output is correct |
46 |
Correct |
4353 ms |
30676 KB |
Output is correct |
47 |
Correct |
4325 ms |
30676 KB |
Output is correct |
48 |
Correct |
4331 ms |
35316 KB |
Output is correct |
49 |
Correct |
4324 ms |
30708 KB |
Output is correct |
50 |
Correct |
2670 ms |
30840 KB |
Output is correct |
51 |
Correct |
4336 ms |
35288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
23900 KB |
Output is correct |
2 |
Correct |
9 ms |
23976 KB |
Output is correct |
3 |
Correct |
10 ms |
23900 KB |
Output is correct |
4 |
Correct |
9 ms |
23900 KB |
Output is correct |
5 |
Correct |
9 ms |
23940 KB |
Output is correct |
6 |
Correct |
9 ms |
23900 KB |
Output is correct |
7 |
Correct |
9 ms |
23968 KB |
Output is correct |
8 |
Correct |
9 ms |
23896 KB |
Output is correct |
9 |
Correct |
10 ms |
23900 KB |
Output is correct |
10 |
Correct |
10 ms |
23900 KB |
Output is correct |
11 |
Correct |
8 ms |
23900 KB |
Output is correct |
12 |
Correct |
9 ms |
23900 KB |
Output is correct |
13 |
Correct |
9 ms |
23840 KB |
Output is correct |
14 |
Correct |
9 ms |
23900 KB |
Output is correct |
15 |
Correct |
9 ms |
23936 KB |
Output is correct |
16 |
Correct |
8 ms |
23900 KB |
Output is correct |
17 |
Correct |
8 ms |
23972 KB |
Output is correct |
18 |
Correct |
8 ms |
23956 KB |
Output is correct |
19 |
Correct |
10 ms |
23900 KB |
Output is correct |
20 |
Correct |
4349 ms |
32308 KB |
Output is correct |
21 |
Correct |
4168 ms |
33328 KB |
Output is correct |
22 |
Correct |
4333 ms |
32296 KB |
Output is correct |
23 |
Correct |
4436 ms |
34084 KB |
Output is correct |
24 |
Correct |
4555 ms |
33580 KB |
Output is correct |
25 |
Correct |
4384 ms |
34252 KB |
Output is correct |
26 |
Correct |
4426 ms |
32308 KB |
Output is correct |
27 |
Correct |
4389 ms |
31440 KB |
Output is correct |
28 |
Correct |
4282 ms |
33580 KB |
Output is correct |
29 |
Correct |
3463 ms |
34504 KB |
Output is correct |
30 |
Correct |
4334 ms |
32972 KB |
Output is correct |
31 |
Correct |
4420 ms |
32972 KB |
Output is correct |
32 |
Correct |
4426 ms |
35024 KB |
Output is correct |
33 |
Correct |
4394 ms |
30520 KB |
Output is correct |
34 |
Correct |
2687 ms |
30624 KB |
Output is correct |
35 |
Correct |
4403 ms |
35120 KB |
Output is correct |
36 |
Correct |
10 ms |
23900 KB |
Output is correct |
37 |
Correct |
9 ms |
23900 KB |
Output is correct |
38 |
Correct |
10 ms |
23900 KB |
Output is correct |
39 |
Correct |
4485 ms |
50384 KB |
Output is correct |
40 |
Correct |
4527 ms |
50428 KB |
Output is correct |
41 |
Correct |
4582 ms |
50444 KB |
Output is correct |
42 |
Correct |
2701 ms |
52184 KB |
Output is correct |
43 |
Correct |
2291 ms |
52460 KB |
Output is correct |
44 |
Correct |
2233 ms |
52432 KB |
Output is correct |
45 |
Correct |
4572 ms |
55752 KB |
Output is correct |
46 |
Correct |
2328 ms |
57580 KB |
Output is correct |
47 |
Correct |
9 ms |
23900 KB |
Output is correct |
48 |
Correct |
157 ms |
45944 KB |
Output is correct |
49 |
Correct |
184 ms |
46164 KB |
Output is correct |
50 |
Correct |
154 ms |
46040 KB |
Output is correct |
51 |
Correct |
157 ms |
45908 KB |
Output is correct |
52 |
Correct |
173 ms |
45904 KB |
Output is correct |
53 |
Correct |
167 ms |
45908 KB |
Output is correct |
54 |
Correct |
157 ms |
45936 KB |
Output is correct |
55 |
Correct |
160 ms |
45908 KB |
Output is correct |
56 |
Correct |
155 ms |
45908 KB |
Output is correct |
57 |
Correct |
157 ms |
45904 KB |
Output is correct |
58 |
Correct |
159 ms |
46164 KB |
Output is correct |
59 |
Correct |
161 ms |
45908 KB |
Output is correct |
60 |
Correct |
149 ms |
46484 KB |
Output is correct |
61 |
Correct |
159 ms |
45860 KB |
Output is correct |
62 |
Correct |
4394 ms |
30712 KB |
Output is correct |
63 |
Correct |
3965 ms |
30704 KB |
Output is correct |
64 |
Correct |
4336 ms |
30712 KB |
Output is correct |
65 |
Correct |
4460 ms |
30676 KB |
Output is correct |
66 |
Correct |
4663 ms |
30712 KB |
Output is correct |
67 |
Correct |
4394 ms |
30712 KB |
Output is correct |
68 |
Correct |
4485 ms |
30672 KB |
Output is correct |
69 |
Correct |
4301 ms |
30712 KB |
Output is correct |
70 |
Correct |
4411 ms |
30720 KB |
Output is correct |
71 |
Correct |
3311 ms |
31220 KB |
Output is correct |
72 |
Correct |
4353 ms |
30676 KB |
Output is correct |
73 |
Correct |
4325 ms |
30676 KB |
Output is correct |
74 |
Correct |
4331 ms |
35316 KB |
Output is correct |
75 |
Correct |
4324 ms |
30708 KB |
Output is correct |
76 |
Correct |
2670 ms |
30840 KB |
Output is correct |
77 |
Correct |
4336 ms |
35288 KB |
Output is correct |
78 |
Correct |
4555 ms |
49404 KB |
Output is correct |
79 |
Correct |
3989 ms |
51408 KB |
Output is correct |
80 |
Correct |
4390 ms |
50432 KB |
Output is correct |
81 |
Correct |
4501 ms |
50400 KB |
Output is correct |
82 |
Correct |
4613 ms |
49400 KB |
Output is correct |
83 |
Correct |
4462 ms |
49356 KB |
Output is correct |
84 |
Correct |
4493 ms |
49356 KB |
Output is correct |
85 |
Correct |
4539 ms |
49340 KB |
Output is correct |
86 |
Correct |
4600 ms |
49408 KB |
Output is correct |
87 |
Correct |
3464 ms |
52236 KB |
Output is correct |
88 |
Correct |
4564 ms |
49404 KB |
Output is correct |
89 |
Correct |
4520 ms |
49400 KB |
Output is correct |
90 |
Correct |
4541 ms |
54776 KB |
Output is correct |
91 |
Correct |
4578 ms |
49400 KB |
Output is correct |
92 |
Correct |
2813 ms |
52428 KB |
Output is correct |
93 |
Correct |
4476 ms |
55752 KB |
Output is correct |