#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);
}
// for(int i = 0; i < m; i++) {
// cout << l[i] << ' ' << r[i] << '\n';
// }
int q;
cin >> q;
while(q--) {
int x;
cin >> x;
ll res = 0;
for(int i = 0; i < m; i++) {
if(l[i] <= x && x <= r[i]) {
res += abs(x - e[i][0]);
}
}
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 |
23972 KB |
Output is correct |
2 |
Correct |
8 ms |
23900 KB |
Output is correct |
3 |
Correct |
8 ms |
23900 KB |
Output is correct |
4 |
Correct |
9 ms |
23900 KB |
Output is correct |
5 |
Correct |
8 ms |
23972 KB |
Output is correct |
6 |
Correct |
8 ms |
23900 KB |
Output is correct |
7 |
Correct |
9 ms |
23900 KB |
Output is correct |
8 |
Correct |
8 ms |
23900 KB |
Output is correct |
9 |
Correct |
8 ms |
23900 KB |
Output is correct |
10 |
Correct |
8 ms |
23820 KB |
Output is correct |
11 |
Correct |
9 ms |
23896 KB |
Output is correct |
12 |
Correct |
8 ms |
23848 KB |
Output is correct |
13 |
Correct |
8 ms |
23952 KB |
Output is correct |
14 |
Correct |
8 ms |
23968 KB |
Output is correct |
15 |
Correct |
8 ms |
23900 KB |
Output is correct |
16 |
Correct |
8 ms |
23804 KB |
Output is correct |
17 |
Correct |
8 ms |
23904 KB |
Output is correct |
18 |
Correct |
8 ms |
23900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
23972 KB |
Output is correct |
2 |
Correct |
8 ms |
23900 KB |
Output is correct |
3 |
Correct |
8 ms |
23900 KB |
Output is correct |
4 |
Correct |
9 ms |
23900 KB |
Output is correct |
5 |
Correct |
8 ms |
23972 KB |
Output is correct |
6 |
Correct |
8 ms |
23900 KB |
Output is correct |
7 |
Correct |
9 ms |
23900 KB |
Output is correct |
8 |
Correct |
8 ms |
23900 KB |
Output is correct |
9 |
Correct |
8 ms |
23900 KB |
Output is correct |
10 |
Correct |
8 ms |
23820 KB |
Output is correct |
11 |
Correct |
9 ms |
23896 KB |
Output is correct |
12 |
Correct |
8 ms |
23848 KB |
Output is correct |
13 |
Correct |
8 ms |
23952 KB |
Output is correct |
14 |
Correct |
8 ms |
23968 KB |
Output is correct |
15 |
Correct |
8 ms |
23900 KB |
Output is correct |
16 |
Correct |
8 ms |
23804 KB |
Output is correct |
17 |
Correct |
8 ms |
23904 KB |
Output is correct |
18 |
Correct |
8 ms |
23900 KB |
Output is correct |
19 |
Correct |
9 ms |
23896 KB |
Output is correct |
20 |
Correct |
4283 ms |
27212 KB |
Output is correct |
21 |
Correct |
3843 ms |
27320 KB |
Output is correct |
22 |
Correct |
4250 ms |
27260 KB |
Output is correct |
23 |
Correct |
4329 ms |
27256 KB |
Output is correct |
24 |
Correct |
4327 ms |
27320 KB |
Output is correct |
25 |
Correct |
4210 ms |
27216 KB |
Output is correct |
26 |
Correct |
4269 ms |
27224 KB |
Output is correct |
27 |
Correct |
4326 ms |
27324 KB |
Output is correct |
28 |
Correct |
4276 ms |
27228 KB |
Output is correct |
29 |
Correct |
3285 ms |
27220 KB |
Output is correct |
30 |
Correct |
4337 ms |
27216 KB |
Output is correct |
31 |
Correct |
4226 ms |
27276 KB |
Output is correct |
32 |
Correct |
4450 ms |
27324 KB |
Output is correct |
33 |
Correct |
4280 ms |
27324 KB |
Output is correct |
34 |
Correct |
2671 ms |
27228 KB |
Output is correct |
35 |
Correct |
4214 ms |
27216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23896 KB |
Output is correct |
2 |
Correct |
9 ms |
23920 KB |
Output is correct |
3 |
Correct |
9 ms |
23752 KB |
Output is correct |
4 |
Execution timed out |
5037 ms |
27508 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
23972 KB |
Output is correct |
2 |
Correct |
8 ms |
23900 KB |
Output is correct |
3 |
Correct |
8 ms |
23900 KB |
Output is correct |
4 |
Correct |
9 ms |
23900 KB |
Output is correct |
5 |
Correct |
8 ms |
23972 KB |
Output is correct |
6 |
Correct |
8 ms |
23900 KB |
Output is correct |
7 |
Correct |
9 ms |
23900 KB |
Output is correct |
8 |
Correct |
8 ms |
23900 KB |
Output is correct |
9 |
Correct |
8 ms |
23900 KB |
Output is correct |
10 |
Correct |
8 ms |
23820 KB |
Output is correct |
11 |
Correct |
9 ms |
23896 KB |
Output is correct |
12 |
Correct |
8 ms |
23848 KB |
Output is correct |
13 |
Correct |
8 ms |
23952 KB |
Output is correct |
14 |
Correct |
8 ms |
23968 KB |
Output is correct |
15 |
Correct |
8 ms |
23900 KB |
Output is correct |
16 |
Correct |
8 ms |
23804 KB |
Output is correct |
17 |
Correct |
8 ms |
23904 KB |
Output is correct |
18 |
Correct |
8 ms |
23900 KB |
Output is correct |
19 |
Correct |
9 ms |
23900 KB |
Output is correct |
20 |
Correct |
1089 ms |
45948 KB |
Output is correct |
21 |
Correct |
1103 ms |
45908 KB |
Output is correct |
22 |
Correct |
1119 ms |
45908 KB |
Output is correct |
23 |
Correct |
1088 ms |
46052 KB |
Output is correct |
24 |
Correct |
1075 ms |
45908 KB |
Output is correct |
25 |
Correct |
1098 ms |
45904 KB |
Output is correct |
26 |
Correct |
1084 ms |
45960 KB |
Output is correct |
27 |
Correct |
1071 ms |
45908 KB |
Output is correct |
28 |
Correct |
1073 ms |
45904 KB |
Output is correct |
29 |
Correct |
1078 ms |
45896 KB |
Output is correct |
30 |
Correct |
1087 ms |
46116 KB |
Output is correct |
31 |
Correct |
1064 ms |
45652 KB |
Output is correct |
32 |
Correct |
1079 ms |
46420 KB |
Output is correct |
33 |
Correct |
1076 ms |
45904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
23972 KB |
Output is correct |
2 |
Correct |
8 ms |
23900 KB |
Output is correct |
3 |
Correct |
8 ms |
23900 KB |
Output is correct |
4 |
Correct |
9 ms |
23900 KB |
Output is correct |
5 |
Correct |
8 ms |
23972 KB |
Output is correct |
6 |
Correct |
8 ms |
23900 KB |
Output is correct |
7 |
Correct |
9 ms |
23900 KB |
Output is correct |
8 |
Correct |
8 ms |
23900 KB |
Output is correct |
9 |
Correct |
8 ms |
23900 KB |
Output is correct |
10 |
Correct |
8 ms |
23820 KB |
Output is correct |
11 |
Correct |
9 ms |
23896 KB |
Output is correct |
12 |
Correct |
8 ms |
23848 KB |
Output is correct |
13 |
Correct |
8 ms |
23952 KB |
Output is correct |
14 |
Correct |
8 ms |
23968 KB |
Output is correct |
15 |
Correct |
8 ms |
23900 KB |
Output is correct |
16 |
Correct |
8 ms |
23804 KB |
Output is correct |
17 |
Correct |
8 ms |
23904 KB |
Output is correct |
18 |
Correct |
8 ms |
23900 KB |
Output is correct |
19 |
Correct |
9 ms |
23896 KB |
Output is correct |
20 |
Correct |
4283 ms |
27212 KB |
Output is correct |
21 |
Correct |
3843 ms |
27320 KB |
Output is correct |
22 |
Correct |
4250 ms |
27260 KB |
Output is correct |
23 |
Correct |
4329 ms |
27256 KB |
Output is correct |
24 |
Correct |
4327 ms |
27320 KB |
Output is correct |
25 |
Correct |
4210 ms |
27216 KB |
Output is correct |
26 |
Correct |
4269 ms |
27224 KB |
Output is correct |
27 |
Correct |
4326 ms |
27324 KB |
Output is correct |
28 |
Correct |
4276 ms |
27228 KB |
Output is correct |
29 |
Correct |
3285 ms |
27220 KB |
Output is correct |
30 |
Correct |
4337 ms |
27216 KB |
Output is correct |
31 |
Correct |
4226 ms |
27276 KB |
Output is correct |
32 |
Correct |
4450 ms |
27324 KB |
Output is correct |
33 |
Correct |
4280 ms |
27324 KB |
Output is correct |
34 |
Correct |
2671 ms |
27228 KB |
Output is correct |
35 |
Correct |
4214 ms |
27216 KB |
Output is correct |
36 |
Execution timed out |
5038 ms |
27516 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
23972 KB |
Output is correct |
2 |
Correct |
8 ms |
23900 KB |
Output is correct |
3 |
Correct |
8 ms |
23900 KB |
Output is correct |
4 |
Correct |
9 ms |
23900 KB |
Output is correct |
5 |
Correct |
8 ms |
23972 KB |
Output is correct |
6 |
Correct |
8 ms |
23900 KB |
Output is correct |
7 |
Correct |
9 ms |
23900 KB |
Output is correct |
8 |
Correct |
8 ms |
23900 KB |
Output is correct |
9 |
Correct |
8 ms |
23900 KB |
Output is correct |
10 |
Correct |
8 ms |
23820 KB |
Output is correct |
11 |
Correct |
9 ms |
23896 KB |
Output is correct |
12 |
Correct |
8 ms |
23848 KB |
Output is correct |
13 |
Correct |
8 ms |
23952 KB |
Output is correct |
14 |
Correct |
8 ms |
23968 KB |
Output is correct |
15 |
Correct |
8 ms |
23900 KB |
Output is correct |
16 |
Correct |
8 ms |
23804 KB |
Output is correct |
17 |
Correct |
8 ms |
23904 KB |
Output is correct |
18 |
Correct |
8 ms |
23900 KB |
Output is correct |
19 |
Correct |
9 ms |
23896 KB |
Output is correct |
20 |
Correct |
4283 ms |
27212 KB |
Output is correct |
21 |
Correct |
3843 ms |
27320 KB |
Output is correct |
22 |
Correct |
4250 ms |
27260 KB |
Output is correct |
23 |
Correct |
4329 ms |
27256 KB |
Output is correct |
24 |
Correct |
4327 ms |
27320 KB |
Output is correct |
25 |
Correct |
4210 ms |
27216 KB |
Output is correct |
26 |
Correct |
4269 ms |
27224 KB |
Output is correct |
27 |
Correct |
4326 ms |
27324 KB |
Output is correct |
28 |
Correct |
4276 ms |
27228 KB |
Output is correct |
29 |
Correct |
3285 ms |
27220 KB |
Output is correct |
30 |
Correct |
4337 ms |
27216 KB |
Output is correct |
31 |
Correct |
4226 ms |
27276 KB |
Output is correct |
32 |
Correct |
4450 ms |
27324 KB |
Output is correct |
33 |
Correct |
4280 ms |
27324 KB |
Output is correct |
34 |
Correct |
2671 ms |
27228 KB |
Output is correct |
35 |
Correct |
4214 ms |
27216 KB |
Output is correct |
36 |
Correct |
10 ms |
23896 KB |
Output is correct |
37 |
Correct |
9 ms |
23920 KB |
Output is correct |
38 |
Correct |
9 ms |
23752 KB |
Output is correct |
39 |
Execution timed out |
5037 ms |
27508 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |