#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
struct Persist{
int seg[10000000],lc[10000000],rc[10000000];
ll ptr=1;
ll build(ll l, ll r){
if(l==r){
seg[ptr]=1e18,lc[ptr]=rc[ptr]=-1; ptr++; return ptr-1;
}
else{
ll mid=(l+r)>>1;
ll curptr=ptr; ptr++;
lc[curptr]=build(l,mid); rc[curptr]=build(mid+1,r);
seg[curptr]=1e18;
return curptr;
}
}
ll upd(ll ul, ll l, ll r, ll val, ll id){
if(l==r){
seg[ptr]=val; lc[ptr]=rc[ptr]=-1; ptr++; return ptr-1;
}
else{
ll mid=(l+r)>>1;
ll curp=ptr; ptr++;
if(ul<=mid){
lc[curp]=upd(ul,l,mid,val,lc[id]); rc[curp]=rc[id];
}
else{
rc[curp]=upd(ul,mid+1,r,val,rc[id]); lc[curp]=lc[id];
}
seg[curp]=min(seg[lc[curp]],seg[rc[curp]]);
return curp;
}
}
ll qry(ll ql, ll qr, ll l, ll r, ll id){
if(ql>qr)return 1e18;
if(l>qr||r<ql)return 1e18;
if(l>=ql&&r<=qr){
return seg[id];
}
ll mid=(l+r)>>1;
return min(qry(ql,qr,l,mid,lc[id]),qry(ql,qr,mid+1,r,rc[id]));
}
ll bs(ll l, ll r, ll k, ll id){
if(l==r)return l;
ll mid=(l+r)>>1;
if(seg[lc[id]]==k)return bs(l,mid,k,lc[id]);
return bs(mid+1,r,k,rc[id]);
}
ll find(ll ql, ll qr, ll l, ll r, ll id, ll k){
if(ql>qr)return -1;
if(l>qr||r<ql)return -1;
if(l>=ql&&r<=qr){
if(seg[id]!=k) return -1;
return bs(l,r,k,id);
}
ll mid=(l+r)>>1;
ll bruh=find(ql,qr,l,mid,lc[id],k);
if(bruh!=-1)return bruh;
bruh=find(ql,qr,mid+1,r,rc[id],k);
return bruh;
}
}st,st2;
struct THING{
ll val,verx,very,insegpos;
bool operator<(const THING& th)const{
return val>th.val;
}
};
priority_queue<THING>pq;
int version[250005][3];
int prevver,prevver2;
vector<pair<ll,ll> >disc;
ll pos(pair<ll,ll>x){
return lower_bound(disc.begin(),disc.end(),x)-disc.begin()+1;
}
int32_t main(){
ll n,k;
cin>>n>>k;
ll x[n+5],y[n+5];
for(int i=1;i<=n;i++)cin>>x[i]>>y[i];
vector<pair<ll,ll> >pts;
for(int i=1;i<=n;i++)pts.push_back({x[i],y[i]});
sort(pts.begin(),pts.end());
for(int i=0;i<pts.size();i++){
disc.push_back({pts[i].second,i});
}
sort(disc.begin(),disc.end());
prevver=st.build(1,disc.size()); prevver2=st2.build(1,disc.size());
for(int i=0;i<pts.size();i++){
int xx=pts[i].first,yy=pts[i].second;
version[i][1]=prevver; version[i][2]=prevver2;
prevver=st.upd(pos(make_pair(yy,i)),1,disc.size(),-xx-yy,prevver);
prevver2=st2.upd(pos(make_pair(yy,i)),1,disc.size(),-xx+yy,prevver2);
}
for(int i=0;i<pts.size();i++){
int xx=pts[i].first,yy=pts[i].second;
ll posi=pos(make_pair(yy,i));
ll for1=st.qry(1,posi,1,disc.size(),version[i][1]);
ll segtreebst=st.find(1,posi,1,disc.size(),version[i][1],for1);
pq.push({for1+xx+yy,i,1,segtreebst});
ll for2=st2.qry(posi+1,disc.size(),1,disc.size(),version[i][2]);
segtreebst=st2.find(posi+1,disc.size(),1,disc.size(),version[i][2],for2);
pq.push({for2+xx-yy,i,2,segtreebst});
}
for(int i=1;i<=k;i++){
THING lol=pq.top();
ll val=lol.val,verx=lol.verx,very=lol.very,insegpos=lol.insegpos;
ll xx=pts[verx].first,yy=pts[verx].second;
pq.pop();
cout<<val<<'\n';
ll posi=pos(make_pair(yy,verx));
if(very==1){
version[verx][very]=st.upd(insegpos,1,disc.size(),1e18,version[verx][very]);
ll for1=st.qry(1,posi,1,disc.size(),version[verx][1]);
ll segtreebst=st.find(1,posi,1,disc.size(),version[verx][1],for1);
pq.push({for1+xx+yy,verx,1,segtreebst});
}
else{
version[verx][very]=st2.upd(insegpos,1,disc.size(),1e18,version[verx][very]);
ll for2=st2.qry(posi+1,disc.size(),1,disc.size(),version[verx][2]);
ll segtreebst=st2.find(posi+1,disc.size(),1,disc.size(),version[verx][2],for2);
pq.push({for2+xx-yy,verx,2,segtreebst});
}
}
}
Compilation message
road_construction.cpp: In function 'int32_t main()':
road_construction.cpp:88:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for(int i=0;i<pts.size();i++){
| ~^~~~~~~~~~~
road_construction.cpp:93:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for(int i=0;i<pts.size();i++){
| ~^~~~~~~~~~~
road_construction.cpp:99:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for(int i=0;i<pts.size();i++){
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
193 ms |
80968 KB |
Output is correct |
2 |
Correct |
188 ms |
81088 KB |
Output is correct |
3 |
Correct |
181 ms |
77112 KB |
Output is correct |
4 |
Correct |
152 ms |
77128 KB |
Output is correct |
5 |
Correct |
179 ms |
79944 KB |
Output is correct |
6 |
Correct |
4 ms |
14928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
958 ms |
395580 KB |
Output is correct |
2 |
Correct |
938 ms |
398592 KB |
Output is correct |
3 |
Correct |
117 ms |
76872 KB |
Output is correct |
4 |
Correct |
627 ms |
398220 KB |
Output is correct |
5 |
Correct |
692 ms |
398452 KB |
Output is correct |
6 |
Correct |
672 ms |
398464 KB |
Output is correct |
7 |
Correct |
738 ms |
397956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1162 ms |
285300 KB |
Output is correct |
2 |
Correct |
1285 ms |
285336 KB |
Output is correct |
3 |
Correct |
2 ms |
12624 KB |
Output is correct |
4 |
Correct |
395 ms |
285336 KB |
Output is correct |
5 |
Correct |
723 ms |
285336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1162 ms |
285300 KB |
Output is correct |
2 |
Correct |
1285 ms |
285336 KB |
Output is correct |
3 |
Correct |
2 ms |
12624 KB |
Output is correct |
4 |
Correct |
395 ms |
285336 KB |
Output is correct |
5 |
Correct |
723 ms |
285336 KB |
Output is correct |
6 |
Correct |
1280 ms |
285168 KB |
Output is correct |
7 |
Correct |
1269 ms |
285264 KB |
Output is correct |
8 |
Correct |
2 ms |
12624 KB |
Output is correct |
9 |
Correct |
2 ms |
12624 KB |
Output is correct |
10 |
Correct |
1234 ms |
285336 KB |
Output is correct |
11 |
Correct |
386 ms |
285284 KB |
Output is correct |
12 |
Correct |
725 ms |
285344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
193 ms |
80968 KB |
Output is correct |
2 |
Correct |
188 ms |
81088 KB |
Output is correct |
3 |
Correct |
181 ms |
77112 KB |
Output is correct |
4 |
Correct |
152 ms |
77128 KB |
Output is correct |
5 |
Correct |
179 ms |
79944 KB |
Output is correct |
6 |
Correct |
4 ms |
14928 KB |
Output is correct |
7 |
Correct |
1128 ms |
223016 KB |
Output is correct |
8 |
Correct |
1204 ms |
222908 KB |
Output is correct |
9 |
Correct |
165 ms |
75080 KB |
Output is correct |
10 |
Correct |
639 ms |
224236 KB |
Output is correct |
11 |
Correct |
707 ms |
222128 KB |
Output is correct |
12 |
Correct |
530 ms |
222800 KB |
Output is correct |
13 |
Correct |
667 ms |
221360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
193 ms |
80968 KB |
Output is correct |
2 |
Correct |
188 ms |
81088 KB |
Output is correct |
3 |
Correct |
181 ms |
77112 KB |
Output is correct |
4 |
Correct |
152 ms |
77128 KB |
Output is correct |
5 |
Correct |
179 ms |
79944 KB |
Output is correct |
6 |
Correct |
4 ms |
14928 KB |
Output is correct |
7 |
Correct |
958 ms |
395580 KB |
Output is correct |
8 |
Correct |
938 ms |
398592 KB |
Output is correct |
9 |
Correct |
117 ms |
76872 KB |
Output is correct |
10 |
Correct |
627 ms |
398220 KB |
Output is correct |
11 |
Correct |
692 ms |
398452 KB |
Output is correct |
12 |
Correct |
672 ms |
398464 KB |
Output is correct |
13 |
Correct |
738 ms |
397956 KB |
Output is correct |
14 |
Correct |
1162 ms |
285300 KB |
Output is correct |
15 |
Correct |
1285 ms |
285336 KB |
Output is correct |
16 |
Correct |
2 ms |
12624 KB |
Output is correct |
17 |
Correct |
395 ms |
285336 KB |
Output is correct |
18 |
Correct |
723 ms |
285336 KB |
Output is correct |
19 |
Correct |
1280 ms |
285168 KB |
Output is correct |
20 |
Correct |
1269 ms |
285264 KB |
Output is correct |
21 |
Correct |
2 ms |
12624 KB |
Output is correct |
22 |
Correct |
2 ms |
12624 KB |
Output is correct |
23 |
Correct |
1234 ms |
285336 KB |
Output is correct |
24 |
Correct |
386 ms |
285284 KB |
Output is correct |
25 |
Correct |
725 ms |
285344 KB |
Output is correct |
26 |
Correct |
1128 ms |
223016 KB |
Output is correct |
27 |
Correct |
1204 ms |
222908 KB |
Output is correct |
28 |
Correct |
165 ms |
75080 KB |
Output is correct |
29 |
Correct |
639 ms |
224236 KB |
Output is correct |
30 |
Correct |
707 ms |
222128 KB |
Output is correct |
31 |
Correct |
530 ms |
222800 KB |
Output is correct |
32 |
Correct |
667 ms |
221360 KB |
Output is correct |
33 |
Correct |
2275 ms |
403568 KB |
Output is correct |
34 |
Correct |
2331 ms |
402832 KB |
Output is correct |
35 |
Correct |
1559 ms |
402100 KB |
Output is correct |
36 |
Correct |
1100 ms |
402976 KB |
Output is correct |
37 |
Correct |
1108 ms |
402744 KB |
Output is correct |
38 |
Correct |
1225 ms |
401632 KB |
Output is correct |