#include <bits/stdc++.h>
#define ll long long
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define vll vector<ll>
#define bc back()
#define arr array
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class _T>
bool chmin(_T &x, const _T &y){
if (x>y)x=y;
return x>y;
}
template <class _T>
bool chmax(_T &x, const _T &y){
if (x<y)x=y;
return x<y;
}
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
void start(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const ll inf=1e18+7;
const ll mod=1e9+7;
const ll N=2e5+5;
const ld eps=1e-9;
vector<pair<ll,ll>> v;
multiset<ll> ms;
ll n,k;
ll i,j,mn=inf;
map<pair<pair<ll,ll>,pair<ll,ll>>,bool> vis;
ll dis(ll i,ll j){
return abs(v[i].fr-v[j].fr)+abs(v[i].sc-v[j].sc);
}
bool cmp(pair<ll,ll> a,pair<ll,ll> b){
return a.sc<b.sc;
}
void upd_ans(ll i,ll j){
if(dis(i,j)>=mn || vis[{v[i],v[j]}])return;
vis[{v[i],v[j]}]=true;
vis[{v[j],v[i]}]=true;
ms.ins(dis(i,j));
if((ll)ms.sz>k)ms.erase(--ms.end());
if((ll)ms.sz==k)chmin(mn,*ms.rbegin());
}
void rec(ll l,ll r){
if(r-l<=3){
for(i=l;i<r;i++){
for(j=i+1;j<=r;j++)upd_ans(i,j);
}
sort(v.begin()+l,v.begin()+r+1,cmp);
return ;
}
ll m=(l+r)/2;
ll mx=v[m].fr;
rec(l,m),rec(m+1,r);
vector<pair<ll,ll>> v2(r-l+1);
merge(v.begin()+l,v.begin()+m+1,v.begin()+m+1,v.begin()+r+1,v2.begin(),cmp);
copy(v2.begin(),v2.begin()+r-l+1,v.begin()+l);
vll vv;
for(i=l;i<=r;i++){
if(mn<=abs(v[i].fr-mx))continue;
for(j=vv.sz-1;j>=0;j--){
if(mn<=abs(v[i].sc-v[vv[j]].sc))break;
upd_ans(i,vv[j]);
}
vv.pb(i);
}
}
void solve(){
ll i,j;
ll a,b;
cin>>n>>k;
for(i=0;i<n;i++){
cin>>a>>b;
v.pb({a,b});
}
sort(all(v));
rec(0,n-1);
for(auto i : ms) cout<<i<<endl;
}
signed main(){
start();
ll t=1;
//cin>>t;
while(t--) solve();
return 0;
}
/*
*/
Compilation message
road_construction.cpp: In function 'void solve()':
road_construction.cpp:90:7: warning: unused variable 'j' [-Wunused-variable]
90 | ll i,j;
| ^
road_construction.cpp: In function 'void fre(std::string)':
road_construction.cpp:36:27: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
road_construction.cpp:36:64: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
705 ms |
70112 KB |
Output is correct |
2 |
Correct |
719 ms |
69572 KB |
Output is correct |
3 |
Correct |
503 ms |
53844 KB |
Output is correct |
4 |
Correct |
463 ms |
53840 KB |
Output is correct |
5 |
Correct |
700 ms |
73332 KB |
Output is correct |
6 |
Correct |
5 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2821 ms |
282860 KB |
Output is correct |
2 |
Correct |
2720 ms |
281788 KB |
Output is correct |
3 |
Correct |
354 ms |
53844 KB |
Output is correct |
4 |
Correct |
2375 ms |
307852 KB |
Output is correct |
5 |
Correct |
1733 ms |
266356 KB |
Output is correct |
6 |
Correct |
1769 ms |
266636 KB |
Output is correct |
7 |
Correct |
1862 ms |
271992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
10236 KB |
Output is correct |
2 |
Correct |
109 ms |
10240 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
77 ms |
10204 KB |
Output is correct |
5 |
Correct |
96 ms |
10068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
10236 KB |
Output is correct |
2 |
Correct |
109 ms |
10240 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
77 ms |
10204 KB |
Output is correct |
5 |
Correct |
96 ms |
10068 KB |
Output is correct |
6 |
Correct |
122 ms |
10192 KB |
Output is correct |
7 |
Correct |
112 ms |
10128 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
113 ms |
10176 KB |
Output is correct |
11 |
Correct |
80 ms |
10160 KB |
Output is correct |
12 |
Correct |
96 ms |
10128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
705 ms |
70112 KB |
Output is correct |
2 |
Correct |
719 ms |
69572 KB |
Output is correct |
3 |
Correct |
503 ms |
53844 KB |
Output is correct |
4 |
Correct |
463 ms |
53840 KB |
Output is correct |
5 |
Correct |
700 ms |
73332 KB |
Output is correct |
6 |
Correct |
5 ms |
1372 KB |
Output is correct |
7 |
Correct |
3995 ms |
305656 KB |
Output is correct |
8 |
Correct |
3898 ms |
305812 KB |
Output is correct |
9 |
Correct |
448 ms |
53844 KB |
Output is correct |
10 |
Correct |
4572 ms |
306748 KB |
Output is correct |
11 |
Correct |
5551 ms |
389148 KB |
Output is correct |
12 |
Correct |
1644 ms |
204448 KB |
Output is correct |
13 |
Correct |
2547 ms |
285172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
705 ms |
70112 KB |
Output is correct |
2 |
Correct |
719 ms |
69572 KB |
Output is correct |
3 |
Correct |
503 ms |
53844 KB |
Output is correct |
4 |
Correct |
463 ms |
53840 KB |
Output is correct |
5 |
Correct |
700 ms |
73332 KB |
Output is correct |
6 |
Correct |
5 ms |
1372 KB |
Output is correct |
7 |
Correct |
2821 ms |
282860 KB |
Output is correct |
8 |
Correct |
2720 ms |
281788 KB |
Output is correct |
9 |
Correct |
354 ms |
53844 KB |
Output is correct |
10 |
Correct |
2375 ms |
307852 KB |
Output is correct |
11 |
Correct |
1733 ms |
266356 KB |
Output is correct |
12 |
Correct |
1769 ms |
266636 KB |
Output is correct |
13 |
Correct |
1862 ms |
271992 KB |
Output is correct |
14 |
Correct |
109 ms |
10236 KB |
Output is correct |
15 |
Correct |
109 ms |
10240 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
77 ms |
10204 KB |
Output is correct |
18 |
Correct |
96 ms |
10068 KB |
Output is correct |
19 |
Correct |
122 ms |
10192 KB |
Output is correct |
20 |
Correct |
112 ms |
10128 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
113 ms |
10176 KB |
Output is correct |
24 |
Correct |
80 ms |
10160 KB |
Output is correct |
25 |
Correct |
96 ms |
10128 KB |
Output is correct |
26 |
Correct |
3995 ms |
305656 KB |
Output is correct |
27 |
Correct |
3898 ms |
305812 KB |
Output is correct |
28 |
Correct |
448 ms |
53844 KB |
Output is correct |
29 |
Correct |
4572 ms |
306748 KB |
Output is correct |
30 |
Correct |
5551 ms |
389148 KB |
Output is correct |
31 |
Correct |
1644 ms |
204448 KB |
Output is correct |
32 |
Correct |
2547 ms |
285172 KB |
Output is correct |
33 |
Correct |
4785 ms |
359908 KB |
Output is correct |
34 |
Correct |
4735 ms |
360568 KB |
Output is correct |
35 |
Correct |
5037 ms |
384540 KB |
Output is correct |
36 |
Correct |
1684 ms |
213144 KB |
Output is correct |
37 |
Correct |
1714 ms |
212588 KB |
Output is correct |
38 |
Correct |
2863 ms |
328972 KB |
Output is correct |