#pragma GCC optimize("Ofast","unroll-loops")
#include <bits/stdc++.h>
using namespace std;
// #define int long long
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,m;
cin >> n >> m;
vector<pair<int,pair<int,int>>> edges(m);
for(auto&[a,b]:edges)cin>>b.first>>b.second>>a;
sort(edges.begin(),edges.end());
vector<set<pair<int,int>>> adj(n+1);
// Calculation from the front
int tar;
function<pair<bool,int>(int,int)> dfs = [&](int x,int p){
if(x==tar)return make_pair(true,(int)1e15);
for(auto[v,i]:adj[x])if(v!=p){
auto res = dfs(v,x);
if(res.first)return make_pair(true,min(res.second,i));
}
return make_pair(false,(int)0);
};
vector<tuple<int,bool,int>> events;
for(int i=0;i<m;i++){
tar = edges[i].second.second;
auto res = dfs(edges[i].second.first,-1);
if(res.first){
//res.second is the edge which is replaced by i
int diffpoint = (edges[i].first+edges[res.second].first+1)/2;
events.emplace_back(diffpoint,true,+edges[i].first);
events.emplace_back(diffpoint,false,-edges[res.second].first);
adj[edges[res.second].second.first].erase({edges[res.second].second.second,res.second});
adj[edges[res.second].second.second].erase({edges[res.second].second.first,res.second});
} else {
events.emplace_back(0,true,+edges[i].first);
}
events.emplace_back(edges[i].first,true,-edges[i].first);
events.emplace_back(edges[i].first,false,+edges[i].first);
adj[edges[i].second.first].insert({edges[i].second.second,i});
adj[edges[i].second.second].insert({edges[i].second.first,i});
}
events.emplace_back(2e9,false,0);
sort(events.begin(),events.end());
long long smallersum = 0;
long long biggersum = 0;
long long smallercnt = 0;
long long biggercnt = 0;
auto iter = events.begin();
int q;
cin >> q;
for(int i=1;i<=q;i++){
long long query;
cin >> query;
while(get<0>(*iter)<=query){
auto [a,b,c] = *iter;
if(b){
biggersum+=c;
if(c<0)biggercnt--;
else biggercnt++;
} else {
smallersum+=c;
if(c<0)smallercnt--;
else smallercnt++;
}
iter++;
}
cout << biggersum-query*biggercnt+query*smallercnt-smallersum << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
383 ms |
9504 KB |
Output is correct |
21 |
Correct |
195 ms |
9500 KB |
Output is correct |
22 |
Correct |
263 ms |
9552 KB |
Output is correct |
23 |
Correct |
332 ms |
9448 KB |
Output is correct |
24 |
Correct |
338 ms |
9708 KB |
Output is correct |
25 |
Correct |
371 ms |
9456 KB |
Output is correct |
26 |
Correct |
392 ms |
9716 KB |
Output is correct |
27 |
Correct |
393 ms |
9712 KB |
Output is correct |
28 |
Correct |
397 ms |
9692 KB |
Output is correct |
29 |
Correct |
294 ms |
9584 KB |
Output is correct |
30 |
Correct |
388 ms |
9632 KB |
Output is correct |
31 |
Correct |
384 ms |
9520 KB |
Output is correct |
32 |
Correct |
387 ms |
9688 KB |
Output is correct |
33 |
Correct |
395 ms |
9688 KB |
Output is correct |
34 |
Correct |
240 ms |
9728 KB |
Output is correct |
35 |
Correct |
371 ms |
9436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
576 ms |
28748 KB |
Output is correct |
5 |
Correct |
497 ms |
28892 KB |
Output is correct |
6 |
Correct |
529 ms |
28888 KB |
Output is correct |
7 |
Correct |
199 ms |
30828 KB |
Output is correct |
8 |
Correct |
188 ms |
30904 KB |
Output is correct |
9 |
Correct |
182 ms |
30796 KB |
Output is correct |
10 |
Correct |
565 ms |
28892 KB |
Output is correct |
11 |
Correct |
186 ms |
30996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
133 ms |
22344 KB |
Output is correct |
21 |
Correct |
130 ms |
22612 KB |
Output is correct |
22 |
Correct |
127 ms |
22356 KB |
Output is correct |
23 |
Correct |
132 ms |
22356 KB |
Output is correct |
24 |
Correct |
128 ms |
22436 KB |
Output is correct |
25 |
Correct |
134 ms |
22352 KB |
Output is correct |
26 |
Correct |
130 ms |
22368 KB |
Output is correct |
27 |
Correct |
129 ms |
22356 KB |
Output is correct |
28 |
Correct |
125 ms |
22352 KB |
Output is correct |
29 |
Correct |
126 ms |
22356 KB |
Output is correct |
30 |
Correct |
125 ms |
22436 KB |
Output is correct |
31 |
Correct |
135 ms |
22352 KB |
Output is correct |
32 |
Correct |
136 ms |
22864 KB |
Output is correct |
33 |
Correct |
129 ms |
22356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
383 ms |
9504 KB |
Output is correct |
21 |
Correct |
195 ms |
9500 KB |
Output is correct |
22 |
Correct |
263 ms |
9552 KB |
Output is correct |
23 |
Correct |
332 ms |
9448 KB |
Output is correct |
24 |
Correct |
338 ms |
9708 KB |
Output is correct |
25 |
Correct |
371 ms |
9456 KB |
Output is correct |
26 |
Correct |
392 ms |
9716 KB |
Output is correct |
27 |
Correct |
393 ms |
9712 KB |
Output is correct |
28 |
Correct |
397 ms |
9692 KB |
Output is correct |
29 |
Correct |
294 ms |
9584 KB |
Output is correct |
30 |
Correct |
388 ms |
9632 KB |
Output is correct |
31 |
Correct |
384 ms |
9520 KB |
Output is correct |
32 |
Correct |
387 ms |
9688 KB |
Output is correct |
33 |
Correct |
395 ms |
9688 KB |
Output is correct |
34 |
Correct |
240 ms |
9728 KB |
Output is correct |
35 |
Correct |
371 ms |
9436 KB |
Output is correct |
36 |
Correct |
414 ms |
9688 KB |
Output is correct |
37 |
Correct |
178 ms |
9812 KB |
Output is correct |
38 |
Correct |
281 ms |
9696 KB |
Output is correct |
39 |
Correct |
295 ms |
9756 KB |
Output is correct |
40 |
Correct |
327 ms |
9712 KB |
Output is correct |
41 |
Correct |
360 ms |
9716 KB |
Output is correct |
42 |
Correct |
394 ms |
9784 KB |
Output is correct |
43 |
Correct |
382 ms |
9688 KB |
Output is correct |
44 |
Correct |
386 ms |
9776 KB |
Output is correct |
45 |
Correct |
289 ms |
9672 KB |
Output is correct |
46 |
Correct |
393 ms |
9756 KB |
Output is correct |
47 |
Correct |
385 ms |
9688 KB |
Output is correct |
48 |
Correct |
383 ms |
9688 KB |
Output is correct |
49 |
Correct |
385 ms |
9692 KB |
Output is correct |
50 |
Correct |
241 ms |
9944 KB |
Output is correct |
51 |
Correct |
377 ms |
9712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
383 ms |
9504 KB |
Output is correct |
21 |
Correct |
195 ms |
9500 KB |
Output is correct |
22 |
Correct |
263 ms |
9552 KB |
Output is correct |
23 |
Correct |
332 ms |
9448 KB |
Output is correct |
24 |
Correct |
338 ms |
9708 KB |
Output is correct |
25 |
Correct |
371 ms |
9456 KB |
Output is correct |
26 |
Correct |
392 ms |
9716 KB |
Output is correct |
27 |
Correct |
393 ms |
9712 KB |
Output is correct |
28 |
Correct |
397 ms |
9692 KB |
Output is correct |
29 |
Correct |
294 ms |
9584 KB |
Output is correct |
30 |
Correct |
388 ms |
9632 KB |
Output is correct |
31 |
Correct |
384 ms |
9520 KB |
Output is correct |
32 |
Correct |
387 ms |
9688 KB |
Output is correct |
33 |
Correct |
395 ms |
9688 KB |
Output is correct |
34 |
Correct |
240 ms |
9728 KB |
Output is correct |
35 |
Correct |
371 ms |
9436 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
576 ms |
28748 KB |
Output is correct |
40 |
Correct |
497 ms |
28892 KB |
Output is correct |
41 |
Correct |
529 ms |
28888 KB |
Output is correct |
42 |
Correct |
199 ms |
30828 KB |
Output is correct |
43 |
Correct |
188 ms |
30904 KB |
Output is correct |
44 |
Correct |
182 ms |
30796 KB |
Output is correct |
45 |
Correct |
565 ms |
28892 KB |
Output is correct |
46 |
Correct |
186 ms |
30996 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
133 ms |
22344 KB |
Output is correct |
49 |
Correct |
130 ms |
22612 KB |
Output is correct |
50 |
Correct |
127 ms |
22356 KB |
Output is correct |
51 |
Correct |
132 ms |
22356 KB |
Output is correct |
52 |
Correct |
128 ms |
22436 KB |
Output is correct |
53 |
Correct |
134 ms |
22352 KB |
Output is correct |
54 |
Correct |
130 ms |
22368 KB |
Output is correct |
55 |
Correct |
129 ms |
22356 KB |
Output is correct |
56 |
Correct |
125 ms |
22352 KB |
Output is correct |
57 |
Correct |
126 ms |
22356 KB |
Output is correct |
58 |
Correct |
125 ms |
22436 KB |
Output is correct |
59 |
Correct |
135 ms |
22352 KB |
Output is correct |
60 |
Correct |
136 ms |
22864 KB |
Output is correct |
61 |
Correct |
129 ms |
22356 KB |
Output is correct |
62 |
Correct |
414 ms |
9688 KB |
Output is correct |
63 |
Correct |
178 ms |
9812 KB |
Output is correct |
64 |
Correct |
281 ms |
9696 KB |
Output is correct |
65 |
Correct |
295 ms |
9756 KB |
Output is correct |
66 |
Correct |
327 ms |
9712 KB |
Output is correct |
67 |
Correct |
360 ms |
9716 KB |
Output is correct |
68 |
Correct |
394 ms |
9784 KB |
Output is correct |
69 |
Correct |
382 ms |
9688 KB |
Output is correct |
70 |
Correct |
386 ms |
9776 KB |
Output is correct |
71 |
Correct |
289 ms |
9672 KB |
Output is correct |
72 |
Correct |
393 ms |
9756 KB |
Output is correct |
73 |
Correct |
385 ms |
9688 KB |
Output is correct |
74 |
Correct |
383 ms |
9688 KB |
Output is correct |
75 |
Correct |
385 ms |
9692 KB |
Output is correct |
76 |
Correct |
241 ms |
9944 KB |
Output is correct |
77 |
Correct |
377 ms |
9712 KB |
Output is correct |
78 |
Correct |
518 ms |
27856 KB |
Output is correct |
79 |
Correct |
301 ms |
29904 KB |
Output is correct |
80 |
Correct |
407 ms |
28764 KB |
Output is correct |
81 |
Correct |
408 ms |
29148 KB |
Output is correct |
82 |
Correct |
453 ms |
27876 KB |
Output is correct |
83 |
Correct |
515 ms |
27860 KB |
Output is correct |
84 |
Correct |
494 ms |
27860 KB |
Output is correct |
85 |
Correct |
510 ms |
28116 KB |
Output is correct |
86 |
Correct |
495 ms |
27868 KB |
Output is correct |
87 |
Correct |
410 ms |
29532 KB |
Output is correct |
88 |
Correct |
488 ms |
27860 KB |
Output is correct |
89 |
Correct |
478 ms |
27864 KB |
Output is correct |
90 |
Correct |
507 ms |
27856 KB |
Output is correct |
91 |
Correct |
514 ms |
27860 KB |
Output is correct |
92 |
Correct |
394 ms |
30872 KB |
Output is correct |
93 |
Correct |
528 ms |
28884 KB |
Output is correct |