// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXM = 100005;
const int MAXN = 505;
const int MAXQ = 1000005;
int n, m;
iii edges[MAXM];
int q;
ii qry[MAXQ];
vii vw[MAXN];
int lft[MAXM], rht[MAXM];
ll ans[MAXQ];
vii adj[MAXN];
ii mst[MAXN];
void dfs(int u, int p) {
for (auto [v, id] : adj[u]) {
if (v == p) continue;
auto [_, __, w] = edges[id];
mst[v] = min(mst[u], ii{w, id});
dfs(v, u);
}
}
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> n >> m;
REP (i, 0, m) {
int a, b, w; cin >> a >> b >> w;
edges[i] = {a, b, w};
}
cin >> q;
REP (i, 0, q) {
cin >> qry[i].FI;
qry[i].SE = i;
}
sort(edges, edges + m, [&] (iii l, iii r) {
return get<2>(l) < get<2>(r);
});
REP (i, 0, m) {
auto [a, b, w] = edges[i];
REP (j, 1, n + 1) {
mst[j] = ii{INF, INF};
}
dfs(a, -1);
adj[a].pb({b, i});
adj[b].pb({a, i});
rht[i] = INF;
if (mst[b] == ii{INF, INF}) {
lft[i] = 0;
continue;
}
auto [ow, id] = mst[b];
int mid = ow + w + 1 >> 1;
rht[id] = mid;
lft[i] = mid;
auto [oa, ob, _] = edges[id];
REP (j, 0, SZ(adj[oa])) {
if (adj[oa][j].SE == id) {
if (j + 1 != SZ(adj[oa])) {
swap(adj[oa][j], adj[oa][SZ(adj[oa]) - 1]);
}
adj[oa].pop_back();
break;
}
}
REP (j, 0, SZ(adj[ob])) {
if (adj[ob][j].SE == id) {
if (j + 1 != SZ(adj[ob])) {
swap(adj[ob][j], adj[ob][SZ(adj[ob]) - 1]);
}
adj[ob].pop_back();
break;
}
}
}
viii ev;
REP (i, 0, m) {
auto [a, b, w] = edges[i];
cerr << i << ": " << lft[i] << ' ' << rht[i] << '\n';
ev.pb({lft[i], w, 1});
ev.pb({w + 1, -2 * w, -2});
ev.pb({rht[i], w, 1});
}
sort(ALL(ev));
int j = 0;
ll res = 0;
int minus = 0;
REP (i, 0, q) {
auto [x, id] = qry[i];
while (j < SZ(ev) && get<0>(ev[j]) <= x) {
auto [a, b, c] = ev[j];
res += b;
minus += c;
j++;
}
ans[id] = res - (ll) x * minus;
}
REP (i, 0, q) {
cout << ans[i] << '\n';
}
return 0;
}
Compilation message
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:90:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
90 | int mid = ow + w + 1 >> 1;
| ~~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
384 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 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 |
0 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 |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
384 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 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 |
0 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 |
1037 ms |
8652 KB |
Output is correct |
21 |
Correct |
517 ms |
8752 KB |
Output is correct |
22 |
Correct |
823 ms |
8708 KB |
Output is correct |
23 |
Correct |
837 ms |
8692 KB |
Output is correct |
24 |
Correct |
1018 ms |
8640 KB |
Output is correct |
25 |
Correct |
1058 ms |
8692 KB |
Output is correct |
26 |
Correct |
1066 ms |
8628 KB |
Output is correct |
27 |
Correct |
1052 ms |
8604 KB |
Output is correct |
28 |
Correct |
1163 ms |
8572 KB |
Output is correct |
29 |
Correct |
1014 ms |
8848 KB |
Output is correct |
30 |
Correct |
1076 ms |
8564 KB |
Output is correct |
31 |
Correct |
1192 ms |
8596 KB |
Output is correct |
32 |
Correct |
1121 ms |
8672 KB |
Output is correct |
33 |
Correct |
1102 ms |
8588 KB |
Output is correct |
34 |
Correct |
1042 ms |
8600 KB |
Output is correct |
35 |
Correct |
1180 ms |
8688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1235 ms |
32660 KB |
Output is correct |
5 |
Correct |
1185 ms |
32632 KB |
Output is correct |
6 |
Correct |
1111 ms |
32712 KB |
Output is correct |
7 |
Correct |
308 ms |
34552 KB |
Output is correct |
8 |
Correct |
281 ms |
34624 KB |
Output is correct |
9 |
Correct |
299 ms |
34848 KB |
Output is correct |
10 |
Correct |
1178 ms |
32696 KB |
Output is correct |
11 |
Correct |
288 ms |
34704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
384 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 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 |
0 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 |
221 ms |
28304 KB |
Output is correct |
21 |
Correct |
241 ms |
37908 KB |
Output is correct |
22 |
Correct |
231 ms |
38116 KB |
Output is correct |
23 |
Correct |
201 ms |
38128 KB |
Output is correct |
24 |
Correct |
226 ms |
37928 KB |
Output is correct |
25 |
Correct |
199 ms |
38012 KB |
Output is correct |
26 |
Correct |
247 ms |
37908 KB |
Output is correct |
27 |
Correct |
207 ms |
38056 KB |
Output is correct |
28 |
Correct |
190 ms |
37856 KB |
Output is correct |
29 |
Correct |
216 ms |
38028 KB |
Output is correct |
30 |
Correct |
198 ms |
38036 KB |
Output is correct |
31 |
Correct |
193 ms |
37848 KB |
Output is correct |
32 |
Correct |
204 ms |
38420 KB |
Output is correct |
33 |
Correct |
197 ms |
37844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
384 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 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 |
0 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 |
1037 ms |
8652 KB |
Output is correct |
21 |
Correct |
517 ms |
8752 KB |
Output is correct |
22 |
Correct |
823 ms |
8708 KB |
Output is correct |
23 |
Correct |
837 ms |
8692 KB |
Output is correct |
24 |
Correct |
1018 ms |
8640 KB |
Output is correct |
25 |
Correct |
1058 ms |
8692 KB |
Output is correct |
26 |
Correct |
1066 ms |
8628 KB |
Output is correct |
27 |
Correct |
1052 ms |
8604 KB |
Output is correct |
28 |
Correct |
1163 ms |
8572 KB |
Output is correct |
29 |
Correct |
1014 ms |
8848 KB |
Output is correct |
30 |
Correct |
1076 ms |
8564 KB |
Output is correct |
31 |
Correct |
1192 ms |
8596 KB |
Output is correct |
32 |
Correct |
1121 ms |
8672 KB |
Output is correct |
33 |
Correct |
1102 ms |
8588 KB |
Output is correct |
34 |
Correct |
1042 ms |
8600 KB |
Output is correct |
35 |
Correct |
1180 ms |
8688 KB |
Output is correct |
36 |
Correct |
1037 ms |
8812 KB |
Output is correct |
37 |
Correct |
504 ms |
8728 KB |
Output is correct |
38 |
Correct |
847 ms |
9048 KB |
Output is correct |
39 |
Correct |
923 ms |
8980 KB |
Output is correct |
40 |
Correct |
965 ms |
9112 KB |
Output is correct |
41 |
Correct |
1031 ms |
9060 KB |
Output is correct |
42 |
Correct |
1056 ms |
8984 KB |
Output is correct |
43 |
Correct |
1072 ms |
8980 KB |
Output is correct |
44 |
Correct |
1186 ms |
9204 KB |
Output is correct |
45 |
Correct |
1075 ms |
9204 KB |
Output is correct |
46 |
Correct |
1092 ms |
9120 KB |
Output is correct |
47 |
Correct |
1086 ms |
9104 KB |
Output is correct |
48 |
Correct |
1101 ms |
9108 KB |
Output is correct |
49 |
Correct |
1061 ms |
8996 KB |
Output is correct |
50 |
Correct |
1088 ms |
9172 KB |
Output is correct |
51 |
Correct |
1052 ms |
9064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
384 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 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 |
0 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 |
1037 ms |
8652 KB |
Output is correct |
21 |
Correct |
517 ms |
8752 KB |
Output is correct |
22 |
Correct |
823 ms |
8708 KB |
Output is correct |
23 |
Correct |
837 ms |
8692 KB |
Output is correct |
24 |
Correct |
1018 ms |
8640 KB |
Output is correct |
25 |
Correct |
1058 ms |
8692 KB |
Output is correct |
26 |
Correct |
1066 ms |
8628 KB |
Output is correct |
27 |
Correct |
1052 ms |
8604 KB |
Output is correct |
28 |
Correct |
1163 ms |
8572 KB |
Output is correct |
29 |
Correct |
1014 ms |
8848 KB |
Output is correct |
30 |
Correct |
1076 ms |
8564 KB |
Output is correct |
31 |
Correct |
1192 ms |
8596 KB |
Output is correct |
32 |
Correct |
1121 ms |
8672 KB |
Output is correct |
33 |
Correct |
1102 ms |
8588 KB |
Output is correct |
34 |
Correct |
1042 ms |
8600 KB |
Output is correct |
35 |
Correct |
1180 ms |
8688 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1235 ms |
32660 KB |
Output is correct |
40 |
Correct |
1185 ms |
32632 KB |
Output is correct |
41 |
Correct |
1111 ms |
32712 KB |
Output is correct |
42 |
Correct |
308 ms |
34552 KB |
Output is correct |
43 |
Correct |
281 ms |
34624 KB |
Output is correct |
44 |
Correct |
299 ms |
34848 KB |
Output is correct |
45 |
Correct |
1178 ms |
32696 KB |
Output is correct |
46 |
Correct |
288 ms |
34704 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
221 ms |
28304 KB |
Output is correct |
49 |
Correct |
241 ms |
37908 KB |
Output is correct |
50 |
Correct |
231 ms |
38116 KB |
Output is correct |
51 |
Correct |
201 ms |
38128 KB |
Output is correct |
52 |
Correct |
226 ms |
37928 KB |
Output is correct |
53 |
Correct |
199 ms |
38012 KB |
Output is correct |
54 |
Correct |
247 ms |
37908 KB |
Output is correct |
55 |
Correct |
207 ms |
38056 KB |
Output is correct |
56 |
Correct |
190 ms |
37856 KB |
Output is correct |
57 |
Correct |
216 ms |
38028 KB |
Output is correct |
58 |
Correct |
198 ms |
38036 KB |
Output is correct |
59 |
Correct |
193 ms |
37848 KB |
Output is correct |
60 |
Correct |
204 ms |
38420 KB |
Output is correct |
61 |
Correct |
197 ms |
37844 KB |
Output is correct |
62 |
Correct |
1037 ms |
8812 KB |
Output is correct |
63 |
Correct |
504 ms |
8728 KB |
Output is correct |
64 |
Correct |
847 ms |
9048 KB |
Output is correct |
65 |
Correct |
923 ms |
8980 KB |
Output is correct |
66 |
Correct |
965 ms |
9112 KB |
Output is correct |
67 |
Correct |
1031 ms |
9060 KB |
Output is correct |
68 |
Correct |
1056 ms |
8984 KB |
Output is correct |
69 |
Correct |
1072 ms |
8980 KB |
Output is correct |
70 |
Correct |
1186 ms |
9204 KB |
Output is correct |
71 |
Correct |
1075 ms |
9204 KB |
Output is correct |
72 |
Correct |
1092 ms |
9120 KB |
Output is correct |
73 |
Correct |
1086 ms |
9104 KB |
Output is correct |
74 |
Correct |
1101 ms |
9108 KB |
Output is correct |
75 |
Correct |
1061 ms |
8996 KB |
Output is correct |
76 |
Correct |
1088 ms |
9172 KB |
Output is correct |
77 |
Correct |
1052 ms |
9064 KB |
Output is correct |
78 |
Correct |
1241 ms |
42796 KB |
Output is correct |
79 |
Correct |
685 ms |
44516 KB |
Output is correct |
80 |
Correct |
1056 ms |
43984 KB |
Output is correct |
81 |
Correct |
1070 ms |
44080 KB |
Output is correct |
82 |
Correct |
1156 ms |
43068 KB |
Output is correct |
83 |
Correct |
1233 ms |
42452 KB |
Output is correct |
84 |
Correct |
1257 ms |
40056 KB |
Output is correct |
85 |
Correct |
1238 ms |
42828 KB |
Output is correct |
86 |
Correct |
1259 ms |
31708 KB |
Output is correct |
87 |
Correct |
1129 ms |
37768 KB |
Output is correct |
88 |
Correct |
1167 ms |
42832 KB |
Output is correct |
89 |
Correct |
1206 ms |
42748 KB |
Output is correct |
90 |
Correct |
1179 ms |
42804 KB |
Output is correct |
91 |
Correct |
1163 ms |
35552 KB |
Output is correct |
92 |
Correct |
1107 ms |
34500 KB |
Output is correct |
93 |
Correct |
1154 ms |
33760 KB |
Output is correct |