#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<int> replaces_front(m,-1);
{
vector<int> currbest(n+1,-1);
for(int i=0;i<m;i++){
replaces_front[i] = currbest[edges[i].second.first];
currbest[edges[i].second.first]=i;
}
}
vector<int> replaces_back(m,-1);
vector<int> currbest(n+1,-1);
{
for(int i=m-1;i>=0;i--){
replaces_back[i] = currbest[edges[i].second.first];
currbest[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:currbest)if(i!=-1)curr_right.insert(i);
for(int i:curr_right)curredgesr.emplace_back(0,i);
vector<bool> blacklist(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();
int ans = 0;
int cnt = 1;
while(cnt<n){
if(iterl==curredgesl.end()){
if(!blacklist[iterr->second+1]){
ans+=iterr->first;
blacklist[replaces_front[iterr->second]+1]=true;
cnt++;
}
iterr++;
} else if(iterr==curredgesr.end()){
if(!blacklist[iterl->second+1]){
ans+=iterl->first;
blacklist[replaces_back[iterl->second]+1]=true;
cnt++;
}
iterl++;
} else 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 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
5098 ms |
12276 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |