#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define lastbit(i) __builtin_ctz(i)
const int maxn=5e2+9;
const int maxm=1e5+9;
const int maxq=1e6+9;
struct edg{
int u,v,w;
bool operator < (const edg &p){
return w<p.w;
}
}a[maxm];
int f[maxn];
int fs(int u){
if (f[u]<0)return u;
return f[u]=fs(f[u]);
}
bool unite(int u,int v){
u=fs(u),v=fs(v);
if (u==v)return 0;
if (f[u]>f[v])swap(u,v);
f[u]+=f[v];
f[v]=u;
return 1;
}
long long b[maxq];
int l1[maxm],r1[maxm];
vector<int>add[maxq][2];
vector<int>del[maxq][2];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
int n,m;
cin>>n>>m;
for1(i,1,m)cin>>a[i].u>>a[i].v>>a[i].w;
sort(a+1,a+1+m);
int q;
cin>>q;
for1(i,1,q)cin>>b[i];
vector<int>use;
for1(i,1,m){
for1(j,1,n)f[j]=-1;
unite(a[i].u,a[i].v);
vector<int>nw;
nw.pb(i);
for (auto u:use){
if (unite(a[u].u,a[u].v)){
nw.pb(u);
}
else {
l1[i]=u;
r1[u]=i;
}
}
use.swap(nw);
}
long long cnt1=0,cnt2=0;
long long w1=0,w2=0;
for1(i,1,m){
if (l1[i]==0){
cnt2++;
w2+=a[i].w;
int l=1,r=q,ans=-1;
while (l<=r){
int mid=(l+r)/2;
if (b[mid]>=a[i].w){
ans=mid;
r=mid-1;
}
else l=mid+1;
}
if (ans!=-1){
del[ans][1].pb(i);
add[ans][0].pb(i);
}
}
if (r1[i]==0){
}
else {
int l=1,r=q,ans=-1;
while (l<=r){
int mid=(l+r)/2;
if (2*b[mid]>=a[i].w+a[r1[i]].w){
ans=mid;
r=mid-1;;
}
else l=mid+1;
}
if (ans!=-1){
del[ans][0].pb(i);
add[ans][1].pb(r1[i]);
l=1,r=q,ans=-1;
while (l<=r){
int mid=(l+r)/2;
if (b[mid]>=a[r1[i]].w){
ans=mid;
r=mid-1;
}
else l=mid+1;
}
if (ans!=-1){
del[ans][1].pb(r1[i]);
add[ans][0].pb(r1[i]);
}
}
else {
l=1,r=q,ans=-1;
while (l<=r){
int mid=(l+r)/2;
if (b[mid]>=a[r1[i]].w){
ans=mid;
r=mid-1;
}
else l=mid+1;
}
if (ans!=-1){
del[ans][0].pb(i);
add[ans][0].pb(r1[i]);
}
}
}
}
for1(i,1,q){
for (auto v:add[i][0]){
cnt1++;
w1+=a[v].w;
}
for (auto v:add[i][1]){
cnt2++;
w2+=a[v].w;
}
for (auto v:del[i][0]){
cnt1--;
w1-=a[v].w;
}
for (auto v:del[i][1]){
cnt2--;
w2-=a[v].w;
}
cout<<b[i]*cnt1-w1+w2-b[i]*cnt2<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
98896 KB |
Output is correct |
2 |
Correct |
21 ms |
98904 KB |
Output is correct |
3 |
Correct |
22 ms |
99160 KB |
Output is correct |
4 |
Correct |
21 ms |
99128 KB |
Output is correct |
5 |
Correct |
21 ms |
99052 KB |
Output is correct |
6 |
Correct |
21 ms |
98904 KB |
Output is correct |
7 |
Correct |
22 ms |
98908 KB |
Output is correct |
8 |
Correct |
21 ms |
98908 KB |
Output is correct |
9 |
Correct |
21 ms |
98908 KB |
Output is correct |
10 |
Correct |
21 ms |
98908 KB |
Output is correct |
11 |
Correct |
21 ms |
99012 KB |
Output is correct |
12 |
Correct |
25 ms |
99412 KB |
Output is correct |
13 |
Correct |
22 ms |
99108 KB |
Output is correct |
14 |
Correct |
21 ms |
98908 KB |
Output is correct |
15 |
Correct |
21 ms |
98908 KB |
Output is correct |
16 |
Correct |
21 ms |
98908 KB |
Output is correct |
17 |
Correct |
21 ms |
98908 KB |
Output is correct |
18 |
Correct |
21 ms |
99028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
98896 KB |
Output is correct |
2 |
Correct |
21 ms |
98904 KB |
Output is correct |
3 |
Correct |
22 ms |
99160 KB |
Output is correct |
4 |
Correct |
21 ms |
99128 KB |
Output is correct |
5 |
Correct |
21 ms |
99052 KB |
Output is correct |
6 |
Correct |
21 ms |
98904 KB |
Output is correct |
7 |
Correct |
22 ms |
98908 KB |
Output is correct |
8 |
Correct |
21 ms |
98908 KB |
Output is correct |
9 |
Correct |
21 ms |
98908 KB |
Output is correct |
10 |
Correct |
21 ms |
98908 KB |
Output is correct |
11 |
Correct |
21 ms |
99012 KB |
Output is correct |
12 |
Correct |
25 ms |
99412 KB |
Output is correct |
13 |
Correct |
22 ms |
99108 KB |
Output is correct |
14 |
Correct |
21 ms |
98908 KB |
Output is correct |
15 |
Correct |
21 ms |
98908 KB |
Output is correct |
16 |
Correct |
21 ms |
98908 KB |
Output is correct |
17 |
Correct |
21 ms |
98908 KB |
Output is correct |
18 |
Correct |
21 ms |
99028 KB |
Output is correct |
19 |
Correct |
21 ms |
98904 KB |
Output is correct |
20 |
Correct |
491 ms |
103564 KB |
Output is correct |
21 |
Correct |
448 ms |
103464 KB |
Output is correct |
22 |
Correct |
410 ms |
103476 KB |
Output is correct |
23 |
Correct |
413 ms |
103500 KB |
Output is correct |
24 |
Correct |
449 ms |
103552 KB |
Output is correct |
25 |
Correct |
491 ms |
103584 KB |
Output is correct |
26 |
Correct |
473 ms |
103760 KB |
Output is correct |
27 |
Correct |
500 ms |
103596 KB |
Output is correct |
28 |
Correct |
477 ms |
103884 KB |
Output is correct |
29 |
Correct |
471 ms |
103504 KB |
Output is correct |
30 |
Correct |
474 ms |
103472 KB |
Output is correct |
31 |
Correct |
474 ms |
103508 KB |
Output is correct |
32 |
Correct |
483 ms |
103488 KB |
Output is correct |
33 |
Correct |
477 ms |
102740 KB |
Output is correct |
34 |
Correct |
477 ms |
101880 KB |
Output is correct |
35 |
Correct |
501 ms |
102664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
98908 KB |
Output is correct |
2 |
Correct |
22 ms |
99128 KB |
Output is correct |
3 |
Correct |
22 ms |
98908 KB |
Output is correct |
4 |
Correct |
653 ms |
139076 KB |
Output is correct |
5 |
Correct |
664 ms |
138864 KB |
Output is correct |
6 |
Correct |
642 ms |
139088 KB |
Output is correct |
7 |
Correct |
490 ms |
140824 KB |
Output is correct |
8 |
Correct |
412 ms |
141060 KB |
Output is correct |
9 |
Correct |
386 ms |
141132 KB |
Output is correct |
10 |
Correct |
596 ms |
127544 KB |
Output is correct |
11 |
Correct |
347 ms |
129484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
98896 KB |
Output is correct |
2 |
Correct |
21 ms |
98904 KB |
Output is correct |
3 |
Correct |
22 ms |
99160 KB |
Output is correct |
4 |
Correct |
21 ms |
99128 KB |
Output is correct |
5 |
Correct |
21 ms |
99052 KB |
Output is correct |
6 |
Correct |
21 ms |
98904 KB |
Output is correct |
7 |
Correct |
22 ms |
98908 KB |
Output is correct |
8 |
Correct |
21 ms |
98908 KB |
Output is correct |
9 |
Correct |
21 ms |
98908 KB |
Output is correct |
10 |
Correct |
21 ms |
98908 KB |
Output is correct |
11 |
Correct |
21 ms |
99012 KB |
Output is correct |
12 |
Correct |
25 ms |
99412 KB |
Output is correct |
13 |
Correct |
22 ms |
99108 KB |
Output is correct |
14 |
Correct |
21 ms |
98908 KB |
Output is correct |
15 |
Correct |
21 ms |
98908 KB |
Output is correct |
16 |
Correct |
21 ms |
98908 KB |
Output is correct |
17 |
Correct |
21 ms |
98908 KB |
Output is correct |
18 |
Correct |
21 ms |
99028 KB |
Output is correct |
19 |
Correct |
23 ms |
98904 KB |
Output is correct |
20 |
Correct |
170 ms |
125012 KB |
Output is correct |
21 |
Correct |
190 ms |
125272 KB |
Output is correct |
22 |
Correct |
169 ms |
125268 KB |
Output is correct |
23 |
Correct |
167 ms |
125076 KB |
Output is correct |
24 |
Correct |
181 ms |
125264 KB |
Output is correct |
25 |
Correct |
190 ms |
125012 KB |
Output is correct |
26 |
Correct |
177 ms |
125008 KB |
Output is correct |
27 |
Correct |
195 ms |
125108 KB |
Output is correct |
28 |
Correct |
186 ms |
125016 KB |
Output is correct |
29 |
Correct |
170 ms |
125012 KB |
Output is correct |
30 |
Correct |
170 ms |
125264 KB |
Output is correct |
31 |
Correct |
164 ms |
125012 KB |
Output is correct |
32 |
Correct |
169 ms |
125720 KB |
Output is correct |
33 |
Correct |
163 ms |
125012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
98896 KB |
Output is correct |
2 |
Correct |
21 ms |
98904 KB |
Output is correct |
3 |
Correct |
22 ms |
99160 KB |
Output is correct |
4 |
Correct |
21 ms |
99128 KB |
Output is correct |
5 |
Correct |
21 ms |
99052 KB |
Output is correct |
6 |
Correct |
21 ms |
98904 KB |
Output is correct |
7 |
Correct |
22 ms |
98908 KB |
Output is correct |
8 |
Correct |
21 ms |
98908 KB |
Output is correct |
9 |
Correct |
21 ms |
98908 KB |
Output is correct |
10 |
Correct |
21 ms |
98908 KB |
Output is correct |
11 |
Correct |
21 ms |
99012 KB |
Output is correct |
12 |
Correct |
25 ms |
99412 KB |
Output is correct |
13 |
Correct |
22 ms |
99108 KB |
Output is correct |
14 |
Correct |
21 ms |
98908 KB |
Output is correct |
15 |
Correct |
21 ms |
98908 KB |
Output is correct |
16 |
Correct |
21 ms |
98908 KB |
Output is correct |
17 |
Correct |
21 ms |
98908 KB |
Output is correct |
18 |
Correct |
21 ms |
99028 KB |
Output is correct |
19 |
Correct |
21 ms |
98904 KB |
Output is correct |
20 |
Correct |
491 ms |
103564 KB |
Output is correct |
21 |
Correct |
448 ms |
103464 KB |
Output is correct |
22 |
Correct |
410 ms |
103476 KB |
Output is correct |
23 |
Correct |
413 ms |
103500 KB |
Output is correct |
24 |
Correct |
449 ms |
103552 KB |
Output is correct |
25 |
Correct |
491 ms |
103584 KB |
Output is correct |
26 |
Correct |
473 ms |
103760 KB |
Output is correct |
27 |
Correct |
500 ms |
103596 KB |
Output is correct |
28 |
Correct |
477 ms |
103884 KB |
Output is correct |
29 |
Correct |
471 ms |
103504 KB |
Output is correct |
30 |
Correct |
474 ms |
103472 KB |
Output is correct |
31 |
Correct |
474 ms |
103508 KB |
Output is correct |
32 |
Correct |
483 ms |
103488 KB |
Output is correct |
33 |
Correct |
477 ms |
102740 KB |
Output is correct |
34 |
Correct |
477 ms |
101880 KB |
Output is correct |
35 |
Correct |
501 ms |
102664 KB |
Output is correct |
36 |
Correct |
493 ms |
105296 KB |
Output is correct |
37 |
Correct |
445 ms |
105544 KB |
Output is correct |
38 |
Correct |
429 ms |
105552 KB |
Output is correct |
39 |
Correct |
441 ms |
105544 KB |
Output is correct |
40 |
Correct |
475 ms |
105552 KB |
Output is correct |
41 |
Correct |
495 ms |
105300 KB |
Output is correct |
42 |
Correct |
502 ms |
105544 KB |
Output is correct |
43 |
Correct |
498 ms |
105288 KB |
Output is correct |
44 |
Correct |
480 ms |
104264 KB |
Output is correct |
45 |
Correct |
478 ms |
104020 KB |
Output is correct |
46 |
Correct |
493 ms |
105348 KB |
Output is correct |
47 |
Correct |
495 ms |
105288 KB |
Output is correct |
48 |
Correct |
496 ms |
104908 KB |
Output is correct |
49 |
Correct |
490 ms |
104176 KB |
Output is correct |
50 |
Correct |
479 ms |
102308 KB |
Output is correct |
51 |
Correct |
489 ms |
102864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
98896 KB |
Output is correct |
2 |
Correct |
21 ms |
98904 KB |
Output is correct |
3 |
Correct |
22 ms |
99160 KB |
Output is correct |
4 |
Correct |
21 ms |
99128 KB |
Output is correct |
5 |
Correct |
21 ms |
99052 KB |
Output is correct |
6 |
Correct |
21 ms |
98904 KB |
Output is correct |
7 |
Correct |
22 ms |
98908 KB |
Output is correct |
8 |
Correct |
21 ms |
98908 KB |
Output is correct |
9 |
Correct |
21 ms |
98908 KB |
Output is correct |
10 |
Correct |
21 ms |
98908 KB |
Output is correct |
11 |
Correct |
21 ms |
99012 KB |
Output is correct |
12 |
Correct |
25 ms |
99412 KB |
Output is correct |
13 |
Correct |
22 ms |
99108 KB |
Output is correct |
14 |
Correct |
21 ms |
98908 KB |
Output is correct |
15 |
Correct |
21 ms |
98908 KB |
Output is correct |
16 |
Correct |
21 ms |
98908 KB |
Output is correct |
17 |
Correct |
21 ms |
98908 KB |
Output is correct |
18 |
Correct |
21 ms |
99028 KB |
Output is correct |
19 |
Correct |
21 ms |
98904 KB |
Output is correct |
20 |
Correct |
491 ms |
103564 KB |
Output is correct |
21 |
Correct |
448 ms |
103464 KB |
Output is correct |
22 |
Correct |
410 ms |
103476 KB |
Output is correct |
23 |
Correct |
413 ms |
103500 KB |
Output is correct |
24 |
Correct |
449 ms |
103552 KB |
Output is correct |
25 |
Correct |
491 ms |
103584 KB |
Output is correct |
26 |
Correct |
473 ms |
103760 KB |
Output is correct |
27 |
Correct |
500 ms |
103596 KB |
Output is correct |
28 |
Correct |
477 ms |
103884 KB |
Output is correct |
29 |
Correct |
471 ms |
103504 KB |
Output is correct |
30 |
Correct |
474 ms |
103472 KB |
Output is correct |
31 |
Correct |
474 ms |
103508 KB |
Output is correct |
32 |
Correct |
483 ms |
103488 KB |
Output is correct |
33 |
Correct |
477 ms |
102740 KB |
Output is correct |
34 |
Correct |
477 ms |
101880 KB |
Output is correct |
35 |
Correct |
501 ms |
102664 KB |
Output is correct |
36 |
Correct |
23 ms |
98908 KB |
Output is correct |
37 |
Correct |
22 ms |
99128 KB |
Output is correct |
38 |
Correct |
22 ms |
98908 KB |
Output is correct |
39 |
Correct |
653 ms |
139076 KB |
Output is correct |
40 |
Correct |
664 ms |
138864 KB |
Output is correct |
41 |
Correct |
642 ms |
139088 KB |
Output is correct |
42 |
Correct |
490 ms |
140824 KB |
Output is correct |
43 |
Correct |
412 ms |
141060 KB |
Output is correct |
44 |
Correct |
386 ms |
141132 KB |
Output is correct |
45 |
Correct |
596 ms |
127544 KB |
Output is correct |
46 |
Correct |
347 ms |
129484 KB |
Output is correct |
47 |
Correct |
23 ms |
98904 KB |
Output is correct |
48 |
Correct |
170 ms |
125012 KB |
Output is correct |
49 |
Correct |
190 ms |
125272 KB |
Output is correct |
50 |
Correct |
169 ms |
125268 KB |
Output is correct |
51 |
Correct |
167 ms |
125076 KB |
Output is correct |
52 |
Correct |
181 ms |
125264 KB |
Output is correct |
53 |
Correct |
190 ms |
125012 KB |
Output is correct |
54 |
Correct |
177 ms |
125008 KB |
Output is correct |
55 |
Correct |
195 ms |
125108 KB |
Output is correct |
56 |
Correct |
186 ms |
125016 KB |
Output is correct |
57 |
Correct |
170 ms |
125012 KB |
Output is correct |
58 |
Correct |
170 ms |
125264 KB |
Output is correct |
59 |
Correct |
164 ms |
125012 KB |
Output is correct |
60 |
Correct |
169 ms |
125720 KB |
Output is correct |
61 |
Correct |
163 ms |
125012 KB |
Output is correct |
62 |
Correct |
493 ms |
105296 KB |
Output is correct |
63 |
Correct |
445 ms |
105544 KB |
Output is correct |
64 |
Correct |
429 ms |
105552 KB |
Output is correct |
65 |
Correct |
441 ms |
105544 KB |
Output is correct |
66 |
Correct |
475 ms |
105552 KB |
Output is correct |
67 |
Correct |
495 ms |
105300 KB |
Output is correct |
68 |
Correct |
502 ms |
105544 KB |
Output is correct |
69 |
Correct |
498 ms |
105288 KB |
Output is correct |
70 |
Correct |
480 ms |
104264 KB |
Output is correct |
71 |
Correct |
478 ms |
104020 KB |
Output is correct |
72 |
Correct |
493 ms |
105348 KB |
Output is correct |
73 |
Correct |
495 ms |
105288 KB |
Output is correct |
74 |
Correct |
496 ms |
104908 KB |
Output is correct |
75 |
Correct |
490 ms |
104176 KB |
Output is correct |
76 |
Correct |
479 ms |
102308 KB |
Output is correct |
77 |
Correct |
489 ms |
102864 KB |
Output is correct |
78 |
Correct |
710 ms |
137840 KB |
Output is correct |
79 |
Correct |
604 ms |
139844 KB |
Output is correct |
80 |
Correct |
679 ms |
138824 KB |
Output is correct |
81 |
Correct |
680 ms |
138820 KB |
Output is correct |
82 |
Correct |
680 ms |
138120 KB |
Output is correct |
83 |
Correct |
714 ms |
137936 KB |
Output is correct |
84 |
Correct |
722 ms |
138056 KB |
Output is correct |
85 |
Correct |
745 ms |
137480 KB |
Output is correct |
86 |
Correct |
663 ms |
127564 KB |
Output is correct |
87 |
Correct |
649 ms |
129108 KB |
Output is correct |
88 |
Correct |
700 ms |
136900 KB |
Output is correct |
89 |
Correct |
689 ms |
136780 KB |
Output is correct |
90 |
Correct |
675 ms |
132616 KB |
Output is correct |
91 |
Correct |
693 ms |
131340 KB |
Output is correct |
92 |
Correct |
651 ms |
128436 KB |
Output is correct |
93 |
Correct |
626 ms |
127528 KB |
Output is correct |