//Challenge: Accepted
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 505
#define maxm 100005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const int inf = 1<<30;
struct edge{
int u, v, w, id;
edge(){u = v = w = id = 0;}
edge(int a, int b, int c, int d){u = a, v = b, w = c, id = d;}
};
vector<int> wei;
inline void del(vector<pii> &v, pii x) {
v.erase(find(v.begin(), v.end(), x));
}
inline void del(vector<int> &v, int x) {
v.erase(find(v.begin(), v.end(), x));
}
vector<pii> ev;
vector<pii> adj[maxn];
pii best;
int bid = -1, t = 0;
bool getpath(int n, int par, int goal) {
if (n == goal) return 1;
for (auto [v, id]:adj[n]) {
if (v != par && getpath(v, n, goal)) {
if (id < bid) {
bid = id;
best = {n, v};
}
return 1;
}
}
return 0;
}
void upd(edge e) {
int u = e.u, v = e.v, w = e.w, id = e.id;
bid = inf;
if (u > v) swap(u, v);
if (getpath(u, 0, v)) {
if (best.ff > best.ss) swap(best.ff, best.ss);
del(adj[best.ff], make_pair(best.ss, bid));
del(adj[best.ss], make_pair(best.ff, bid));
ev.push_back({(wei[bid] + w + 1) / 2, bid});
ev.push_back({(wei[bid] + w + 1) / 2, id});
} else {
ev.push_back({0, id});
}
adj[u].push_back({v, t});
adj[v].push_back({u, t});
t++;
}
bool on[maxm];
int main() {
io
int n, m;
cin >> n >> m;
vector<edge> ed;
for (int i = 0;i < m;i++) {
int u, v, w;
cin >> u >> v >> w;
ed.push_back(edge(u, v, w, i));
}
sort(ed.begin(), ed.end(), [&] (edge x, edge y){return x.w < y.w;});
for (int i = 0;i < m;i++) wei.push_back(ed[i].w), ed[i].id = i;
for (int i = 0;i < m;i++) upd(ed[i]);
sort(ev.begin(), ev.end());
multiset<int> se;
int q;
cin >> q;
vector<int> que(q);
int ind = 0;
for (int i = 0;i < q;i++) {
cin >> que[i];
while (ind < ev.size() && ev[ind].ff <= que[i]) {
if (!on[ev[ind].ss]) {
se.insert(wei[ev[ind].ss]);
on[ev[ind].ss] = 1;
} else {
se.erase(se.find(wei[ev[ind].ss]));
}
ind++;
}
ll ans = 0;
for (int j:se) ans += abs(j - que[i]);
cout << ans << "\n";
}
}
Compilation message
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:95:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | while (ind < ev.size() && ev[ind].ff <= que[i]) {
| ~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
581 ms |
7616 KB |
Output is correct |
21 |
Correct |
341 ms |
7700 KB |
Output is correct |
22 |
Correct |
516 ms |
7624 KB |
Output is correct |
23 |
Correct |
532 ms |
7776 KB |
Output is correct |
24 |
Correct |
500 ms |
7636 KB |
Output is correct |
25 |
Correct |
584 ms |
7600 KB |
Output is correct |
26 |
Correct |
573 ms |
7676 KB |
Output is correct |
27 |
Correct |
582 ms |
7708 KB |
Output is correct |
28 |
Correct |
565 ms |
7636 KB |
Output is correct |
29 |
Correct |
613 ms |
7920 KB |
Output is correct |
30 |
Correct |
596 ms |
7924 KB |
Output is correct |
31 |
Correct |
623 ms |
7960 KB |
Output is correct |
32 |
Correct |
561 ms |
7940 KB |
Output is correct |
33 |
Correct |
594 ms |
7936 KB |
Output is correct |
34 |
Correct |
568 ms |
7848 KB |
Output is correct |
35 |
Correct |
593 ms |
7924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
3063 ms |
23892 KB |
Output is correct |
5 |
Correct |
2990 ms |
23944 KB |
Output is correct |
6 |
Correct |
2927 ms |
24056 KB |
Output is correct |
7 |
Correct |
2610 ms |
25796 KB |
Output is correct |
8 |
Correct |
2347 ms |
25840 KB |
Output is correct |
9 |
Correct |
2277 ms |
26948 KB |
Output is correct |
10 |
Correct |
2879 ms |
25552 KB |
Output is correct |
11 |
Correct |
2277 ms |
27544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
2518 ms |
18408 KB |
Output is correct |
21 |
Correct |
2588 ms |
18708 KB |
Output is correct |
22 |
Correct |
2602 ms |
18672 KB |
Output is correct |
23 |
Correct |
2484 ms |
18800 KB |
Output is correct |
24 |
Correct |
2557 ms |
18436 KB |
Output is correct |
25 |
Correct |
2518 ms |
18636 KB |
Output is correct |
26 |
Correct |
2538 ms |
18540 KB |
Output is correct |
27 |
Correct |
2350 ms |
18544 KB |
Output is correct |
28 |
Correct |
2470 ms |
26188 KB |
Output is correct |
29 |
Correct |
2472 ms |
26332 KB |
Output is correct |
30 |
Correct |
2586 ms |
26224 KB |
Output is correct |
31 |
Correct |
2399 ms |
25996 KB |
Output is correct |
32 |
Correct |
2190 ms |
26700 KB |
Output is correct |
33 |
Correct |
2620 ms |
26060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
581 ms |
7616 KB |
Output is correct |
21 |
Correct |
341 ms |
7700 KB |
Output is correct |
22 |
Correct |
516 ms |
7624 KB |
Output is correct |
23 |
Correct |
532 ms |
7776 KB |
Output is correct |
24 |
Correct |
500 ms |
7636 KB |
Output is correct |
25 |
Correct |
584 ms |
7600 KB |
Output is correct |
26 |
Correct |
573 ms |
7676 KB |
Output is correct |
27 |
Correct |
582 ms |
7708 KB |
Output is correct |
28 |
Correct |
565 ms |
7636 KB |
Output is correct |
29 |
Correct |
613 ms |
7920 KB |
Output is correct |
30 |
Correct |
596 ms |
7924 KB |
Output is correct |
31 |
Correct |
623 ms |
7960 KB |
Output is correct |
32 |
Correct |
561 ms |
7940 KB |
Output is correct |
33 |
Correct |
594 ms |
7936 KB |
Output is correct |
34 |
Correct |
568 ms |
7848 KB |
Output is correct |
35 |
Correct |
593 ms |
7924 KB |
Output is correct |
36 |
Correct |
643 ms |
8372 KB |
Output is correct |
37 |
Correct |
392 ms |
8456 KB |
Output is correct |
38 |
Correct |
572 ms |
8368 KB |
Output is correct |
39 |
Correct |
590 ms |
8444 KB |
Output is correct |
40 |
Correct |
570 ms |
8284 KB |
Output is correct |
41 |
Correct |
618 ms |
8300 KB |
Output is correct |
42 |
Correct |
633 ms |
8244 KB |
Output is correct |
43 |
Correct |
640 ms |
8256 KB |
Output is correct |
44 |
Correct |
621 ms |
8256 KB |
Output is correct |
45 |
Correct |
632 ms |
8388 KB |
Output is correct |
46 |
Correct |
647 ms |
8352 KB |
Output is correct |
47 |
Correct |
665 ms |
8320 KB |
Output is correct |
48 |
Correct |
668 ms |
8388 KB |
Output is correct |
49 |
Correct |
644 ms |
8388 KB |
Output is correct |
50 |
Correct |
607 ms |
8416 KB |
Output is correct |
51 |
Correct |
609 ms |
8208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
581 ms |
7616 KB |
Output is correct |
21 |
Correct |
341 ms |
7700 KB |
Output is correct |
22 |
Correct |
516 ms |
7624 KB |
Output is correct |
23 |
Correct |
532 ms |
7776 KB |
Output is correct |
24 |
Correct |
500 ms |
7636 KB |
Output is correct |
25 |
Correct |
584 ms |
7600 KB |
Output is correct |
26 |
Correct |
573 ms |
7676 KB |
Output is correct |
27 |
Correct |
582 ms |
7708 KB |
Output is correct |
28 |
Correct |
565 ms |
7636 KB |
Output is correct |
29 |
Correct |
613 ms |
7920 KB |
Output is correct |
30 |
Correct |
596 ms |
7924 KB |
Output is correct |
31 |
Correct |
623 ms |
7960 KB |
Output is correct |
32 |
Correct |
561 ms |
7940 KB |
Output is correct |
33 |
Correct |
594 ms |
7936 KB |
Output is correct |
34 |
Correct |
568 ms |
7848 KB |
Output is correct |
35 |
Correct |
593 ms |
7924 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
3063 ms |
23892 KB |
Output is correct |
40 |
Correct |
2990 ms |
23944 KB |
Output is correct |
41 |
Correct |
2927 ms |
24056 KB |
Output is correct |
42 |
Correct |
2610 ms |
25796 KB |
Output is correct |
43 |
Correct |
2347 ms |
25840 KB |
Output is correct |
44 |
Correct |
2277 ms |
26948 KB |
Output is correct |
45 |
Correct |
2879 ms |
25552 KB |
Output is correct |
46 |
Correct |
2277 ms |
27544 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
2518 ms |
18408 KB |
Output is correct |
49 |
Correct |
2588 ms |
18708 KB |
Output is correct |
50 |
Correct |
2602 ms |
18672 KB |
Output is correct |
51 |
Correct |
2484 ms |
18800 KB |
Output is correct |
52 |
Correct |
2557 ms |
18436 KB |
Output is correct |
53 |
Correct |
2518 ms |
18636 KB |
Output is correct |
54 |
Correct |
2538 ms |
18540 KB |
Output is correct |
55 |
Correct |
2350 ms |
18544 KB |
Output is correct |
56 |
Correct |
2470 ms |
26188 KB |
Output is correct |
57 |
Correct |
2472 ms |
26332 KB |
Output is correct |
58 |
Correct |
2586 ms |
26224 KB |
Output is correct |
59 |
Correct |
2399 ms |
25996 KB |
Output is correct |
60 |
Correct |
2190 ms |
26700 KB |
Output is correct |
61 |
Correct |
2620 ms |
26060 KB |
Output is correct |
62 |
Correct |
643 ms |
8372 KB |
Output is correct |
63 |
Correct |
392 ms |
8456 KB |
Output is correct |
64 |
Correct |
572 ms |
8368 KB |
Output is correct |
65 |
Correct |
590 ms |
8444 KB |
Output is correct |
66 |
Correct |
570 ms |
8284 KB |
Output is correct |
67 |
Correct |
618 ms |
8300 KB |
Output is correct |
68 |
Correct |
633 ms |
8244 KB |
Output is correct |
69 |
Correct |
640 ms |
8256 KB |
Output is correct |
70 |
Correct |
621 ms |
8256 KB |
Output is correct |
71 |
Correct |
632 ms |
8388 KB |
Output is correct |
72 |
Correct |
647 ms |
8352 KB |
Output is correct |
73 |
Correct |
665 ms |
8320 KB |
Output is correct |
74 |
Correct |
668 ms |
8388 KB |
Output is correct |
75 |
Correct |
644 ms |
8388 KB |
Output is correct |
76 |
Correct |
607 ms |
8416 KB |
Output is correct |
77 |
Correct |
609 ms |
8208 KB |
Output is correct |
78 |
Correct |
3445 ms |
31612 KB |
Output is correct |
79 |
Correct |
3147 ms |
33624 KB |
Output is correct |
80 |
Correct |
3342 ms |
32680 KB |
Output is correct |
81 |
Correct |
3339 ms |
32644 KB |
Output is correct |
82 |
Correct |
3431 ms |
31696 KB |
Output is correct |
83 |
Correct |
3559 ms |
31580 KB |
Output is correct |
84 |
Correct |
3760 ms |
31524 KB |
Output is correct |
85 |
Correct |
3415 ms |
31680 KB |
Output is correct |
86 |
Correct |
3105 ms |
31592 KB |
Output is correct |
87 |
Correct |
3017 ms |
33000 KB |
Output is correct |
88 |
Correct |
3420 ms |
31632 KB |
Output is correct |
89 |
Correct |
3401 ms |
31680 KB |
Output is correct |
90 |
Correct |
3323 ms |
31668 KB |
Output is correct |
91 |
Correct |
3357 ms |
24312 KB |
Output is correct |
92 |
Correct |
2939 ms |
24000 KB |
Output is correct |
93 |
Correct |
3296 ms |
32692 KB |
Output is correct |