#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;
vector<int> replaces_front(m,-1);
{
// Calculation from the front
adj = vector<set<pair<int,int>>>(n+1);
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);
};
for(int i=0;i<m;i++){
tar = edges[i].second.second;
auto res = dfs(edges[i].second.first,-1);
if(res.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});
replaces_front[i] = res.second;
}
adj[edges[i].second.first].insert({edges[i].second.second,i});
adj[edges[i].second.second].insert({edges[i].second.first,i});
}
}
vector<int> replaces_back(m,-1);
{
// Calculation from the back
adj = vector<set<pair<int,int>>>(n+1);
int tar;
function<pair<bool,int>(int,int)> dfs = [&](int x,int p){
if(x==tar)return make_pair(true,(int)-1);
for(auto[v,i]:adj[x])if(v!=p){
auto res = dfs(v,x);
if(res.first)return make_pair(true,max(res.second,i));
}
return make_pair(false,(int)0);
};
for(int i=m-1;i>=0;i--){
tar = edges[i].second.second;
auto res = dfs(edges[i].second.first,-1);
if(res.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});
replaces_back[i] = res.second;
}
adj[edges[i].second.first].insert({edges[i].second.second,i});
adj[edges[i].second.second].insert({edges[i].second.first,i});
}
}
set<int> curr_left;
set<int> curr_right;
vector<pair<int,int>> curredgesl;
vector<pair<int,int>> curredgesr;
bool changed;
for(auto&i:adj)for(auto[a,b]:i)curr_right.insert(b);
for(int i:curr_right)curredgesr.emplace_back(0,i);
vector<bool> blacklist(m+3);
edges.emplace_back(-1e9,make_pair(0,0));
curr_left.insert(m);
curredgesl.emplace_back(0,m);
edges.emplace_back(2e9,make_pair(0,0));
curr_right.insert(m+1);
curredgesr.emplace_back(0,m+1);
auto solve = [&](int x){
if(changed){
curredgesl.clear();
for(int i:curr_left){
curredgesl.emplace_back(x-edges[i].first,i);
}
sort(curredgesl.begin(),curredgesl.end());
curredgesr.clear();
for(int i:curr_right){
curredgesr.emplace_back(edges[i].first-x,i);
}
sort(curredgesr.begin(),curredgesr.end());
} else {
for(auto&[c,i]:curredgesl)c=x-edges[i].first;
for(auto&[c,i]:curredgesr)c=edges[i].first-x;
}
for(auto[c,i]:curredgesl)blacklist[i+1]=false;
for(auto[c,i]:curredgesr)blacklist[i+1]=false;
auto iterl = curredgesl.begin();
auto iterr = curredgesr.begin();
long long ans = 0;
int cnt = 1;
while(cnt<n){
if(iterl->first<iterr->first){
if(!blacklist[iterl->second+1]){
ans+=iterl->first;
blacklist[replaces_back[iterl->second]+1]=true;
cnt++;
}
iterl++;
} else {
if(!blacklist[iterr->second+1]){
ans+=iterr->first;
blacklist[replaces_front[iterr->second]+1]=true;
cnt++;
}
iterr++;
}
}
return ans;
};
int iter = 0;
int q;
cin >> q;
for(int i=1;i<=q;i++){
int query;
cin >> query;
changed = false;
while(iter!=m and edges[iter].first<query){
curr_right.erase(iter);
if(replaces_back[iter]!=-1){
curr_right.insert(replaces_back[iter]);
}
curr_left.insert(iter);
if(replaces_front[iter]!=-1){
curr_left.erase(replaces_front[iter]);
}
iter++;
changed = true;
}
cout << solve(query) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 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 |
452 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
452 KB |
Output is correct |
15 |
Correct |
0 ms |
352 KB |
Output is correct |
16 |
Correct |
0 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 |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 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 |
452 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
452 KB |
Output is correct |
15 |
Correct |
0 ms |
352 KB |
Output is correct |
16 |
Correct |
0 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 |
344 KB |
Output is correct |
20 |
Correct |
788 ms |
5156 KB |
Output is correct |
21 |
Correct |
401 ms |
5060 KB |
Output is correct |
22 |
Correct |
528 ms |
5148 KB |
Output is correct |
23 |
Correct |
584 ms |
5052 KB |
Output is correct |
24 |
Correct |
686 ms |
5200 KB |
Output is correct |
25 |
Correct |
762 ms |
5164 KB |
Output is correct |
26 |
Correct |
796 ms |
5156 KB |
Output is correct |
27 |
Correct |
781 ms |
5156 KB |
Output is correct |
28 |
Correct |
811 ms |
5204 KB |
Output is correct |
29 |
Correct |
597 ms |
5060 KB |
Output is correct |
30 |
Correct |
762 ms |
5160 KB |
Output is correct |
31 |
Correct |
829 ms |
5160 KB |
Output is correct |
32 |
Correct |
783 ms |
5000 KB |
Output is correct |
33 |
Correct |
789 ms |
5156 KB |
Output is correct |
34 |
Correct |
395 ms |
5452 KB |
Output is correct |
35 |
Correct |
757 ms |
5204 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 |
Execution timed out |
5078 ms |
13508 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 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 |
452 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
452 KB |
Output is correct |
15 |
Correct |
0 ms |
352 KB |
Output is correct |
16 |
Correct |
0 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 |
2624 ms |
20480 KB |
Output is correct |
21 |
Correct |
2570 ms |
20484 KB |
Output is correct |
22 |
Correct |
2559 ms |
20476 KB |
Output is correct |
23 |
Correct |
2646 ms |
17904 KB |
Output is correct |
24 |
Correct |
2556 ms |
17744 KB |
Output is correct |
25 |
Correct |
2536 ms |
17972 KB |
Output is correct |
26 |
Correct |
2587 ms |
12808 KB |
Output is correct |
27 |
Correct |
2538 ms |
12672 KB |
Output is correct |
28 |
Correct |
2537 ms |
12832 KB |
Output is correct |
29 |
Correct |
2542 ms |
12740 KB |
Output is correct |
30 |
Correct |
2515 ms |
12804 KB |
Output is correct |
31 |
Correct |
2643 ms |
12716 KB |
Output is correct |
32 |
Correct |
1921 ms |
13340 KB |
Output is correct |
33 |
Correct |
2771 ms |
12480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 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 |
452 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
452 KB |
Output is correct |
15 |
Correct |
0 ms |
352 KB |
Output is correct |
16 |
Correct |
0 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 |
344 KB |
Output is correct |
20 |
Correct |
788 ms |
5156 KB |
Output is correct |
21 |
Correct |
401 ms |
5060 KB |
Output is correct |
22 |
Correct |
528 ms |
5148 KB |
Output is correct |
23 |
Correct |
584 ms |
5052 KB |
Output is correct |
24 |
Correct |
686 ms |
5200 KB |
Output is correct |
25 |
Correct |
762 ms |
5164 KB |
Output is correct |
26 |
Correct |
796 ms |
5156 KB |
Output is correct |
27 |
Correct |
781 ms |
5156 KB |
Output is correct |
28 |
Correct |
811 ms |
5204 KB |
Output is correct |
29 |
Correct |
597 ms |
5060 KB |
Output is correct |
30 |
Correct |
762 ms |
5160 KB |
Output is correct |
31 |
Correct |
829 ms |
5160 KB |
Output is correct |
32 |
Correct |
783 ms |
5000 KB |
Output is correct |
33 |
Correct |
789 ms |
5156 KB |
Output is correct |
34 |
Correct |
395 ms |
5452 KB |
Output is correct |
35 |
Correct |
757 ms |
5204 KB |
Output is correct |
36 |
Correct |
1305 ms |
3620 KB |
Output is correct |
37 |
Correct |
909 ms |
3664 KB |
Output is correct |
38 |
Correct |
1156 ms |
3620 KB |
Output is correct |
39 |
Correct |
1177 ms |
3664 KB |
Output is correct |
40 |
Correct |
1216 ms |
3664 KB |
Output is correct |
41 |
Correct |
1305 ms |
3624 KB |
Output is correct |
42 |
Correct |
1307 ms |
3660 KB |
Output is correct |
43 |
Correct |
1451 ms |
3528 KB |
Output is correct |
44 |
Correct |
887 ms |
3688 KB |
Output is correct |
45 |
Correct |
647 ms |
3668 KB |
Output is correct |
46 |
Correct |
1300 ms |
3668 KB |
Output is correct |
47 |
Correct |
1297 ms |
3668 KB |
Output is correct |
48 |
Correct |
1274 ms |
3664 KB |
Output is correct |
49 |
Correct |
1304 ms |
3616 KB |
Output is correct |
50 |
Correct |
425 ms |
3880 KB |
Output is correct |
51 |
Correct |
850 ms |
3668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 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 |
452 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
452 KB |
Output is correct |
15 |
Correct |
0 ms |
352 KB |
Output is correct |
16 |
Correct |
0 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 |
344 KB |
Output is correct |
20 |
Correct |
788 ms |
5156 KB |
Output is correct |
21 |
Correct |
401 ms |
5060 KB |
Output is correct |
22 |
Correct |
528 ms |
5148 KB |
Output is correct |
23 |
Correct |
584 ms |
5052 KB |
Output is correct |
24 |
Correct |
686 ms |
5200 KB |
Output is correct |
25 |
Correct |
762 ms |
5164 KB |
Output is correct |
26 |
Correct |
796 ms |
5156 KB |
Output is correct |
27 |
Correct |
781 ms |
5156 KB |
Output is correct |
28 |
Correct |
811 ms |
5204 KB |
Output is correct |
29 |
Correct |
597 ms |
5060 KB |
Output is correct |
30 |
Correct |
762 ms |
5160 KB |
Output is correct |
31 |
Correct |
829 ms |
5160 KB |
Output is correct |
32 |
Correct |
783 ms |
5000 KB |
Output is correct |
33 |
Correct |
789 ms |
5156 KB |
Output is correct |
34 |
Correct |
395 ms |
5452 KB |
Output is correct |
35 |
Correct |
757 ms |
5204 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 |
Execution timed out |
5078 ms |
13508 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |