#include <bits/stdc++.h>
bool home = 1;
using namespace std;
typedef long long ll;
struct Edge{
int a;
int b;
int w;
};
bool operator < (Edge a,Edge b) {
return a.w<b.w;
}
const int N=500+7;
const int M=100000+7;
const int Q=1000000+7;
const int INF=(int)1e9+7;
int n;
int m;
int q;
int t[N];
Edge edges[M];
void clr() {
for(int i=1;i<=n;i++) t[i]=i;
}
int root(int a) {
if (t[a]==a) return a;
return t[a]=root(t[a]);
}
void join(int a,int b) {
t[root(a)]=root(b);
}
int smallest[M];
int biggest[M];
struct E{
ll t,a,b;
};
bool operator<(E a,E b) {
return a.t<b.t;
}
signed main() {
#ifdef ONLINE_JUDGE
home = 0;
#endif
home=0;
if (home) {
freopen("I_am_iron_man", "r", stdin);
}
else {
ios::sync_with_stdio(0); cin.tie(0);
}
cin>>n>>m;
for (int i=1;i<=m;i++) {
cin>>edges[i].a>>edges[i].b>>edges[i].w;
smallest[i]=-INF;
biggest[i]=+INF;
}
sort(edges+1,edges+m+1);
for (int i=2;i<=m;i++){
assert(edges[i-1].w<=edges[i].w);
}
vector<E> e;
for (int i=1;i<=m;i++) {
clr();
int low=i-1;
while (low>=1&&root(edges[i].a)!=root(edges[i].b)) {
join(edges[low].a,edges[low].b);
low--;
}
if(root(edges[i].a)==root(edges[i].b)) low++;
clr();
int high=i+1;
while(high<=m&&root(edges[i].a)!=root(edges[i].b)) {
join(edges[high].a,edges[high].b);
high++;
}
if(root(edges[i].a)==root(edges[i].b)) high--;
if (low>=1) smallest[i]=(edges[low].w+edges[i].w+1)/2;
if (high<=m) biggest[i]=(edges[high].w+edges[i].w-1)/2;
e.push_back({smallest[i], +1, -edges[i].w});
e.push_back({edges[i].w, -2, 2*edges[i].w});
e.push_back({biggest[i]+1, +1, -edges[i].w});
}
sort(e.begin(),e.end());
ll a=0,b=0;
int p=0;
cin>>q;
for (int iq=1;iq<=q;iq++) {
int w;
cin>>w;
while(p<(int)e.size()&&e[p].t<=w) {
a+=e[p].a;
b+=e[p].b;
p++;
}
cout<<-(a*w+b)<<"\n";
}
}
Compilation message
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:61:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen("I_am_iron_man", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 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 |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 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 |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
2126 ms |
14720 KB |
Output is correct |
21 |
Correct |
1198 ms |
14812 KB |
Output is correct |
22 |
Correct |
1291 ms |
14628 KB |
Output is correct |
23 |
Correct |
1463 ms |
14804 KB |
Output is correct |
24 |
Correct |
1813 ms |
15000 KB |
Output is correct |
25 |
Correct |
2172 ms |
14816 KB |
Output is correct |
26 |
Correct |
2142 ms |
14768 KB |
Output is correct |
27 |
Correct |
2079 ms |
14708 KB |
Output is correct |
28 |
Correct |
2084 ms |
14736 KB |
Output is correct |
29 |
Correct |
2073 ms |
14704 KB |
Output is correct |
30 |
Correct |
2079 ms |
14844 KB |
Output is correct |
31 |
Correct |
2080 ms |
14664 KB |
Output is correct |
32 |
Correct |
2093 ms |
14704 KB |
Output is correct |
33 |
Correct |
2095 ms |
14972 KB |
Output is correct |
34 |
Correct |
2075 ms |
14932 KB |
Output is correct |
35 |
Correct |
2093 ms |
14756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1445 ms |
29220 KB |
Output is correct |
5 |
Correct |
1460 ms |
31724 KB |
Output is correct |
6 |
Correct |
1435 ms |
31628 KB |
Output is correct |
7 |
Correct |
842 ms |
33732 KB |
Output is correct |
8 |
Correct |
732 ms |
33632 KB |
Output is correct |
9 |
Correct |
614 ms |
33820 KB |
Output is correct |
10 |
Correct |
1434 ms |
32236 KB |
Output is correct |
11 |
Correct |
731 ms |
33860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 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 |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
187 ms |
12772 KB |
Output is correct |
21 |
Correct |
190 ms |
12792 KB |
Output is correct |
22 |
Correct |
187 ms |
12788 KB |
Output is correct |
23 |
Correct |
191 ms |
12876 KB |
Output is correct |
24 |
Correct |
186 ms |
12748 KB |
Output is correct |
25 |
Correct |
185 ms |
12664 KB |
Output is correct |
26 |
Correct |
189 ms |
12796 KB |
Output is correct |
27 |
Correct |
183 ms |
12708 KB |
Output is correct |
28 |
Correct |
183 ms |
12696 KB |
Output is correct |
29 |
Correct |
196 ms |
12736 KB |
Output is correct |
30 |
Correct |
189 ms |
12736 KB |
Output is correct |
31 |
Correct |
194 ms |
12736 KB |
Output is correct |
32 |
Correct |
190 ms |
13328 KB |
Output is correct |
33 |
Correct |
187 ms |
12624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 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 |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
2126 ms |
14720 KB |
Output is correct |
21 |
Correct |
1198 ms |
14812 KB |
Output is correct |
22 |
Correct |
1291 ms |
14628 KB |
Output is correct |
23 |
Correct |
1463 ms |
14804 KB |
Output is correct |
24 |
Correct |
1813 ms |
15000 KB |
Output is correct |
25 |
Correct |
2172 ms |
14816 KB |
Output is correct |
26 |
Correct |
2142 ms |
14768 KB |
Output is correct |
27 |
Correct |
2079 ms |
14708 KB |
Output is correct |
28 |
Correct |
2084 ms |
14736 KB |
Output is correct |
29 |
Correct |
2073 ms |
14704 KB |
Output is correct |
30 |
Correct |
2079 ms |
14844 KB |
Output is correct |
31 |
Correct |
2080 ms |
14664 KB |
Output is correct |
32 |
Correct |
2093 ms |
14704 KB |
Output is correct |
33 |
Correct |
2095 ms |
14972 KB |
Output is correct |
34 |
Correct |
2075 ms |
14932 KB |
Output is correct |
35 |
Correct |
2093 ms |
14756 KB |
Output is correct |
36 |
Correct |
2073 ms |
14916 KB |
Output is correct |
37 |
Correct |
1197 ms |
14880 KB |
Output is correct |
38 |
Correct |
1305 ms |
14648 KB |
Output is correct |
39 |
Correct |
1460 ms |
14724 KB |
Output is correct |
40 |
Correct |
1805 ms |
14840 KB |
Output is correct |
41 |
Correct |
2177 ms |
14776 KB |
Output is correct |
42 |
Correct |
2110 ms |
14708 KB |
Output is correct |
43 |
Correct |
2091 ms |
14772 KB |
Output is correct |
44 |
Correct |
2074 ms |
14956 KB |
Output is correct |
45 |
Correct |
2075 ms |
14600 KB |
Output is correct |
46 |
Correct |
2095 ms |
14696 KB |
Output is correct |
47 |
Correct |
2087 ms |
14772 KB |
Output is correct |
48 |
Correct |
2086 ms |
14712 KB |
Output is correct |
49 |
Correct |
2089 ms |
14720 KB |
Output is correct |
50 |
Correct |
2079 ms |
14712 KB |
Output is correct |
51 |
Correct |
2070 ms |
14708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 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 |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
2126 ms |
14720 KB |
Output is correct |
21 |
Correct |
1198 ms |
14812 KB |
Output is correct |
22 |
Correct |
1291 ms |
14628 KB |
Output is correct |
23 |
Correct |
1463 ms |
14804 KB |
Output is correct |
24 |
Correct |
1813 ms |
15000 KB |
Output is correct |
25 |
Correct |
2172 ms |
14816 KB |
Output is correct |
26 |
Correct |
2142 ms |
14768 KB |
Output is correct |
27 |
Correct |
2079 ms |
14708 KB |
Output is correct |
28 |
Correct |
2084 ms |
14736 KB |
Output is correct |
29 |
Correct |
2073 ms |
14704 KB |
Output is correct |
30 |
Correct |
2079 ms |
14844 KB |
Output is correct |
31 |
Correct |
2080 ms |
14664 KB |
Output is correct |
32 |
Correct |
2093 ms |
14704 KB |
Output is correct |
33 |
Correct |
2095 ms |
14972 KB |
Output is correct |
34 |
Correct |
2075 ms |
14932 KB |
Output is correct |
35 |
Correct |
2093 ms |
14756 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1445 ms |
29220 KB |
Output is correct |
40 |
Correct |
1460 ms |
31724 KB |
Output is correct |
41 |
Correct |
1435 ms |
31628 KB |
Output is correct |
42 |
Correct |
842 ms |
33732 KB |
Output is correct |
43 |
Correct |
732 ms |
33632 KB |
Output is correct |
44 |
Correct |
614 ms |
33820 KB |
Output is correct |
45 |
Correct |
1434 ms |
32236 KB |
Output is correct |
46 |
Correct |
731 ms |
33860 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
187 ms |
12772 KB |
Output is correct |
49 |
Correct |
190 ms |
12792 KB |
Output is correct |
50 |
Correct |
187 ms |
12788 KB |
Output is correct |
51 |
Correct |
191 ms |
12876 KB |
Output is correct |
52 |
Correct |
186 ms |
12748 KB |
Output is correct |
53 |
Correct |
185 ms |
12664 KB |
Output is correct |
54 |
Correct |
189 ms |
12796 KB |
Output is correct |
55 |
Correct |
183 ms |
12708 KB |
Output is correct |
56 |
Correct |
183 ms |
12696 KB |
Output is correct |
57 |
Correct |
196 ms |
12736 KB |
Output is correct |
58 |
Correct |
189 ms |
12736 KB |
Output is correct |
59 |
Correct |
194 ms |
12736 KB |
Output is correct |
60 |
Correct |
190 ms |
13328 KB |
Output is correct |
61 |
Correct |
187 ms |
12624 KB |
Output is correct |
62 |
Correct |
2073 ms |
14916 KB |
Output is correct |
63 |
Correct |
1197 ms |
14880 KB |
Output is correct |
64 |
Correct |
1305 ms |
14648 KB |
Output is correct |
65 |
Correct |
1460 ms |
14724 KB |
Output is correct |
66 |
Correct |
1805 ms |
14840 KB |
Output is correct |
67 |
Correct |
2177 ms |
14776 KB |
Output is correct |
68 |
Correct |
2110 ms |
14708 KB |
Output is correct |
69 |
Correct |
2091 ms |
14772 KB |
Output is correct |
70 |
Correct |
2074 ms |
14956 KB |
Output is correct |
71 |
Correct |
2075 ms |
14600 KB |
Output is correct |
72 |
Correct |
2095 ms |
14696 KB |
Output is correct |
73 |
Correct |
2087 ms |
14772 KB |
Output is correct |
74 |
Correct |
2086 ms |
14712 KB |
Output is correct |
75 |
Correct |
2089 ms |
14720 KB |
Output is correct |
76 |
Correct |
2079 ms |
14712 KB |
Output is correct |
77 |
Correct |
2070 ms |
14708 KB |
Output is correct |
78 |
Correct |
2262 ms |
22656 KB |
Output is correct |
79 |
Correct |
1341 ms |
30428 KB |
Output is correct |
80 |
Correct |
1467 ms |
31960 KB |
Output is correct |
81 |
Correct |
1622 ms |
31844 KB |
Output is correct |
82 |
Correct |
2011 ms |
30824 KB |
Output is correct |
83 |
Correct |
2383 ms |
30292 KB |
Output is correct |
84 |
Correct |
2246 ms |
27840 KB |
Output is correct |
85 |
Correct |
2304 ms |
30620 KB |
Output is correct |
86 |
Correct |
2264 ms |
19556 KB |
Output is correct |
87 |
Correct |
2275 ms |
25388 KB |
Output is correct |
88 |
Correct |
2283 ms |
30692 KB |
Output is correct |
89 |
Correct |
2296 ms |
30592 KB |
Output is correct |
90 |
Correct |
2289 ms |
29108 KB |
Output is correct |
91 |
Correct |
2284 ms |
19548 KB |
Output is correct |
92 |
Correct |
2246 ms |
22520 KB |
Output is correct |
93 |
Correct |
2252 ms |
20584 KB |
Output is correct |