This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define ll long long
#define pll pair<ll,ll>
#define plx pair<ll,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int mxn=250005;
int id[mxn]{0},rev[mxn]{0};
struct node{
pll v;
node*l,*r;
node(): v({1e18,-1}),l(NULL),r(NULL){};
node(ll x,ll y) : v({x,y}),l(NULL),r(NULL){};
node(node*l,node*r) : v(min(l->v,r->v)),l(l),r(r){};
};map<pll,bool>mp;
node*pre[2][mxn],*suf[2][mxn];
pll p[mxn];
pair<pll,int>y[mxn];
node* build(node*t,int l,int r){
t=new node();
if(l==r)return new node(1e18,l);
int m=(l+r)>>1;
return new node(build(t->l,l,m),build(t->r,m+1,r));
}
node *update(node*t,int l,int r,int id,ll v){
if(l==r)return new node(v,id);
int m=(l+r)>>1;
if(id<=m)return new node(update(t->l,l,m,id,v),t->r);
else return new node(t->l,update(t->r,m+1,r,id,v));
}
pll qr(node*t,int l,int r,int tl,int tr){
if(r<tl||l>tr)return {1e18,-1};
if(r<=tr&&l>=tl)return t->v;
int m=(l+r)>>1;
return min(qr(t->l,l,m,tl,tr),qr(t->r,m+1,r,tl,tr));
}
bool cmp(pair<pll,ll>a,pair<pll,ll>b){
if(a.f.s==b.f.s)return a<b;
return a.f.s<b.f.s;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n,k;cin>>n>>k;
for(int i=1;i<=n;i++)cin>>p[i].f>>p[i].s,y[i].f=p[i],y[i].s=0;
sort(y+1,y+n+1);for(int i=1;i<=n;i++)y[i].s=i;
sort(y+1,y+n+1,cmp);for(int i=1;i<=n;i++)id[y[i].s]=i,rev[i]=y[i].s;
sort(p+1,p+n+1);
pre[0][1]=build(pre[0][1],1,n);
pre[1][1]=build(pre[1][1],1,n);
for(int i=2;i<=n;i++){
pre[0][i]=update(pre[0][i-1],1,n,id[i-1],-p[i-1].f-p[i-1].s);
pre[1][i]=update(pre[1][i-1],1,n,id[i-1],-p[i-1].f+p[i-1].s);
}suf[0][n]=build(suf[0][n],1,n);suf[1][n]=build(suf[1][n],1,n);
for(int i=n-1;i>=1;i--){
suf[0][i]=update(suf[0][i+1],1,n,id[i+1],p[i+1].f-p[i+1].s);
suf[1][i]=update(suf[1][i+1],1,n,id[i+1],p[i+1].f+p[i+1].s);
}priority_queue<pair<ll,pll>,vector<pair<ll,pll>>,greater<pair<ll,pll>>>pq;
for(int i=1;i<=n;i++){
pll ans1 = qr(pre[0][i],1,n,1,id[i]);
ans1.f+=p[i].f+p[i].s;
pll ans2 = qr(pre[1][i],1,n,id[i],n);
ans2.f+=p[i].f-p[i].s;
pll ans3 = qr(suf[0][i],1,n,1,id[i]);
ans3.f+=-p[i].f+p[i].s;
pll ans4 = qr(suf[1][i],1,n,id[i],n);
ans4.f+=-p[i].f-p[i].s;
ll res=1e17;int mem=-1;
if(res>ans1.f)res=ans1.f,mem=ans1.s;
if(res>ans2.f)res=ans2.f,mem=ans2.s;
if(res>ans3.f)res=ans3.f,mem=ans3.s;
if(res>ans4.f)res=ans4.f,mem=ans4.s;
pq.push({res,{i,mem}});
}vector<ll>ans;ll cnt=k;
while(cnt>0){
auto u=pq.top();pq.pop();
if(mp.find({u.s.f,rev[u.s.s]})==mp.end()){
ans.pb(u.f);mp[{u.s.f,rev[u.s.s]}]=mp[{rev[u.s.s],u.s.f}]=1;cnt--;
}
int i=u.s.f;
if(i>rev[u.s.s]&&id[i]>u.s.s)pre[0][i]=update(pre[0][i],1,n,u.s.s,1e18);
if(i>rev[u.s.s]&&u.s.s>id[i])pre[1][i]=update(pre[1][i],1,n,u.s.s,1e18);
if(i<rev[u.s.s]&&id[i]>u.s.s)suf[0][i]=update(suf[0][i],1,n,u.s.s,1e18);
if(i<rev[u.s.s]&&id[i]<u.s.s)suf[1][i]=update(suf[1][i],1,n,u.s.s,1e18);
pll ans1 = qr(pre[0][i],1,n,1,id[i]);ans1.f+=p[i].f+p[i].s;
pll ans2 = qr(pre[1][i],1,n,id[i],n);ans2.f+=p[i].f-p[i].s;
pll ans3 = qr(suf[0][i],1,n,1,id[i]);ans3.f+=-p[i].f+p[i].s;
pll ans4 = qr(suf[1][i],1,n,id[i],n);ans4.f+=-p[i].f-p[i].s;
ll res=1e17;int mem=-1;
if(res>ans1.f)res=ans1.f,mem=ans1.s;
if(res>ans2.f)res=ans2.f,mem=ans2.s;
if(res>ans3.f)res=ans3.f,mem=ans3.s;
if(res>ans4.f)res=ans4.f,mem=ans4.s;
if(mem==-1)continue;
pq.push({res,{i,mem}});
}
for(int i=0;i<ans.size();i++){
cout<<ans[i]<<'\n';
}
}
Compilation message (stderr)
road_construction.cpp: In function 'int main()':
road_construction.cpp:99:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for(int i=0;i<ans.size();i++){
| ~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |