#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,x,n) for(int i=x;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3xxEYjy ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
struct dsu{
int n,cmps;
vector<int> par,siz;
dsu(int m){init(m);}
void init(int m){
n=m;
cmps=m;
par.resize(n,0);
siz.resize(n,0);
for(int i=0;i<n;i++){
par[i]=i;
siz[i]=1;
}
}
void merge(int v,int u){
v=parent(v),u=parent(u);
if(v==u)return;
cmps--;
if(siz[v]<siz[u])swap(v,u);
siz[v]+=siz[u];
par[u]=v;
}
int parent(int v){
return par[v]==v?v:parent(par[v]);
}
bool same(int v, int u){
return parent(v)==parent(u);
}
int size(int v=-1){
return (v==-1?cmps:siz[parent(v)]);
}
};
signed main(){
_3xxEYjy;
int n,m;
cin>>n>>m;
using fpii=pair<pii,pii>;
vec(fpii) edges;
rep(i,m){
int u,v,w;
cin>>u>>v>>w;
u-=1,v-=1;
edges.pb({{w,i},{u,v}});
}
int q;
cin>>q;
vi qry;
rep(i,q){
int x;
cin>>x;
qry.pb(x);
}
dsu uf(0);
vi usd(m,0);
vi now;
vi ids;
vi psum;
auto mst=[&](int x,int t=0)->bool{
sort(edges.begin(),edges.end(),[&](fpii l,fpii r){
return abs(l.fi.fi-x)<abs(r.fi.fi-x);
});
uf.init(n);
for(auto e:edges){
if(uf.same(e.se.fi,e.se.se)) continue;
if(!t){
if(usd[e.fi.se]==0){
return 0;
}
}else{
usd[e.fi.se]=1;
ids.pb(e.fi.se);
now.pb(e.fi.fi);
}
uf.merge(e.se.fi,e.se.se);
}
if(t){
sort(now.begin(),now.end());
for(auto w:now){
psum.pb((sz(psum)?psum.back():0)+w);
}
}
return 1;
};
auto eval=[&](int x){
int j=prev(upper_bound(now.begin(),now.end(),x))-now.begin();
int sugim=0;
if(j>=0){
sugim+=(j+1)*x-psum[j];
}
if(j+1<sz(psum)){
sugim+=(psum.back()-(j<0?0:psum[j])-(sz(psum)-j-1)*x);
}
return sugim;
};
vi pns;
int here=0;
rep(i,q){
if(!i){
mst(qry[i],1);
pns.pb(eval(qry[i]));
}else{
here+=1;
assert(here<=2*m);
for(auto j:ids){
usd[j]=0;
}
ids.clear();
now.clear();
psum.clear();
mst(qry[i],1);
int l=i,r=q-1,c=i;
while(l<=r){
int m=(l+r)>>1;
if(mst(qry[m])){
c=m;
l=m+1;
}else{
r=m-1;
}
}
for(int j=i;j<=c;j++){
pns.pb(eval(qry[j]));
}
i=c;
}
}
rep(i,q){
cout<<pns[i]<<"\n";
}
//
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 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 |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
237 ms |
4572 KB |
Output is correct |
21 |
Correct |
223 ms |
4556 KB |
Output is correct |
22 |
Correct |
231 ms |
4552 KB |
Output is correct |
23 |
Correct |
238 ms |
4488 KB |
Output is correct |
24 |
Correct |
217 ms |
4564 KB |
Output is correct |
25 |
Correct |
240 ms |
4480 KB |
Output is correct |
26 |
Correct |
237 ms |
4500 KB |
Output is correct |
27 |
Correct |
259 ms |
4492 KB |
Output is correct |
28 |
Correct |
126 ms |
4464 KB |
Output is correct |
29 |
Correct |
122 ms |
4556 KB |
Output is correct |
30 |
Correct |
212 ms |
4496 KB |
Output is correct |
31 |
Correct |
246 ms |
4556 KB |
Output is correct |
32 |
Correct |
216 ms |
4556 KB |
Output is correct |
33 |
Correct |
222 ms |
4572 KB |
Output is correct |
34 |
Correct |
116 ms |
4556 KB |
Output is correct |
35 |
Correct |
62 ms |
4556 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 |
212 KB |
Output is correct |
4 |
Execution timed out |
5054 ms |
12196 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
598 ms |
28476 KB |
Output is correct |
21 |
Correct |
590 ms |
28652 KB |
Output is correct |
22 |
Correct |
583 ms |
28436 KB |
Output is correct |
23 |
Correct |
580 ms |
28548 KB |
Output is correct |
24 |
Correct |
585 ms |
28388 KB |
Output is correct |
25 |
Correct |
585 ms |
28448 KB |
Output is correct |
26 |
Runtime error |
1568 ms |
30888 KB |
Execution killed with signal 6 |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
237 ms |
4572 KB |
Output is correct |
21 |
Correct |
223 ms |
4556 KB |
Output is correct |
22 |
Correct |
231 ms |
4552 KB |
Output is correct |
23 |
Correct |
238 ms |
4488 KB |
Output is correct |
24 |
Correct |
217 ms |
4564 KB |
Output is correct |
25 |
Correct |
240 ms |
4480 KB |
Output is correct |
26 |
Correct |
237 ms |
4500 KB |
Output is correct |
27 |
Correct |
259 ms |
4492 KB |
Output is correct |
28 |
Correct |
126 ms |
4464 KB |
Output is correct |
29 |
Correct |
122 ms |
4556 KB |
Output is correct |
30 |
Correct |
212 ms |
4496 KB |
Output is correct |
31 |
Correct |
246 ms |
4556 KB |
Output is correct |
32 |
Correct |
216 ms |
4556 KB |
Output is correct |
33 |
Correct |
222 ms |
4572 KB |
Output is correct |
34 |
Correct |
116 ms |
4556 KB |
Output is correct |
35 |
Correct |
62 ms |
4556 KB |
Output is correct |
36 |
Execution timed out |
5098 ms |
4556 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
237 ms |
4572 KB |
Output is correct |
21 |
Correct |
223 ms |
4556 KB |
Output is correct |
22 |
Correct |
231 ms |
4552 KB |
Output is correct |
23 |
Correct |
238 ms |
4488 KB |
Output is correct |
24 |
Correct |
217 ms |
4564 KB |
Output is correct |
25 |
Correct |
240 ms |
4480 KB |
Output is correct |
26 |
Correct |
237 ms |
4500 KB |
Output is correct |
27 |
Correct |
259 ms |
4492 KB |
Output is correct |
28 |
Correct |
126 ms |
4464 KB |
Output is correct |
29 |
Correct |
122 ms |
4556 KB |
Output is correct |
30 |
Correct |
212 ms |
4496 KB |
Output is correct |
31 |
Correct |
246 ms |
4556 KB |
Output is correct |
32 |
Correct |
216 ms |
4556 KB |
Output is correct |
33 |
Correct |
222 ms |
4572 KB |
Output is correct |
34 |
Correct |
116 ms |
4556 KB |
Output is correct |
35 |
Correct |
62 ms |
4556 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 |
212 KB |
Output is correct |
39 |
Execution timed out |
5054 ms |
12196 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |