#include<bits/stdc++.h>
#ifdef LOCAL
#include "Essentials/algo/debug.h"
#else
#define debug(...) 69
#define debugArr(...) 69
#endif
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N = 500 + 23;
const int M = 1e5 + 23;
const ll inf = 1e18;
#define F first
#define S second
#define pb push_back
#define kill(x) cout<<x<<endl, exit(0);
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define lc (v << 1)
#define rc ((v<<1) |1)
struct Edge {
int w,a,b;
};
struct DSU {
int par[N];
int rnk[N];
void clear() {
iota(par,par+N,0);
fill(rnk,rnk+N,0);
}
int getPar(int v) {
return (par[v] == v ? v : par[v] = getPar(par[v]));
}
void merge(int v,int u) {
v = getPar(v),u = getPar(u);
if(v == u) return;
if(rnk[v] > rnk[u]) swap(v,u);
par[v] = u;
if(rnk[v] == rnk[u]) rnk[u] ++;
}
} dsu;
int n,m;
Edge edges[M];
int l[M],r[M];
vector<Edge> vals;
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin>>n>>m;
for(int i =1; i <= m ;i ++) {
cin>>edges[i].a>>edges[i].b>>edges[i].w;
}
sort(edges +1,edges+m+1,[&](const Edge &x,const Edge &y) { return x.w < y.w;});
fill(r,r+M,m+1);
for(int i = 1; i <= m ;i ++) {
l[i] = i-1;
dsu.clear();
while(l[i]) {
dsu.merge(edges[l[i]].a,edges[l[i]].b);
if(dsu.getPar(edges[i].a) == dsu.getPar(edges[i].b)) break;
l[i] --;
}
r[l[i]] = i;
}
for(int i = 1; i <= m ;i ++) {
if(l[i]) vals.pb({(edges[i].w + edges[l[i]].w)/2 + 1,-1,edges[i].w});
else vals.pb({0,-1,edges[i].w});
vals.pb({edges[i].w,2,-2*edges[i].w});
if(r[i] != m+1) vals.pb({(edges[i].w + edges[r[i]].w)/2 +1,-1,edges[i].w});
}
sort(all(vals),[&](const Edge &x,const Edge &y) {return x.w < y.w;});
int q; cin>>q;
ll c1=0,c2 = 0;
int I = 0;
while(q--) {
int x; cin>>x;
while(I < sz(vals) && vals[I].w <= x) {
c1 += vals[I].a;
c2 += vals[I].b;
I++;
}
cout << c1*x + c2 << '\n';
}
return 0;
}
// Jumpsuit, Jumpsuit cover me!
// Jumpsuit, Jumpsuit cover me!
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
0 ms |
860 KB |
Output is correct |
3 |
Correct |
0 ms |
856 KB |
Output is correct |
4 |
Correct |
0 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
856 KB |
Output is correct |
6 |
Correct |
0 ms |
860 KB |
Output is correct |
7 |
Correct |
0 ms |
860 KB |
Output is correct |
8 |
Correct |
0 ms |
860 KB |
Output is correct |
9 |
Correct |
0 ms |
860 KB |
Output is correct |
10 |
Correct |
0 ms |
860 KB |
Output is correct |
11 |
Correct |
0 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
856 KB |
Output is correct |
14 |
Correct |
0 ms |
860 KB |
Output is correct |
15 |
Correct |
0 ms |
860 KB |
Output is correct |
16 |
Correct |
0 ms |
860 KB |
Output is correct |
17 |
Correct |
0 ms |
860 KB |
Output is correct |
18 |
Correct |
0 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
0 ms |
860 KB |
Output is correct |
3 |
Correct |
0 ms |
856 KB |
Output is correct |
4 |
Correct |
0 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
856 KB |
Output is correct |
6 |
Correct |
0 ms |
860 KB |
Output is correct |
7 |
Correct |
0 ms |
860 KB |
Output is correct |
8 |
Correct |
0 ms |
860 KB |
Output is correct |
9 |
Correct |
0 ms |
860 KB |
Output is correct |
10 |
Correct |
0 ms |
860 KB |
Output is correct |
11 |
Correct |
0 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
856 KB |
Output is correct |
14 |
Correct |
0 ms |
860 KB |
Output is correct |
15 |
Correct |
0 ms |
860 KB |
Output is correct |
16 |
Correct |
0 ms |
860 KB |
Output is correct |
17 |
Correct |
0 ms |
860 KB |
Output is correct |
18 |
Correct |
0 ms |
860 KB |
Output is correct |
19 |
Correct |
0 ms |
860 KB |
Output is correct |
20 |
Correct |
434 ms |
10504 KB |
Output is correct |
21 |
Correct |
331 ms |
10948 KB |
Output is correct |
22 |
Correct |
361 ms |
11016 KB |
Output is correct |
23 |
Correct |
396 ms |
11796 KB |
Output is correct |
24 |
Correct |
448 ms |
10432 KB |
Output is correct |
25 |
Correct |
439 ms |
11024 KB |
Output is correct |
26 |
Correct |
475 ms |
10740 KB |
Output is correct |
27 |
Correct |
458 ms |
11396 KB |
Output is correct |
28 |
Correct |
438 ms |
11456 KB |
Output is correct |
29 |
Correct |
444 ms |
11972 KB |
Output is correct |
30 |
Correct |
451 ms |
12228 KB |
Output is correct |
31 |
Correct |
459 ms |
12224 KB |
Output is correct |
32 |
Correct |
443 ms |
10176 KB |
Output is correct |
33 |
Correct |
438 ms |
12312 KB |
Output is correct |
34 |
Correct |
451 ms |
11200 KB |
Output is correct |
35 |
Correct |
441 ms |
10692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
0 ms |
860 KB |
Output is correct |
3 |
Correct |
0 ms |
860 KB |
Output is correct |
4 |
Correct |
519 ms |
29128 KB |
Output is correct |
5 |
Correct |
514 ms |
29132 KB |
Output is correct |
6 |
Correct |
548 ms |
29612 KB |
Output is correct |
7 |
Correct |
377 ms |
31548 KB |
Output is correct |
8 |
Correct |
272 ms |
30900 KB |
Output is correct |
9 |
Correct |
249 ms |
32176 KB |
Output is correct |
10 |
Correct |
505 ms |
29752 KB |
Output is correct |
11 |
Correct |
284 ms |
32180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
0 ms |
860 KB |
Output is correct |
3 |
Correct |
0 ms |
856 KB |
Output is correct |
4 |
Correct |
0 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
856 KB |
Output is correct |
6 |
Correct |
0 ms |
860 KB |
Output is correct |
7 |
Correct |
0 ms |
860 KB |
Output is correct |
8 |
Correct |
0 ms |
860 KB |
Output is correct |
9 |
Correct |
0 ms |
860 KB |
Output is correct |
10 |
Correct |
0 ms |
860 KB |
Output is correct |
11 |
Correct |
0 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
856 KB |
Output is correct |
14 |
Correct |
0 ms |
860 KB |
Output is correct |
15 |
Correct |
0 ms |
860 KB |
Output is correct |
16 |
Correct |
0 ms |
860 KB |
Output is correct |
17 |
Correct |
0 ms |
860 KB |
Output is correct |
18 |
Correct |
0 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
856 KB |
Output is correct |
20 |
Correct |
128 ms |
22736 KB |
Output is correct |
21 |
Correct |
125 ms |
22876 KB |
Output is correct |
22 |
Correct |
129 ms |
22868 KB |
Output is correct |
23 |
Correct |
127 ms |
22868 KB |
Output is correct |
24 |
Correct |
128 ms |
22868 KB |
Output is correct |
25 |
Correct |
128 ms |
22720 KB |
Output is correct |
26 |
Correct |
126 ms |
22860 KB |
Output is correct |
27 |
Correct |
128 ms |
22864 KB |
Output is correct |
28 |
Correct |
117 ms |
22868 KB |
Output is correct |
29 |
Correct |
123 ms |
22800 KB |
Output is correct |
30 |
Correct |
149 ms |
22928 KB |
Output is correct |
31 |
Correct |
125 ms |
22608 KB |
Output is correct |
32 |
Correct |
121 ms |
23376 KB |
Output is correct |
33 |
Correct |
128 ms |
22608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
0 ms |
860 KB |
Output is correct |
3 |
Correct |
0 ms |
856 KB |
Output is correct |
4 |
Correct |
0 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
856 KB |
Output is correct |
6 |
Correct |
0 ms |
860 KB |
Output is correct |
7 |
Correct |
0 ms |
860 KB |
Output is correct |
8 |
Correct |
0 ms |
860 KB |
Output is correct |
9 |
Correct |
0 ms |
860 KB |
Output is correct |
10 |
Correct |
0 ms |
860 KB |
Output is correct |
11 |
Correct |
0 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
856 KB |
Output is correct |
14 |
Correct |
0 ms |
860 KB |
Output is correct |
15 |
Correct |
0 ms |
860 KB |
Output is correct |
16 |
Correct |
0 ms |
860 KB |
Output is correct |
17 |
Correct |
0 ms |
860 KB |
Output is correct |
18 |
Correct |
0 ms |
860 KB |
Output is correct |
19 |
Correct |
0 ms |
860 KB |
Output is correct |
20 |
Correct |
434 ms |
10504 KB |
Output is correct |
21 |
Correct |
331 ms |
10948 KB |
Output is correct |
22 |
Correct |
361 ms |
11016 KB |
Output is correct |
23 |
Correct |
396 ms |
11796 KB |
Output is correct |
24 |
Correct |
448 ms |
10432 KB |
Output is correct |
25 |
Correct |
439 ms |
11024 KB |
Output is correct |
26 |
Correct |
475 ms |
10740 KB |
Output is correct |
27 |
Correct |
458 ms |
11396 KB |
Output is correct |
28 |
Correct |
438 ms |
11456 KB |
Output is correct |
29 |
Correct |
444 ms |
11972 KB |
Output is correct |
30 |
Correct |
451 ms |
12228 KB |
Output is correct |
31 |
Correct |
459 ms |
12224 KB |
Output is correct |
32 |
Correct |
443 ms |
10176 KB |
Output is correct |
33 |
Correct |
438 ms |
12312 KB |
Output is correct |
34 |
Correct |
451 ms |
11200 KB |
Output is correct |
35 |
Correct |
441 ms |
10692 KB |
Output is correct |
36 |
Correct |
476 ms |
11428 KB |
Output is correct |
37 |
Correct |
345 ms |
10692 KB |
Output is correct |
38 |
Correct |
370 ms |
11484 KB |
Output is correct |
39 |
Correct |
423 ms |
10728 KB |
Output is correct |
40 |
Correct |
461 ms |
11972 KB |
Output is correct |
41 |
Correct |
451 ms |
11972 KB |
Output is correct |
42 |
Correct |
447 ms |
12224 KB |
Output is correct |
43 |
Correct |
452 ms |
10944 KB |
Output is correct |
44 |
Correct |
438 ms |
11972 KB |
Output is correct |
45 |
Correct |
456 ms |
10804 KB |
Output is correct |
46 |
Correct |
454 ms |
11756 KB |
Output is correct |
47 |
Correct |
456 ms |
11456 KB |
Output is correct |
48 |
Correct |
474 ms |
10948 KB |
Output is correct |
49 |
Correct |
459 ms |
12184 KB |
Output is correct |
50 |
Correct |
446 ms |
11740 KB |
Output is correct |
51 |
Correct |
437 ms |
10464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
0 ms |
860 KB |
Output is correct |
3 |
Correct |
0 ms |
856 KB |
Output is correct |
4 |
Correct |
0 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
856 KB |
Output is correct |
6 |
Correct |
0 ms |
860 KB |
Output is correct |
7 |
Correct |
0 ms |
860 KB |
Output is correct |
8 |
Correct |
0 ms |
860 KB |
Output is correct |
9 |
Correct |
0 ms |
860 KB |
Output is correct |
10 |
Correct |
0 ms |
860 KB |
Output is correct |
11 |
Correct |
0 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
856 KB |
Output is correct |
14 |
Correct |
0 ms |
860 KB |
Output is correct |
15 |
Correct |
0 ms |
860 KB |
Output is correct |
16 |
Correct |
0 ms |
860 KB |
Output is correct |
17 |
Correct |
0 ms |
860 KB |
Output is correct |
18 |
Correct |
0 ms |
860 KB |
Output is correct |
19 |
Correct |
0 ms |
860 KB |
Output is correct |
20 |
Correct |
434 ms |
10504 KB |
Output is correct |
21 |
Correct |
331 ms |
10948 KB |
Output is correct |
22 |
Correct |
361 ms |
11016 KB |
Output is correct |
23 |
Correct |
396 ms |
11796 KB |
Output is correct |
24 |
Correct |
448 ms |
10432 KB |
Output is correct |
25 |
Correct |
439 ms |
11024 KB |
Output is correct |
26 |
Correct |
475 ms |
10740 KB |
Output is correct |
27 |
Correct |
458 ms |
11396 KB |
Output is correct |
28 |
Correct |
438 ms |
11456 KB |
Output is correct |
29 |
Correct |
444 ms |
11972 KB |
Output is correct |
30 |
Correct |
451 ms |
12228 KB |
Output is correct |
31 |
Correct |
459 ms |
12224 KB |
Output is correct |
32 |
Correct |
443 ms |
10176 KB |
Output is correct |
33 |
Correct |
438 ms |
12312 KB |
Output is correct |
34 |
Correct |
451 ms |
11200 KB |
Output is correct |
35 |
Correct |
441 ms |
10692 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
0 ms |
860 KB |
Output is correct |
38 |
Correct |
0 ms |
860 KB |
Output is correct |
39 |
Correct |
519 ms |
29128 KB |
Output is correct |
40 |
Correct |
514 ms |
29132 KB |
Output is correct |
41 |
Correct |
548 ms |
29612 KB |
Output is correct |
42 |
Correct |
377 ms |
31548 KB |
Output is correct |
43 |
Correct |
272 ms |
30900 KB |
Output is correct |
44 |
Correct |
249 ms |
32176 KB |
Output is correct |
45 |
Correct |
505 ms |
29752 KB |
Output is correct |
46 |
Correct |
284 ms |
32180 KB |
Output is correct |
47 |
Correct |
1 ms |
856 KB |
Output is correct |
48 |
Correct |
128 ms |
22736 KB |
Output is correct |
49 |
Correct |
125 ms |
22876 KB |
Output is correct |
50 |
Correct |
129 ms |
22868 KB |
Output is correct |
51 |
Correct |
127 ms |
22868 KB |
Output is correct |
52 |
Correct |
128 ms |
22868 KB |
Output is correct |
53 |
Correct |
128 ms |
22720 KB |
Output is correct |
54 |
Correct |
126 ms |
22860 KB |
Output is correct |
55 |
Correct |
128 ms |
22864 KB |
Output is correct |
56 |
Correct |
117 ms |
22868 KB |
Output is correct |
57 |
Correct |
123 ms |
22800 KB |
Output is correct |
58 |
Correct |
149 ms |
22928 KB |
Output is correct |
59 |
Correct |
125 ms |
22608 KB |
Output is correct |
60 |
Correct |
121 ms |
23376 KB |
Output is correct |
61 |
Correct |
128 ms |
22608 KB |
Output is correct |
62 |
Correct |
476 ms |
11428 KB |
Output is correct |
63 |
Correct |
345 ms |
10692 KB |
Output is correct |
64 |
Correct |
370 ms |
11484 KB |
Output is correct |
65 |
Correct |
423 ms |
10728 KB |
Output is correct |
66 |
Correct |
461 ms |
11972 KB |
Output is correct |
67 |
Correct |
451 ms |
11972 KB |
Output is correct |
68 |
Correct |
447 ms |
12224 KB |
Output is correct |
69 |
Correct |
452 ms |
10944 KB |
Output is correct |
70 |
Correct |
438 ms |
11972 KB |
Output is correct |
71 |
Correct |
456 ms |
10804 KB |
Output is correct |
72 |
Correct |
454 ms |
11756 KB |
Output is correct |
73 |
Correct |
456 ms |
11456 KB |
Output is correct |
74 |
Correct |
474 ms |
10948 KB |
Output is correct |
75 |
Correct |
459 ms |
12184 KB |
Output is correct |
76 |
Correct |
446 ms |
11740 KB |
Output is correct |
77 |
Correct |
437 ms |
10464 KB |
Output is correct |
78 |
Correct |
566 ms |
27828 KB |
Output is correct |
79 |
Correct |
437 ms |
30644 KB |
Output is correct |
80 |
Correct |
498 ms |
29760 KB |
Output is correct |
81 |
Correct |
522 ms |
30128 KB |
Output is correct |
82 |
Correct |
568 ms |
29112 KB |
Output is correct |
83 |
Correct |
587 ms |
29360 KB |
Output is correct |
84 |
Correct |
583 ms |
29108 KB |
Output is correct |
85 |
Correct |
586 ms |
29364 KB |
Output is correct |
86 |
Correct |
573 ms |
28100 KB |
Output is correct |
87 |
Correct |
581 ms |
30132 KB |
Output is correct |
88 |
Correct |
555 ms |
28868 KB |
Output is correct |
89 |
Correct |
580 ms |
27568 KB |
Output is correct |
90 |
Correct |
585 ms |
28884 KB |
Output is correct |
91 |
Correct |
583 ms |
28600 KB |
Output is correct |
92 |
Correct |
582 ms |
30784 KB |
Output is correct |
93 |
Correct |
604 ms |
28852 KB |
Output is correct |