#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);
}
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";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
372 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
372 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
444 KB |
Output is correct |
20 |
Correct |
816 ms |
9812 KB |
Output is correct |
21 |
Correct |
436 ms |
10436 KB |
Output is correct |
22 |
Correct |
480 ms |
9056 KB |
Output is correct |
23 |
Correct |
543 ms |
8892 KB |
Output is correct |
24 |
Correct |
711 ms |
8596 KB |
Output is correct |
25 |
Correct |
859 ms |
9920 KB |
Output is correct |
26 |
Correct |
812 ms |
9568 KB |
Output is correct |
27 |
Correct |
806 ms |
9160 KB |
Output is correct |
28 |
Correct |
798 ms |
10172 KB |
Output is correct |
29 |
Correct |
617 ms |
10180 KB |
Output is correct |
30 |
Correct |
806 ms |
8664 KB |
Output is correct |
31 |
Correct |
804 ms |
8900 KB |
Output is correct |
32 |
Correct |
842 ms |
9616 KB |
Output is correct |
33 |
Correct |
802 ms |
8896 KB |
Output is correct |
34 |
Correct |
289 ms |
9924 KB |
Output is correct |
35 |
Correct |
810 ms |
8936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
532 ms |
18324 KB |
Output is correct |
5 |
Correct |
542 ms |
19032 KB |
Output is correct |
6 |
Correct |
534 ms |
18980 KB |
Output is correct |
7 |
Correct |
435 ms |
20444 KB |
Output is correct |
8 |
Correct |
438 ms |
19636 KB |
Output is correct |
9 |
Correct |
280 ms |
20660 KB |
Output is correct |
10 |
Correct |
522 ms |
17784 KB |
Output is correct |
11 |
Correct |
403 ms |
19380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
372 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
124 ms |
12812 KB |
Output is correct |
21 |
Correct |
148 ms |
12880 KB |
Output is correct |
22 |
Correct |
123 ms |
12692 KB |
Output is correct |
23 |
Correct |
138 ms |
12628 KB |
Output is correct |
24 |
Correct |
121 ms |
12692 KB |
Output is correct |
25 |
Correct |
123 ms |
12636 KB |
Output is correct |
26 |
Correct |
122 ms |
12628 KB |
Output is correct |
27 |
Correct |
120 ms |
12624 KB |
Output is correct |
28 |
Correct |
128 ms |
12624 KB |
Output is correct |
29 |
Correct |
159 ms |
12636 KB |
Output is correct |
30 |
Correct |
125 ms |
12624 KB |
Output is correct |
31 |
Correct |
127 ms |
12932 KB |
Output is correct |
32 |
Correct |
130 ms |
13384 KB |
Output is correct |
33 |
Correct |
124 ms |
12368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
372 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
444 KB |
Output is correct |
20 |
Correct |
816 ms |
9812 KB |
Output is correct |
21 |
Correct |
436 ms |
10436 KB |
Output is correct |
22 |
Correct |
480 ms |
9056 KB |
Output is correct |
23 |
Correct |
543 ms |
8892 KB |
Output is correct |
24 |
Correct |
711 ms |
8596 KB |
Output is correct |
25 |
Correct |
859 ms |
9920 KB |
Output is correct |
26 |
Correct |
812 ms |
9568 KB |
Output is correct |
27 |
Correct |
806 ms |
9160 KB |
Output is correct |
28 |
Correct |
798 ms |
10172 KB |
Output is correct |
29 |
Correct |
617 ms |
10180 KB |
Output is correct |
30 |
Correct |
806 ms |
8664 KB |
Output is correct |
31 |
Correct |
804 ms |
8900 KB |
Output is correct |
32 |
Correct |
842 ms |
9616 KB |
Output is correct |
33 |
Correct |
802 ms |
8896 KB |
Output is correct |
34 |
Correct |
289 ms |
9924 KB |
Output is correct |
35 |
Correct |
810 ms |
8936 KB |
Output is correct |
36 |
Correct |
815 ms |
8784 KB |
Output is correct |
37 |
Correct |
432 ms |
9412 KB |
Output is correct |
38 |
Correct |
486 ms |
9312 KB |
Output is correct |
39 |
Correct |
575 ms |
9528 KB |
Output is correct |
40 |
Correct |
717 ms |
9612 KB |
Output is correct |
41 |
Correct |
881 ms |
8828 KB |
Output is correct |
42 |
Correct |
849 ms |
9924 KB |
Output is correct |
43 |
Correct |
808 ms |
10324 KB |
Output is correct |
44 |
Correct |
789 ms |
10192 KB |
Output is correct |
45 |
Correct |
610 ms |
10172 KB |
Output is correct |
46 |
Correct |
814 ms |
9152 KB |
Output is correct |
47 |
Correct |
799 ms |
9880 KB |
Output is correct |
48 |
Correct |
814 ms |
8948 KB |
Output is correct |
49 |
Correct |
807 ms |
9204 KB |
Output is correct |
50 |
Correct |
302 ms |
10172 KB |
Output is correct |
51 |
Correct |
805 ms |
10180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
372 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
444 KB |
Output is correct |
20 |
Correct |
816 ms |
9812 KB |
Output is correct |
21 |
Correct |
436 ms |
10436 KB |
Output is correct |
22 |
Correct |
480 ms |
9056 KB |
Output is correct |
23 |
Correct |
543 ms |
8892 KB |
Output is correct |
24 |
Correct |
711 ms |
8596 KB |
Output is correct |
25 |
Correct |
859 ms |
9920 KB |
Output is correct |
26 |
Correct |
812 ms |
9568 KB |
Output is correct |
27 |
Correct |
806 ms |
9160 KB |
Output is correct |
28 |
Correct |
798 ms |
10172 KB |
Output is correct |
29 |
Correct |
617 ms |
10180 KB |
Output is correct |
30 |
Correct |
806 ms |
8664 KB |
Output is correct |
31 |
Correct |
804 ms |
8900 KB |
Output is correct |
32 |
Correct |
842 ms |
9616 KB |
Output is correct |
33 |
Correct |
802 ms |
8896 KB |
Output is correct |
34 |
Correct |
289 ms |
9924 KB |
Output is correct |
35 |
Correct |
810 ms |
8936 KB |
Output is correct |
36 |
Correct |
0 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 |
532 ms |
18324 KB |
Output is correct |
40 |
Correct |
542 ms |
19032 KB |
Output is correct |
41 |
Correct |
534 ms |
18980 KB |
Output is correct |
42 |
Correct |
435 ms |
20444 KB |
Output is correct |
43 |
Correct |
438 ms |
19636 KB |
Output is correct |
44 |
Correct |
280 ms |
20660 KB |
Output is correct |
45 |
Correct |
522 ms |
17784 KB |
Output is correct |
46 |
Correct |
403 ms |
19380 KB |
Output is correct |
47 |
Correct |
0 ms |
344 KB |
Output is correct |
48 |
Correct |
124 ms |
12812 KB |
Output is correct |
49 |
Correct |
148 ms |
12880 KB |
Output is correct |
50 |
Correct |
123 ms |
12692 KB |
Output is correct |
51 |
Correct |
138 ms |
12628 KB |
Output is correct |
52 |
Correct |
121 ms |
12692 KB |
Output is correct |
53 |
Correct |
123 ms |
12636 KB |
Output is correct |
54 |
Correct |
122 ms |
12628 KB |
Output is correct |
55 |
Correct |
120 ms |
12624 KB |
Output is correct |
56 |
Correct |
128 ms |
12624 KB |
Output is correct |
57 |
Correct |
159 ms |
12636 KB |
Output is correct |
58 |
Correct |
125 ms |
12624 KB |
Output is correct |
59 |
Correct |
127 ms |
12932 KB |
Output is correct |
60 |
Correct |
130 ms |
13384 KB |
Output is correct |
61 |
Correct |
124 ms |
12368 KB |
Output is correct |
62 |
Correct |
815 ms |
8784 KB |
Output is correct |
63 |
Correct |
432 ms |
9412 KB |
Output is correct |
64 |
Correct |
486 ms |
9312 KB |
Output is correct |
65 |
Correct |
575 ms |
9528 KB |
Output is correct |
66 |
Correct |
717 ms |
9612 KB |
Output is correct |
67 |
Correct |
881 ms |
8828 KB |
Output is correct |
68 |
Correct |
849 ms |
9924 KB |
Output is correct |
69 |
Correct |
808 ms |
10324 KB |
Output is correct |
70 |
Correct |
789 ms |
10192 KB |
Output is correct |
71 |
Correct |
610 ms |
10172 KB |
Output is correct |
72 |
Correct |
814 ms |
9152 KB |
Output is correct |
73 |
Correct |
799 ms |
9880 KB |
Output is correct |
74 |
Correct |
814 ms |
8948 KB |
Output is correct |
75 |
Correct |
807 ms |
9204 KB |
Output is correct |
76 |
Correct |
302 ms |
10172 KB |
Output is correct |
77 |
Correct |
805 ms |
10180 KB |
Output is correct |
78 |
Correct |
929 ms |
16048 KB |
Output is correct |
79 |
Correct |
550 ms |
19748 KB |
Output is correct |
80 |
Correct |
592 ms |
17592 KB |
Output is correct |
81 |
Correct |
656 ms |
18000 KB |
Output is correct |
82 |
Correct |
814 ms |
17668 KB |
Output is correct |
83 |
Correct |
982 ms |
16448 KB |
Output is correct |
84 |
Correct |
923 ms |
17332 KB |
Output is correct |
85 |
Correct |
949 ms |
18100 KB |
Output is correct |
86 |
Correct |
916 ms |
17016 KB |
Output is correct |
87 |
Correct |
740 ms |
18712 KB |
Output is correct |
88 |
Correct |
920 ms |
16472 KB |
Output is correct |
89 |
Correct |
922 ms |
16768 KB |
Output is correct |
90 |
Correct |
915 ms |
16060 KB |
Output is correct |
91 |
Correct |
924 ms |
18112 KB |
Output is correct |
92 |
Correct |
416 ms |
19636 KB |
Output is correct |
93 |
Correct |
921 ms |
17588 KB |
Output is correct |