#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#ifndef LOCAL
#define cerr if(false)cerr
#endif
const ll MAX_N = 2e5 + 10;
ll n, m;
pair<ll, pair<ll, ll> > edg[MAX_N];
struct {
ll par[MAX_N], sz[MAX_N];
vector<ll> now;
void reset() {
for(ll i = 1; i <= n; i ++) {
par[i] = i;
sz[i] = 1;
}
now.resize(0);
}
ll find(ll x) {
if(par[x] == x) {
return x;
} else {
return par[x] = find(par[x]);
}
}
bool merge(ll x, ll y, ll c) {
x = find(x); y = find(y);
if(x == y) { return false; }
if(sz[x] < sz[y]) { swap(x, y); }
par[y] = x;
sz[x] += sz[y];
now.push_back(c);
return true;
}
} dsu;
vector<ll> cand[MAX_N];
ll lft[MAX_N], rght[MAX_N];
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> m;
for(ll i = 0; i < m; i ++) {
cin >> edg[i].second.first >> edg[i].second.second >> edg[i].first;
}
sort(edg, edg + m);
dsu.reset();
for(ll i = 0; i < m; i ++) {
int other = -1;
dsu.reset();
dsu.merge(edg[i].second.first, edg[i].second.second, i);
if(i != 0) for(const auto j : cand[i - 1]) {
if(!dsu.merge(edg[j].second.first, edg[j].second.second, j)) {
other = j;
}
}
cand[i] = dsu.now;
if(other != -1) {
lft[i] = (edg[i].first + edg[other].first + 2ll) / 2ll;
rght[other] = (edg[i].first + edg[other].first) / 2ll;
} else {
lft[i] = -1e12;
}
rght[i] = 1e12;
}
priority_queue<pair<ll, pair<ll, ll> > > pq;
for(int i = 0; i < m; i ++) {
pq.push({-lft[i], {-1, edg[i].first}});
pq.push({-edg[i].first, {0, edg[i].first}});
pq.push({-rght[i] - 1, {1, edg[i].first}});
}
ll cntadd = 0;
ll cntrem = 0;
ll sum = 0;
ll q;
cin >> q;
while(q --) {
ll x;
cin >> x;
while(!pq.empty() && -pq.top().first <= x) {
auto curr = pq.top(); pq.pop();
if(curr.second.first == -1) {
cntadd ++;
sum += curr.second.second;
} else if(curr.second.first == 0) {
cntadd --;
cntrem ++;
sum -= curr.second.second * 2ll;
} else {
cntrem --;
sum += curr.second.second;
}
}
ll ans = sum - cntadd * x + cntrem * x;
cout << ans << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
627 ms |
411612 KB |
Output is correct |
21 |
Correct |
540 ms |
390648 KB |
Output is correct |
22 |
Correct |
548 ms |
406464 KB |
Output is correct |
23 |
Correct |
532 ms |
410548 KB |
Output is correct |
24 |
Correct |
501 ms |
411232 KB |
Output is correct |
25 |
Correct |
556 ms |
411452 KB |
Output is correct |
26 |
Correct |
567 ms |
411536 KB |
Output is correct |
27 |
Correct |
535 ms |
411628 KB |
Output is correct |
28 |
Correct |
507 ms |
411616 KB |
Output is correct |
29 |
Correct |
538 ms |
411008 KB |
Output is correct |
30 |
Correct |
542 ms |
411576 KB |
Output is correct |
31 |
Correct |
516 ms |
411560 KB |
Output is correct |
32 |
Correct |
516 ms |
411616 KB |
Output is correct |
33 |
Correct |
513 ms |
411544 KB |
Output is correct |
34 |
Correct |
444 ms |
411580 KB |
Output is correct |
35 |
Correct |
550 ms |
411528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
1717 ms |
425336 KB |
Output is correct |
5 |
Correct |
1639 ms |
428104 KB |
Output is correct |
6 |
Correct |
1743 ms |
427968 KB |
Output is correct |
7 |
Correct |
1440 ms |
272568 KB |
Output is correct |
8 |
Correct |
1394 ms |
242668 KB |
Output is correct |
9 |
Correct |
1397 ms |
238256 KB |
Output is correct |
10 |
Correct |
1567 ms |
428132 KB |
Output is correct |
11 |
Correct |
1385 ms |
244924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
1116 ms |
20296 KB |
Output is correct |
21 |
Correct |
1122 ms |
20284 KB |
Output is correct |
22 |
Correct |
1148 ms |
20344 KB |
Output is correct |
23 |
Correct |
1133 ms |
20360 KB |
Output is correct |
24 |
Correct |
1156 ms |
20360 KB |
Output is correct |
25 |
Correct |
1159 ms |
20284 KB |
Output is correct |
26 |
Correct |
1184 ms |
20316 KB |
Output is correct |
27 |
Correct |
1120 ms |
20256 KB |
Output is correct |
28 |
Correct |
1187 ms |
20248 KB |
Output is correct |
29 |
Correct |
1151 ms |
20352 KB |
Output is correct |
30 |
Correct |
1125 ms |
20300 KB |
Output is correct |
31 |
Correct |
1205 ms |
20252 KB |
Output is correct |
32 |
Correct |
1174 ms |
20920 KB |
Output is correct |
33 |
Correct |
1110 ms |
20212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
627 ms |
411612 KB |
Output is correct |
21 |
Correct |
540 ms |
390648 KB |
Output is correct |
22 |
Correct |
548 ms |
406464 KB |
Output is correct |
23 |
Correct |
532 ms |
410548 KB |
Output is correct |
24 |
Correct |
501 ms |
411232 KB |
Output is correct |
25 |
Correct |
556 ms |
411452 KB |
Output is correct |
26 |
Correct |
567 ms |
411536 KB |
Output is correct |
27 |
Correct |
535 ms |
411628 KB |
Output is correct |
28 |
Correct |
507 ms |
411616 KB |
Output is correct |
29 |
Correct |
538 ms |
411008 KB |
Output is correct |
30 |
Correct |
542 ms |
411576 KB |
Output is correct |
31 |
Correct |
516 ms |
411560 KB |
Output is correct |
32 |
Correct |
516 ms |
411616 KB |
Output is correct |
33 |
Correct |
513 ms |
411544 KB |
Output is correct |
34 |
Correct |
444 ms |
411580 KB |
Output is correct |
35 |
Correct |
550 ms |
411528 KB |
Output is correct |
36 |
Correct |
532 ms |
411640 KB |
Output is correct |
37 |
Correct |
481 ms |
388852 KB |
Output is correct |
38 |
Correct |
500 ms |
406796 KB |
Output is correct |
39 |
Correct |
502 ms |
410724 KB |
Output is correct |
40 |
Correct |
521 ms |
411360 KB |
Output is correct |
41 |
Correct |
531 ms |
411516 KB |
Output is correct |
42 |
Correct |
560 ms |
411532 KB |
Output is correct |
43 |
Correct |
548 ms |
411684 KB |
Output is correct |
44 |
Correct |
541 ms |
411580 KB |
Output is correct |
45 |
Correct |
474 ms |
411000 KB |
Output is correct |
46 |
Correct |
538 ms |
411648 KB |
Output is correct |
47 |
Correct |
528 ms |
411652 KB |
Output is correct |
48 |
Correct |
558 ms |
411536 KB |
Output is correct |
49 |
Correct |
539 ms |
411576 KB |
Output is correct |
50 |
Correct |
415 ms |
411568 KB |
Output is correct |
51 |
Correct |
557 ms |
411600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
627 ms |
411612 KB |
Output is correct |
21 |
Correct |
540 ms |
390648 KB |
Output is correct |
22 |
Correct |
548 ms |
406464 KB |
Output is correct |
23 |
Correct |
532 ms |
410548 KB |
Output is correct |
24 |
Correct |
501 ms |
411232 KB |
Output is correct |
25 |
Correct |
556 ms |
411452 KB |
Output is correct |
26 |
Correct |
567 ms |
411536 KB |
Output is correct |
27 |
Correct |
535 ms |
411628 KB |
Output is correct |
28 |
Correct |
507 ms |
411616 KB |
Output is correct |
29 |
Correct |
538 ms |
411008 KB |
Output is correct |
30 |
Correct |
542 ms |
411576 KB |
Output is correct |
31 |
Correct |
516 ms |
411560 KB |
Output is correct |
32 |
Correct |
516 ms |
411616 KB |
Output is correct |
33 |
Correct |
513 ms |
411544 KB |
Output is correct |
34 |
Correct |
444 ms |
411580 KB |
Output is correct |
35 |
Correct |
550 ms |
411528 KB |
Output is correct |
36 |
Correct |
2 ms |
4948 KB |
Output is correct |
37 |
Correct |
2 ms |
4948 KB |
Output is correct |
38 |
Correct |
2 ms |
4948 KB |
Output is correct |
39 |
Correct |
1717 ms |
425336 KB |
Output is correct |
40 |
Correct |
1639 ms |
428104 KB |
Output is correct |
41 |
Correct |
1743 ms |
427968 KB |
Output is correct |
42 |
Correct |
1440 ms |
272568 KB |
Output is correct |
43 |
Correct |
1394 ms |
242668 KB |
Output is correct |
44 |
Correct |
1397 ms |
238256 KB |
Output is correct |
45 |
Correct |
1567 ms |
428132 KB |
Output is correct |
46 |
Correct |
1385 ms |
244924 KB |
Output is correct |
47 |
Correct |
2 ms |
4948 KB |
Output is correct |
48 |
Correct |
1116 ms |
20296 KB |
Output is correct |
49 |
Correct |
1122 ms |
20284 KB |
Output is correct |
50 |
Correct |
1148 ms |
20344 KB |
Output is correct |
51 |
Correct |
1133 ms |
20360 KB |
Output is correct |
52 |
Correct |
1156 ms |
20360 KB |
Output is correct |
53 |
Correct |
1159 ms |
20284 KB |
Output is correct |
54 |
Correct |
1184 ms |
20316 KB |
Output is correct |
55 |
Correct |
1120 ms |
20256 KB |
Output is correct |
56 |
Correct |
1187 ms |
20248 KB |
Output is correct |
57 |
Correct |
1151 ms |
20352 KB |
Output is correct |
58 |
Correct |
1125 ms |
20300 KB |
Output is correct |
59 |
Correct |
1205 ms |
20252 KB |
Output is correct |
60 |
Correct |
1174 ms |
20920 KB |
Output is correct |
61 |
Correct |
1110 ms |
20212 KB |
Output is correct |
62 |
Correct |
532 ms |
411640 KB |
Output is correct |
63 |
Correct |
481 ms |
388852 KB |
Output is correct |
64 |
Correct |
500 ms |
406796 KB |
Output is correct |
65 |
Correct |
502 ms |
410724 KB |
Output is correct |
66 |
Correct |
521 ms |
411360 KB |
Output is correct |
67 |
Correct |
531 ms |
411516 KB |
Output is correct |
68 |
Correct |
560 ms |
411532 KB |
Output is correct |
69 |
Correct |
548 ms |
411684 KB |
Output is correct |
70 |
Correct |
541 ms |
411580 KB |
Output is correct |
71 |
Correct |
474 ms |
411000 KB |
Output is correct |
72 |
Correct |
538 ms |
411648 KB |
Output is correct |
73 |
Correct |
528 ms |
411652 KB |
Output is correct |
74 |
Correct |
558 ms |
411536 KB |
Output is correct |
75 |
Correct |
539 ms |
411576 KB |
Output is correct |
76 |
Correct |
415 ms |
411568 KB |
Output is correct |
77 |
Correct |
557 ms |
411600 KB |
Output is correct |
78 |
Correct |
1643 ms |
427880 KB |
Output is correct |
79 |
Correct |
1638 ms |
404340 KB |
Output is correct |
80 |
Correct |
1652 ms |
423904 KB |
Output is correct |
81 |
Correct |
1634 ms |
427816 KB |
Output is correct |
82 |
Correct |
1715 ms |
427720 KB |
Output is correct |
83 |
Correct |
1660 ms |
427652 KB |
Output is correct |
84 |
Correct |
1668 ms |
427748 KB |
Output is correct |
85 |
Correct |
1670 ms |
427844 KB |
Output is correct |
86 |
Correct |
1636 ms |
427708 KB |
Output is correct |
87 |
Correct |
1610 ms |
428800 KB |
Output is correct |
88 |
Correct |
1631 ms |
427716 KB |
Output is correct |
89 |
Correct |
1659 ms |
427772 KB |
Output is correct |
90 |
Correct |
1632 ms |
427804 KB |
Output is correct |
91 |
Correct |
1654 ms |
427688 KB |
Output is correct |
92 |
Correct |
1528 ms |
430824 KB |
Output is correct |
93 |
Correct |
1624 ms |
428848 KB |
Output is correct |