#include<bits/stdc++.h>
using ll = int;
using namespace std;
#define pll pair <ll,ll>
#define fi first
#define se second
#define sz(a) (ll((a).size()))
#define BIT(mask,i) (((mask) >> (i))&1)
#define MASK(i) (1LL << (i))
#define MP make_pair
const ll MAXN = 500;
const ll MAXM = 1e5;
const ll INF = 1e9+7;
struct edge{
ll u,v,w;
bool operator == (const edge &p)const {
return make_tuple(u,v,w) == make_tuple(p.u,p.v,p.w);
}
};
edge all[MAXM+100];
pll query[1'000'100];
long long pf[2][1'000'100];
long long ans[1'000'100];
ll dsu[MAXN+100];
ll f(ll x){
if (dsu[x] < 0)return x;
return (dsu[x] = f(dsu[x]));
}
void join(ll x,ll y){
x = f(x),y = f(y);
if (x!=y){
if (dsu[x] > dsu[y])swap(x,y);
dsu[x] += dsu[y];
dsu[y] = x;
}
}
ll n,m,q;
pll range[MAXM+100];
vector <ll> mst;
int main(){
ios_base::sync_with_stdio(0);cin.tie(nullptr);
// freopen("code1.inp","r",stdin);
cin>>n>>m;
for (ll i = 1;i <= m;i ++){
cin>>all[i].u>>all[i].v>>all[i].w;
}
sort(all+1,all+1+m,[](edge x, edge y){return MP(x.w,MP(x.u,x.v)) < MP(y.w,MP(y.u,y.v));});
m = unique(all+1,all+1+m)-all-1;
cin>>q;
for (ll i = 1;i <= q;i ++){
cin>>query[i].fi;
query[i].se = i;
}
sort(query+1,query+1+q);
mst.clear();
for (ll i = 1;i <= m;i ++){
// assert(sz(mst) < n);
range[i].fi = -INF;
for (ll j = 1;j <= n;j ++)dsu[j] = -1;
ll ptr = 0;
for (auto x:mst){
join(all[x].u,all[x].v);
if (f(all[i].u)==f(all[i].v)){
mst.erase(mst.begin()+ptr);
range[i].fi = (all[x].w + all[i].w) / 2 + 1;
break;
}
ptr++;
}
mst.insert(mst.begin(),i);
}
mst.clear();
for (ll i = m;i >= 1;i --){assert(sz(mst) < n);
range[i].se = INF;
for (ll j = 1;j <= n;j ++)dsu[j] = -1;
ll ptr = 0;
for (auto x:mst){
join(all[x].u,all[x].v);
if (f(all[i].u)==f(all[i].v)){
mst.erase(mst.begin()+ptr);
range[i].se = (all[x].w + all[i].w) / 2;
break;
}
ptr++;
}
mst.insert(mst.begin(),i);
}
// for (ll i = 1;i <= m;i ++){
// cout<<all[i].u<<' '<<all[i].v<<' ' <<all[i].w<<'\n';
// }
// for (ll i = 1;i <= m;i ++){
// cout<<range[i].fi<<' '<<range[i].se<<'\n';
// }
for (ll i = 1;i <= m;i ++){
// if (all[i].w >= range[i].fi){
ll l = lower_bound(query+1,query+1+q,MP(range[i].fi,-1))-query;
ll r = upper_bound(query+1,query+1+q,MP(range[i].se,INF))-query-1;
ll mid = lower_bound(query+1,query+1+q,MP(all[i].w,-1))-query;
// cout<<i<<' '<<l<<' '<<mid<<' '<<r<<'\n';
// if (l <= r){
if (l <= mid - 1){
// -1 * x + w
pf[1][l] += -1;
pf[1][mid] -= (-1);
pf[0][l] += all[i].w;
pf[0][mid] -= all[i].w;
}
if (mid <= r){
//+1 * x - w
pf[1][mid] += +1;
pf[1][r+1] -= (+1);
pf[0][mid] += -all[i].w;
pf[0][r+1] -= -all[i].w;
}
// }
// }
}
for (ll i = 1;i <= q;i ++){
pf[1][i] += pf[1][i-1];
pf[0][i] += pf[0][i-1];
ans[query[i].se] = pf[1][i] * query[i].fi + pf[0][i];
}
for (ll i = 1;i <= q;i ++){
cout<<ans[i]<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10700 KB |
Output is correct |
6 |
Correct |
1 ms |
10584 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10704 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
3 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10704 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10700 KB |
Output is correct |
6 |
Correct |
1 ms |
10584 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10704 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
3 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10704 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
891 ms |
12160 KB |
Output is correct |
21 |
Correct |
361 ms |
12164 KB |
Output is correct |
22 |
Correct |
377 ms |
12144 KB |
Output is correct |
23 |
Correct |
449 ms |
12308 KB |
Output is correct |
24 |
Correct |
643 ms |
12164 KB |
Output is correct |
25 |
Correct |
834 ms |
12368 KB |
Output is correct |
26 |
Correct |
866 ms |
12368 KB |
Output is correct |
27 |
Correct |
900 ms |
12156 KB |
Output is correct |
28 |
Correct |
865 ms |
12372 KB |
Output is correct |
29 |
Correct |
265 ms |
12624 KB |
Output is correct |
30 |
Correct |
923 ms |
12844 KB |
Output is correct |
31 |
Correct |
882 ms |
12840 KB |
Output is correct |
32 |
Correct |
884 ms |
12836 KB |
Output is correct |
33 |
Correct |
879 ms |
12848 KB |
Output is correct |
34 |
Correct |
284 ms |
12952 KB |
Output is correct |
35 |
Correct |
875 ms |
12880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
625 ms |
51056 KB |
Output is correct |
5 |
Correct |
649 ms |
50676 KB |
Output is correct |
6 |
Correct |
613 ms |
50908 KB |
Output is correct |
7 |
Correct |
280 ms |
52672 KB |
Output is correct |
8 |
Correct |
221 ms |
52824 KB |
Output is correct |
9 |
Correct |
206 ms |
52816 KB |
Output is correct |
10 |
Correct |
583 ms |
50828 KB |
Output is correct |
11 |
Correct |
206 ms |
52820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10700 KB |
Output is correct |
6 |
Correct |
1 ms |
10584 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10704 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
3 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10704 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
20 |
Correct |
166 ms |
50648 KB |
Output is correct |
21 |
Correct |
160 ms |
50748 KB |
Output is correct |
22 |
Correct |
157 ms |
50508 KB |
Output is correct |
23 |
Correct |
157 ms |
50476 KB |
Output is correct |
24 |
Correct |
158 ms |
50684 KB |
Output is correct |
25 |
Correct |
160 ms |
50516 KB |
Output is correct |
26 |
Correct |
162 ms |
50700 KB |
Output is correct |
27 |
Correct |
161 ms |
50768 KB |
Output is correct |
28 |
Correct |
164 ms |
54440 KB |
Output is correct |
29 |
Correct |
163 ms |
54356 KB |
Output is correct |
30 |
Correct |
163 ms |
54356 KB |
Output is correct |
31 |
Correct |
159 ms |
54352 KB |
Output is correct |
32 |
Correct |
161 ms |
54948 KB |
Output is correct |
33 |
Correct |
163 ms |
54096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10700 KB |
Output is correct |
6 |
Correct |
1 ms |
10584 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10704 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
3 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10704 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
891 ms |
12160 KB |
Output is correct |
21 |
Correct |
361 ms |
12164 KB |
Output is correct |
22 |
Correct |
377 ms |
12144 KB |
Output is correct |
23 |
Correct |
449 ms |
12308 KB |
Output is correct |
24 |
Correct |
643 ms |
12164 KB |
Output is correct |
25 |
Correct |
834 ms |
12368 KB |
Output is correct |
26 |
Correct |
866 ms |
12368 KB |
Output is correct |
27 |
Correct |
900 ms |
12156 KB |
Output is correct |
28 |
Correct |
865 ms |
12372 KB |
Output is correct |
29 |
Correct |
265 ms |
12624 KB |
Output is correct |
30 |
Correct |
923 ms |
12844 KB |
Output is correct |
31 |
Correct |
882 ms |
12840 KB |
Output is correct |
32 |
Correct |
884 ms |
12836 KB |
Output is correct |
33 |
Correct |
879 ms |
12848 KB |
Output is correct |
34 |
Correct |
284 ms |
12952 KB |
Output is correct |
35 |
Correct |
875 ms |
12880 KB |
Output is correct |
36 |
Correct |
892 ms |
15080 KB |
Output is correct |
37 |
Correct |
396 ms |
15336 KB |
Output is correct |
38 |
Correct |
379 ms |
15164 KB |
Output is correct |
39 |
Correct |
471 ms |
15084 KB |
Output is correct |
40 |
Correct |
647 ms |
15184 KB |
Output is correct |
41 |
Correct |
832 ms |
15084 KB |
Output is correct |
42 |
Correct |
876 ms |
15184 KB |
Output is correct |
43 |
Correct |
911 ms |
15188 KB |
Output is correct |
44 |
Correct |
866 ms |
15100 KB |
Output is correct |
45 |
Correct |
273 ms |
15188 KB |
Output is correct |
46 |
Correct |
897 ms |
15092 KB |
Output is correct |
47 |
Correct |
898 ms |
15084 KB |
Output is correct |
48 |
Correct |
917 ms |
15080 KB |
Output is correct |
49 |
Correct |
887 ms |
15184 KB |
Output is correct |
50 |
Correct |
288 ms |
15556 KB |
Output is correct |
51 |
Correct |
888 ms |
15276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10700 KB |
Output is correct |
6 |
Correct |
1 ms |
10584 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10704 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
3 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10704 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
891 ms |
12160 KB |
Output is correct |
21 |
Correct |
361 ms |
12164 KB |
Output is correct |
22 |
Correct |
377 ms |
12144 KB |
Output is correct |
23 |
Correct |
449 ms |
12308 KB |
Output is correct |
24 |
Correct |
643 ms |
12164 KB |
Output is correct |
25 |
Correct |
834 ms |
12368 KB |
Output is correct |
26 |
Correct |
866 ms |
12368 KB |
Output is correct |
27 |
Correct |
900 ms |
12156 KB |
Output is correct |
28 |
Correct |
865 ms |
12372 KB |
Output is correct |
29 |
Correct |
265 ms |
12624 KB |
Output is correct |
30 |
Correct |
923 ms |
12844 KB |
Output is correct |
31 |
Correct |
882 ms |
12840 KB |
Output is correct |
32 |
Correct |
884 ms |
12836 KB |
Output is correct |
33 |
Correct |
879 ms |
12848 KB |
Output is correct |
34 |
Correct |
284 ms |
12952 KB |
Output is correct |
35 |
Correct |
875 ms |
12880 KB |
Output is correct |
36 |
Correct |
1 ms |
10584 KB |
Output is correct |
37 |
Correct |
2 ms |
10588 KB |
Output is correct |
38 |
Correct |
2 ms |
10588 KB |
Output is correct |
39 |
Correct |
625 ms |
51056 KB |
Output is correct |
40 |
Correct |
649 ms |
50676 KB |
Output is correct |
41 |
Correct |
613 ms |
50908 KB |
Output is correct |
42 |
Correct |
280 ms |
52672 KB |
Output is correct |
43 |
Correct |
221 ms |
52824 KB |
Output is correct |
44 |
Correct |
206 ms |
52816 KB |
Output is correct |
45 |
Correct |
583 ms |
50828 KB |
Output is correct |
46 |
Correct |
206 ms |
52820 KB |
Output is correct |
47 |
Correct |
1 ms |
10588 KB |
Output is correct |
48 |
Correct |
166 ms |
50648 KB |
Output is correct |
49 |
Correct |
160 ms |
50748 KB |
Output is correct |
50 |
Correct |
157 ms |
50508 KB |
Output is correct |
51 |
Correct |
157 ms |
50476 KB |
Output is correct |
52 |
Correct |
158 ms |
50684 KB |
Output is correct |
53 |
Correct |
160 ms |
50516 KB |
Output is correct |
54 |
Correct |
162 ms |
50700 KB |
Output is correct |
55 |
Correct |
161 ms |
50768 KB |
Output is correct |
56 |
Correct |
164 ms |
54440 KB |
Output is correct |
57 |
Correct |
163 ms |
54356 KB |
Output is correct |
58 |
Correct |
163 ms |
54356 KB |
Output is correct |
59 |
Correct |
159 ms |
54352 KB |
Output is correct |
60 |
Correct |
161 ms |
54948 KB |
Output is correct |
61 |
Correct |
163 ms |
54096 KB |
Output is correct |
62 |
Correct |
892 ms |
15080 KB |
Output is correct |
63 |
Correct |
396 ms |
15336 KB |
Output is correct |
64 |
Correct |
379 ms |
15164 KB |
Output is correct |
65 |
Correct |
471 ms |
15084 KB |
Output is correct |
66 |
Correct |
647 ms |
15184 KB |
Output is correct |
67 |
Correct |
832 ms |
15084 KB |
Output is correct |
68 |
Correct |
876 ms |
15184 KB |
Output is correct |
69 |
Correct |
911 ms |
15188 KB |
Output is correct |
70 |
Correct |
866 ms |
15100 KB |
Output is correct |
71 |
Correct |
273 ms |
15188 KB |
Output is correct |
72 |
Correct |
897 ms |
15092 KB |
Output is correct |
73 |
Correct |
898 ms |
15084 KB |
Output is correct |
74 |
Correct |
917 ms |
15080 KB |
Output is correct |
75 |
Correct |
887 ms |
15184 KB |
Output is correct |
76 |
Correct |
288 ms |
15556 KB |
Output is correct |
77 |
Correct |
888 ms |
15276 KB |
Output is correct |
78 |
Correct |
1070 ms |
54636 KB |
Output is correct |
79 |
Correct |
532 ms |
56972 KB |
Output is correct |
80 |
Correct |
548 ms |
56028 KB |
Output is correct |
81 |
Correct |
630 ms |
55952 KB |
Output is correct |
82 |
Correct |
833 ms |
54920 KB |
Output is correct |
83 |
Correct |
991 ms |
55164 KB |
Output is correct |
84 |
Correct |
1034 ms |
55028 KB |
Output is correct |
85 |
Correct |
1044 ms |
55040 KB |
Output is correct |
86 |
Correct |
1008 ms |
54824 KB |
Output is correct |
87 |
Correct |
432 ms |
56744 KB |
Output is correct |
88 |
Correct |
1040 ms |
54996 KB |
Output is correct |
89 |
Correct |
1052 ms |
54800 KB |
Output is correct |
90 |
Correct |
1052 ms |
55104 KB |
Output is correct |
91 |
Correct |
1041 ms |
54672 KB |
Output is correct |
92 |
Correct |
451 ms |
57680 KB |
Output is correct |
93 |
Correct |
1034 ms |
56156 KB |
Output is correct |