// #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';
}
}
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
0 ms |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
352 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
508 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
0 ms |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
352 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
508 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
452 KB |
Output is correct |
19 |
Correct |
1 ms |
352 KB |
Output is correct |
20 |
Correct |
758 ms |
5336 KB |
Output is correct |
21 |
Correct |
372 ms |
5204 KB |
Output is correct |
22 |
Correct |
524 ms |
5328 KB |
Output is correct |
23 |
Correct |
587 ms |
5328 KB |
Output is correct |
24 |
Correct |
652 ms |
5336 KB |
Output is correct |
25 |
Correct |
726 ms |
5204 KB |
Output is correct |
26 |
Correct |
756 ms |
5248 KB |
Output is correct |
27 |
Correct |
798 ms |
5328 KB |
Output is correct |
28 |
Correct |
794 ms |
5336 KB |
Output is correct |
29 |
Correct |
608 ms |
5340 KB |
Output is correct |
30 |
Correct |
758 ms |
5328 KB |
Output is correct |
31 |
Correct |
755 ms |
5340 KB |
Output is correct |
32 |
Correct |
769 ms |
5240 KB |
Output is correct |
33 |
Correct |
775 ms |
5204 KB |
Output is correct |
34 |
Correct |
380 ms |
5332 KB |
Output is correct |
35 |
Correct |
769 ms |
5200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Execution timed out |
5065 ms |
14712 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
0 ms |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
352 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
508 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
452 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
2810 ms |
22460 KB |
Output is correct |
21 |
Correct |
2799 ms |
22476 KB |
Output is correct |
22 |
Correct |
2849 ms |
22352 KB |
Output is correct |
23 |
Correct |
2787 ms |
22356 KB |
Output is correct |
24 |
Correct |
2846 ms |
22356 KB |
Output is correct |
25 |
Correct |
2867 ms |
22356 KB |
Output is correct |
26 |
Correct |
2912 ms |
22536 KB |
Output is correct |
27 |
Correct |
2849 ms |
22552 KB |
Output is correct |
28 |
Correct |
2963 ms |
22352 KB |
Output is correct |
29 |
Correct |
2916 ms |
22424 KB |
Output is correct |
30 |
Correct |
2887 ms |
22608 KB |
Output is correct |
31 |
Correct |
2809 ms |
22208 KB |
Output is correct |
32 |
Correct |
2142 ms |
23104 KB |
Output is correct |
33 |
Correct |
3166 ms |
22396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
0 ms |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
352 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
508 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
452 KB |
Output is correct |
19 |
Correct |
1 ms |
352 KB |
Output is correct |
20 |
Correct |
758 ms |
5336 KB |
Output is correct |
21 |
Correct |
372 ms |
5204 KB |
Output is correct |
22 |
Correct |
524 ms |
5328 KB |
Output is correct |
23 |
Correct |
587 ms |
5328 KB |
Output is correct |
24 |
Correct |
652 ms |
5336 KB |
Output is correct |
25 |
Correct |
726 ms |
5204 KB |
Output is correct |
26 |
Correct |
756 ms |
5248 KB |
Output is correct |
27 |
Correct |
798 ms |
5328 KB |
Output is correct |
28 |
Correct |
794 ms |
5336 KB |
Output is correct |
29 |
Correct |
608 ms |
5340 KB |
Output is correct |
30 |
Correct |
758 ms |
5328 KB |
Output is correct |
31 |
Correct |
755 ms |
5340 KB |
Output is correct |
32 |
Correct |
769 ms |
5240 KB |
Output is correct |
33 |
Correct |
775 ms |
5204 KB |
Output is correct |
34 |
Correct |
380 ms |
5332 KB |
Output is correct |
35 |
Correct |
769 ms |
5200 KB |
Output is correct |
36 |
Correct |
1334 ms |
5588 KB |
Output is correct |
37 |
Correct |
951 ms |
5524 KB |
Output is correct |
38 |
Correct |
1217 ms |
5588 KB |
Output is correct |
39 |
Correct |
1212 ms |
5528 KB |
Output is correct |
40 |
Correct |
1259 ms |
5460 KB |
Output is correct |
41 |
Correct |
1343 ms |
5428 KB |
Output is correct |
42 |
Correct |
1405 ms |
5564 KB |
Output is correct |
43 |
Correct |
1494 ms |
5528 KB |
Output is correct |
44 |
Correct |
858 ms |
5456 KB |
Output is correct |
45 |
Correct |
759 ms |
5452 KB |
Output is correct |
46 |
Correct |
1381 ms |
5460 KB |
Output is correct |
47 |
Correct |
1333 ms |
5524 KB |
Output is correct |
48 |
Correct |
1324 ms |
5464 KB |
Output is correct |
49 |
Correct |
1257 ms |
5524 KB |
Output is correct |
50 |
Correct |
449 ms |
5460 KB |
Output is correct |
51 |
Correct |
856 ms |
5528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
0 ms |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
352 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
508 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
452 KB |
Output is correct |
19 |
Correct |
1 ms |
352 KB |
Output is correct |
20 |
Correct |
758 ms |
5336 KB |
Output is correct |
21 |
Correct |
372 ms |
5204 KB |
Output is correct |
22 |
Correct |
524 ms |
5328 KB |
Output is correct |
23 |
Correct |
587 ms |
5328 KB |
Output is correct |
24 |
Correct |
652 ms |
5336 KB |
Output is correct |
25 |
Correct |
726 ms |
5204 KB |
Output is correct |
26 |
Correct |
756 ms |
5248 KB |
Output is correct |
27 |
Correct |
798 ms |
5328 KB |
Output is correct |
28 |
Correct |
794 ms |
5336 KB |
Output is correct |
29 |
Correct |
608 ms |
5340 KB |
Output is correct |
30 |
Correct |
758 ms |
5328 KB |
Output is correct |
31 |
Correct |
755 ms |
5340 KB |
Output is correct |
32 |
Correct |
769 ms |
5240 KB |
Output is correct |
33 |
Correct |
775 ms |
5204 KB |
Output is correct |
34 |
Correct |
380 ms |
5332 KB |
Output is correct |
35 |
Correct |
769 ms |
5200 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
344 KB |
Output is correct |
38 |
Correct |
0 ms |
456 KB |
Output is correct |
39 |
Execution timed out |
5065 ms |
14712 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |