#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N=505;
const int M=100005;
int n,m,q;
tuple<int,int,int> e[M];
int p[N],l[M],r[M];
vector<tuple<int,int,int>> a;
int fp(int u){
return p[u]=(p[u]==u?u:fp(p[u]));
}
void uni(int u,int v){
u=fp(u),v=fp(v);
p[u]=v;
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m;
for(int i=0;i<m;i++){
auto &[w,u,v]=e[i];
cin >> u >> v >> w;
}
sort(e,e+m);
for(int i=0;i<m;i++)r[i]=m;
for(int i=0;i<m;i++){
iota(p+1,p+n+1,1);
l[i]=i;
auto [w,u,v]=e[i];
while(fp(u)!=fp(v)){
l[i]--;
if(l[i]<0)break;
auto [_,x,y]=e[l[i]];
uni(x,y);
}
cerr << l[i] << " " << i << "\n";
if(l[i]>=0)r[l[i]]=i;
}
for(int i=0;i<m;i++){
int w=get<0>(e[i]);
if(l[i]>=0){
int wl=get<0>(e[l[i]]);
int wm=(wl+w+1)/2;
a.emplace_back(wm,-1,w-wm);
}else{
a.emplace_back(0,-1,w);
}
a.emplace_back(w,+2,0);
if(r[i]<m){
int wr=get<0>(e[r[i]]);
int wm=(w+wr+1)/2;
a.emplace_back(wm,-1,w-wm);
}
}
sort(a.rbegin(),a.rend());
ll sum=0,m=0,p=0;
cin >> q;
while(q--){
int x;
cin >> x;
while(!a.empty()&&get<0>(a.back())<=x){
auto [p2,m2,c]=a.back();
a.pop_back();
sum+=(p2-p)*m+c;
m+=m2;
p=p2;
}
cout << sum+(x-p)*m << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1246 ms |
10332 KB |
Output is correct |
21 |
Correct |
825 ms |
10688 KB |
Output is correct |
22 |
Correct |
847 ms |
10348 KB |
Output is correct |
23 |
Correct |
950 ms |
9728 KB |
Output is correct |
24 |
Correct |
1147 ms |
10224 KB |
Output is correct |
25 |
Correct |
1272 ms |
9924 KB |
Output is correct |
26 |
Correct |
1267 ms |
11244 KB |
Output is correct |
27 |
Correct |
1253 ms |
11916 KB |
Output is correct |
28 |
Correct |
1212 ms |
11712 KB |
Output is correct |
29 |
Correct |
1008 ms |
10688 KB |
Output is correct |
30 |
Correct |
1192 ms |
10688 KB |
Output is correct |
31 |
Correct |
1175 ms |
10468 KB |
Output is correct |
32 |
Correct |
1171 ms |
10452 KB |
Output is correct |
33 |
Correct |
1252 ms |
11200 KB |
Output is correct |
34 |
Correct |
687 ms |
9916 KB |
Output is correct |
35 |
Correct |
1221 ms |
10952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
901 ms |
29776 KB |
Output is correct |
5 |
Correct |
892 ms |
30168 KB |
Output is correct |
6 |
Correct |
928 ms |
30648 KB |
Output is correct |
7 |
Correct |
844 ms |
31668 KB |
Output is correct |
8 |
Correct |
884 ms |
32804 KB |
Output is correct |
9 |
Correct |
667 ms |
32696 KB |
Output is correct |
10 |
Correct |
891 ms |
30908 KB |
Output is correct |
11 |
Correct |
851 ms |
33336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
140 ms |
20396 KB |
Output is correct |
21 |
Correct |
134 ms |
22612 KB |
Output is correct |
22 |
Correct |
149 ms |
22744 KB |
Output is correct |
23 |
Correct |
147 ms |
22456 KB |
Output is correct |
24 |
Correct |
138 ms |
22468 KB |
Output is correct |
25 |
Correct |
136 ms |
22356 KB |
Output is correct |
26 |
Correct |
133 ms |
22356 KB |
Output is correct |
27 |
Correct |
161 ms |
22356 KB |
Output is correct |
28 |
Correct |
133 ms |
22356 KB |
Output is correct |
29 |
Correct |
143 ms |
22436 KB |
Output is correct |
30 |
Correct |
137 ms |
22612 KB |
Output is correct |
31 |
Correct |
133 ms |
22356 KB |
Output is correct |
32 |
Correct |
139 ms |
23064 KB |
Output is correct |
33 |
Correct |
134 ms |
22364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1246 ms |
10332 KB |
Output is correct |
21 |
Correct |
825 ms |
10688 KB |
Output is correct |
22 |
Correct |
847 ms |
10348 KB |
Output is correct |
23 |
Correct |
950 ms |
9728 KB |
Output is correct |
24 |
Correct |
1147 ms |
10224 KB |
Output is correct |
25 |
Correct |
1272 ms |
9924 KB |
Output is correct |
26 |
Correct |
1267 ms |
11244 KB |
Output is correct |
27 |
Correct |
1253 ms |
11916 KB |
Output is correct |
28 |
Correct |
1212 ms |
11712 KB |
Output is correct |
29 |
Correct |
1008 ms |
10688 KB |
Output is correct |
30 |
Correct |
1192 ms |
10688 KB |
Output is correct |
31 |
Correct |
1175 ms |
10468 KB |
Output is correct |
32 |
Correct |
1171 ms |
10452 KB |
Output is correct |
33 |
Correct |
1252 ms |
11200 KB |
Output is correct |
34 |
Correct |
687 ms |
9916 KB |
Output is correct |
35 |
Correct |
1221 ms |
10952 KB |
Output is correct |
36 |
Correct |
1183 ms |
10076 KB |
Output is correct |
37 |
Correct |
837 ms |
12168 KB |
Output is correct |
38 |
Correct |
871 ms |
12772 KB |
Output is correct |
39 |
Correct |
922 ms |
13016 KB |
Output is correct |
40 |
Correct |
1074 ms |
12784 KB |
Output is correct |
41 |
Correct |
1269 ms |
12664 KB |
Output is correct |
42 |
Correct |
1253 ms |
12088 KB |
Output is correct |
43 |
Correct |
1223 ms |
12760 KB |
Output is correct |
44 |
Correct |
1202 ms |
12484 KB |
Output is correct |
45 |
Correct |
1008 ms |
12888 KB |
Output is correct |
46 |
Correct |
1186 ms |
11716 KB |
Output is correct |
47 |
Correct |
1192 ms |
12776 KB |
Output is correct |
48 |
Correct |
1223 ms |
13764 KB |
Output is correct |
49 |
Correct |
1232 ms |
12232 KB |
Output is correct |
50 |
Correct |
675 ms |
13252 KB |
Output is correct |
51 |
Correct |
1185 ms |
12148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1246 ms |
10332 KB |
Output is correct |
21 |
Correct |
825 ms |
10688 KB |
Output is correct |
22 |
Correct |
847 ms |
10348 KB |
Output is correct |
23 |
Correct |
950 ms |
9728 KB |
Output is correct |
24 |
Correct |
1147 ms |
10224 KB |
Output is correct |
25 |
Correct |
1272 ms |
9924 KB |
Output is correct |
26 |
Correct |
1267 ms |
11244 KB |
Output is correct |
27 |
Correct |
1253 ms |
11916 KB |
Output is correct |
28 |
Correct |
1212 ms |
11712 KB |
Output is correct |
29 |
Correct |
1008 ms |
10688 KB |
Output is correct |
30 |
Correct |
1192 ms |
10688 KB |
Output is correct |
31 |
Correct |
1175 ms |
10468 KB |
Output is correct |
32 |
Correct |
1171 ms |
10452 KB |
Output is correct |
33 |
Correct |
1252 ms |
11200 KB |
Output is correct |
34 |
Correct |
687 ms |
9916 KB |
Output is correct |
35 |
Correct |
1221 ms |
10952 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
901 ms |
29776 KB |
Output is correct |
40 |
Correct |
892 ms |
30168 KB |
Output is correct |
41 |
Correct |
928 ms |
30648 KB |
Output is correct |
42 |
Correct |
844 ms |
31668 KB |
Output is correct |
43 |
Correct |
884 ms |
32804 KB |
Output is correct |
44 |
Correct |
667 ms |
32696 KB |
Output is correct |
45 |
Correct |
891 ms |
30908 KB |
Output is correct |
46 |
Correct |
851 ms |
33336 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
140 ms |
20396 KB |
Output is correct |
49 |
Correct |
134 ms |
22612 KB |
Output is correct |
50 |
Correct |
149 ms |
22744 KB |
Output is correct |
51 |
Correct |
147 ms |
22456 KB |
Output is correct |
52 |
Correct |
138 ms |
22468 KB |
Output is correct |
53 |
Correct |
136 ms |
22356 KB |
Output is correct |
54 |
Correct |
133 ms |
22356 KB |
Output is correct |
55 |
Correct |
161 ms |
22356 KB |
Output is correct |
56 |
Correct |
133 ms |
22356 KB |
Output is correct |
57 |
Correct |
143 ms |
22436 KB |
Output is correct |
58 |
Correct |
137 ms |
22612 KB |
Output is correct |
59 |
Correct |
133 ms |
22356 KB |
Output is correct |
60 |
Correct |
139 ms |
23064 KB |
Output is correct |
61 |
Correct |
134 ms |
22364 KB |
Output is correct |
62 |
Correct |
1183 ms |
10076 KB |
Output is correct |
63 |
Correct |
837 ms |
12168 KB |
Output is correct |
64 |
Correct |
871 ms |
12772 KB |
Output is correct |
65 |
Correct |
922 ms |
13016 KB |
Output is correct |
66 |
Correct |
1074 ms |
12784 KB |
Output is correct |
67 |
Correct |
1269 ms |
12664 KB |
Output is correct |
68 |
Correct |
1253 ms |
12088 KB |
Output is correct |
69 |
Correct |
1223 ms |
12760 KB |
Output is correct |
70 |
Correct |
1202 ms |
12484 KB |
Output is correct |
71 |
Correct |
1008 ms |
12888 KB |
Output is correct |
72 |
Correct |
1186 ms |
11716 KB |
Output is correct |
73 |
Correct |
1192 ms |
12776 KB |
Output is correct |
74 |
Correct |
1223 ms |
13764 KB |
Output is correct |
75 |
Correct |
1232 ms |
12232 KB |
Output is correct |
76 |
Correct |
675 ms |
13252 KB |
Output is correct |
77 |
Correct |
1185 ms |
12148 KB |
Output is correct |
78 |
Correct |
1298 ms |
29920 KB |
Output is correct |
79 |
Correct |
942 ms |
30568 KB |
Output is correct |
80 |
Correct |
980 ms |
30520 KB |
Output is correct |
81 |
Correct |
1118 ms |
31320 KB |
Output is correct |
82 |
Correct |
1255 ms |
28764 KB |
Output is correct |
83 |
Correct |
1429 ms |
29384 KB |
Output is correct |
84 |
Correct |
1347 ms |
29900 KB |
Output is correct |
85 |
Correct |
1298 ms |
30232 KB |
Output is correct |
86 |
Correct |
1320 ms |
30388 KB |
Output is correct |
87 |
Correct |
1182 ms |
30824 KB |
Output is correct |
88 |
Correct |
1384 ms |
28852 KB |
Output is correct |
89 |
Correct |
1323 ms |
29112 KB |
Output is correct |
90 |
Correct |
1310 ms |
29284 KB |
Output is correct |
91 |
Correct |
1306 ms |
29992 KB |
Output is correct |
92 |
Correct |
795 ms |
33384 KB |
Output is correct |
93 |
Correct |
1299 ms |
30644 KB |
Output is correct |