#include<bits/stdc++.h>
using namespace std;
#define foru(i,a,b) for(int i=(a); i<=(b); ++i)
#define ford(i,a,b) for(int i=(a); i>=(b); --i)
#define rep(i,a) for(int i=0; i<(a); ++i)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(),(a).end()
#define bit(s,i) (((s)>>(i))&1)
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define fi first
#define se second
#define ll long long
#define eb emplace_back
#define pb push_back
#define __builtin_popcount __builtin_popcountll
#define _ << " " <<
template <class X, class Y> bool maxi(X &x, Y y){return x<y?x=y,true:false;}
template <class X, class Y> bool mini(X &x, Y y){return x>y?x=y,true:false;}
const int N=252525;
const ll INF=1e18;
int n,k;
ii town[N],sorted[N]; int idSe[N];
struct Node{
pair<ll,int> lt,rt;
int ch[2];
Node(){
lt=rt={INF,0};
ch[0]=ch[1]=0;
}
};
vector<Node> tree(2);
int newNode(int x){
tree.eb(tree[x]);
return sz(tree)-1;
}
int upd(int old, int x, bool sta, int l=1, int r=n){ /// sta=0/1
int cur=newNode(old);
if(l==r){
if(sta){
int pos=sorted[x].se;
tree[cur].lt={-town[pos].fi-town[pos].se, x};
tree[cur].rt={-town[pos].fi+town[pos].se, x};
} else{
tree[cur].lt={INF, x};
tree[cur].rt={INF, x};
}
return cur;
}
int mid=(l+r)>>1;
if(x<=mid){
tree[cur].ch[0]=upd(tree[cur].ch[0],x,sta,l,mid);
} else{
tree[cur].ch[1]=upd(tree[cur].ch[1],x,sta,mid+1,r);
}
tree[cur].lt=min(tree[tree[cur].ch[0]].lt,tree[tree[cur].ch[1]].lt);
tree[cur].rt=min(tree[tree[cur].ch[0]].rt,tree[tree[cur].ch[1]].rt);
return cur;
}
pair<ll,int> get(int cur, int x, int l=1, int r=n){
if(r<=x){
int pos=sorted[x].se;
return {tree[cur].lt.fi + town[pos].fi+town[pos].se, tree[cur].lt.se};
}
if(l>=x){
int pos=sorted[x].se;
return {tree[cur].rt.fi + town[pos].fi-town[pos].se, tree[cur].rt.se};
}
int mid=(l+r)>>1;
return min(get(tree[cur].ch[0],x,l,mid), get(tree[cur].ch[1],x,mid+1,r));
}
int root[N];
pair<ll,int> mi[N];
void solve(){
cin>>n>>k;
foru(i,1,n) cin>>town[i].fi>>town[i].se;
sort(town+1, town+1+n);
foru(i,1,n) sorted[i]={town[i].se,i};
sort(sorted+1, sorted+1+n);
foru(i,1,n) idSe[sorted[i].se]=i;
root[1]=1;
foru(i,2,n){
root[i]=upd(root[i-1],idSe[i-1],1);
}
priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> pq;
foru(i,1,n){
mi[i]=get(root[i],idSe[i]);
pq.push({mi[i].fi,i});
}
while(k>0){
auto [val,u]=pq.top(); pq.pop();
cout<<val<<'\n';
root[u]=upd(root[u],mi[u].se,0);
mi[u]=get(root[u],idSe[u]);
pq.push({mi[u].fi,u});
--k;
}
}
int32_t main(){
#define task "test"
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin.tie(0)->sync_with_stdio(0);
int tc=1; //cin>>tc;
foru(i,1,tc){
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
road_construction.cpp: In function 'int32_t main()':
road_construction.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
road_construction.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
122 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |