#include<bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
int n, m, q;
pair< int, pair<int,int> > edg[100005];
vector<int> f[100005], b[100005], t[100005];
int x[100005], y[100005];
int dsu[100005];
int find(int x) {
if(dsu[x]==x) return x;
return dsu[x] = find(dsu[x]);
}
void merge(int x) {
dsu[find(edg[x].second.first)] = find(edg[x].second.second);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for(int i=1; i<=m; i++) {
cin >> edg[i].second.first >> edg[i].second.second >> edg[i].first;
}
sort(edg+1, edg+m+1);
for(int i=1; i<=m; i++) {
b[i].push_back(i);
for(int j=1; j<=n; j++) dsu[j] = j;
bool tmp = false;
x[i] = -1;
for(int j:b[i-1]) {
if(find(edg[j].second.first)!=find(edg[j].second.second)) {
merge(j);
if(find(edg[i].second.first)==find(edg[i].second.second) && !tmp) {
tmp = true;
x[i] = (edg[j].first + edg[i].first + 2) / 2;
}
else b[i].push_back(j);
}
}
}
for(int i=m; i>=1; i--) {
f[i].push_back(i);
for(int j=1; j<=n; j++) dsu[j] = j;
bool tmp = false;
y[i] = 2e9;
for(int j:f[i+1]) {
if(find(edg[j].second.first)!=find(edg[j].second.second)) {
merge(j);
if(find(edg[i].second.first)==find(edg[i].second.second) && !tmp) {
tmp = true;
y[i] = (edg[j].first + edg[i].first) / 2;
}
else f[i].push_back(j);
}
}
}
for(int i=0; i<=m; i++) {
for(int j:b[i]) t[i].push_back(j);
for(int j:f[i+1]) t[i].push_back(j);
}
cin >> q;
while(q--) {
int v;
cin >> v;
int pos = upper_bound(edg+1, edg+m+1, make_pair(v, make_pair((int)1e9, (int)1e9))) - edg - 1;
long long ans = 0;
for(int i:t[pos]) {
if(v>=x[i] && v<=y[i]) {
ans += abs(v-edg[i].first);
}
}
cout << ans << "\n";
}
}
Compilation message
reconstruction.cpp:5: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
5 | #pragma GCC optimization ("O3")
|
reconstruction.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
6 | #pragma GCC optimization ("unroll-loops")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
3 ms |
7380 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
5 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
3 ms |
7380 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
5 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
4539 ms |
815880 KB |
Output is correct |
21 |
Correct |
3393 ms |
814624 KB |
Output is correct |
22 |
Correct |
3619 ms |
814648 KB |
Output is correct |
23 |
Correct |
3735 ms |
814540 KB |
Output is correct |
24 |
Correct |
4179 ms |
814672 KB |
Output is correct |
25 |
Correct |
4876 ms |
814836 KB |
Output is correct |
26 |
Correct |
4867 ms |
816120 KB |
Output is correct |
27 |
Correct |
4753 ms |
816196 KB |
Output is correct |
28 |
Correct |
4710 ms |
816496 KB |
Output is correct |
29 |
Correct |
4243 ms |
813120 KB |
Output is correct |
30 |
Correct |
4677 ms |
816028 KB |
Output is correct |
31 |
Correct |
4736 ms |
816244 KB |
Output is correct |
32 |
Correct |
4895 ms |
816120 KB |
Output is correct |
33 |
Correct |
4590 ms |
814812 KB |
Output is correct |
34 |
Correct |
3128 ms |
786172 KB |
Output is correct |
35 |
Correct |
4521 ms |
816024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Execution timed out |
5049 ms |
826640 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
3 ms |
7380 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
5 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
5 ms |
7380 KB |
Output is correct |
20 |
Correct |
1429 ms |
27388 KB |
Output is correct |
21 |
Correct |
1460 ms |
27280 KB |
Output is correct |
22 |
Correct |
1500 ms |
27232 KB |
Output is correct |
23 |
Correct |
1434 ms |
27188 KB |
Output is correct |
24 |
Correct |
1446 ms |
27168 KB |
Output is correct |
25 |
Correct |
1475 ms |
27224 KB |
Output is correct |
26 |
Correct |
1442 ms |
27248 KB |
Output is correct |
27 |
Correct |
1391 ms |
27168 KB |
Output is correct |
28 |
Correct |
1470 ms |
27388 KB |
Output is correct |
29 |
Correct |
1601 ms |
27224 KB |
Output is correct |
30 |
Correct |
1452 ms |
27248 KB |
Output is correct |
31 |
Correct |
1414 ms |
27216 KB |
Output is correct |
32 |
Correct |
1058 ms |
27772 KB |
Output is correct |
33 |
Correct |
1664 ms |
27000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
3 ms |
7380 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
5 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
4539 ms |
815880 KB |
Output is correct |
21 |
Correct |
3393 ms |
814624 KB |
Output is correct |
22 |
Correct |
3619 ms |
814648 KB |
Output is correct |
23 |
Correct |
3735 ms |
814540 KB |
Output is correct |
24 |
Correct |
4179 ms |
814672 KB |
Output is correct |
25 |
Correct |
4876 ms |
814836 KB |
Output is correct |
26 |
Correct |
4867 ms |
816120 KB |
Output is correct |
27 |
Correct |
4753 ms |
816196 KB |
Output is correct |
28 |
Correct |
4710 ms |
816496 KB |
Output is correct |
29 |
Correct |
4243 ms |
813120 KB |
Output is correct |
30 |
Correct |
4677 ms |
816028 KB |
Output is correct |
31 |
Correct |
4736 ms |
816244 KB |
Output is correct |
32 |
Correct |
4895 ms |
816120 KB |
Output is correct |
33 |
Correct |
4590 ms |
814812 KB |
Output is correct |
34 |
Correct |
3128 ms |
786172 KB |
Output is correct |
35 |
Correct |
4521 ms |
816024 KB |
Output is correct |
36 |
Correct |
4444 ms |
815048 KB |
Output is correct |
37 |
Correct |
3289 ms |
814896 KB |
Output is correct |
38 |
Correct |
3538 ms |
814776 KB |
Output is correct |
39 |
Correct |
3653 ms |
814960 KB |
Output is correct |
40 |
Correct |
4286 ms |
815100 KB |
Output is correct |
41 |
Correct |
4548 ms |
815204 KB |
Output is correct |
42 |
Correct |
4376 ms |
814980 KB |
Output is correct |
43 |
Correct |
4392 ms |
815148 KB |
Output is correct |
44 |
Correct |
4359 ms |
815004 KB |
Output is correct |
45 |
Correct |
3962 ms |
812100 KB |
Output is correct |
46 |
Correct |
4362 ms |
815072 KB |
Output is correct |
47 |
Correct |
4336 ms |
814932 KB |
Output is correct |
48 |
Correct |
4374 ms |
815032 KB |
Output is correct |
49 |
Correct |
4415 ms |
815160 KB |
Output is correct |
50 |
Correct |
3043 ms |
785036 KB |
Output is correct |
51 |
Correct |
4356 ms |
814940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
3 ms |
7380 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
5 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
4539 ms |
815880 KB |
Output is correct |
21 |
Correct |
3393 ms |
814624 KB |
Output is correct |
22 |
Correct |
3619 ms |
814648 KB |
Output is correct |
23 |
Correct |
3735 ms |
814540 KB |
Output is correct |
24 |
Correct |
4179 ms |
814672 KB |
Output is correct |
25 |
Correct |
4876 ms |
814836 KB |
Output is correct |
26 |
Correct |
4867 ms |
816120 KB |
Output is correct |
27 |
Correct |
4753 ms |
816196 KB |
Output is correct |
28 |
Correct |
4710 ms |
816496 KB |
Output is correct |
29 |
Correct |
4243 ms |
813120 KB |
Output is correct |
30 |
Correct |
4677 ms |
816028 KB |
Output is correct |
31 |
Correct |
4736 ms |
816244 KB |
Output is correct |
32 |
Correct |
4895 ms |
816120 KB |
Output is correct |
33 |
Correct |
4590 ms |
814812 KB |
Output is correct |
34 |
Correct |
3128 ms |
786172 KB |
Output is correct |
35 |
Correct |
4521 ms |
816024 KB |
Output is correct |
36 |
Correct |
4 ms |
7380 KB |
Output is correct |
37 |
Correct |
4 ms |
7380 KB |
Output is correct |
38 |
Correct |
4 ms |
7252 KB |
Output is correct |
39 |
Execution timed out |
5049 ms |
826640 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |