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>
using namespace std;
#define int long long
struct node{
int s, e, m;
int v;
node *l = nullptr, *r = nullptr;
node(int _s, int _e){
s = _s, e = _e, m = s + (e-s)/2;
v = 0;
}
void del(){
if (l != nullptr) l->del();
if (r != nullptr) r->del();
delete this;
}
void make_left(){
if (s != e && l == nullptr) l = new node(s, m);
}
void make_right(){
if (s != e && r == nullptr) r = new node(m+1, e);
}
int query(int qs, int qe){
if (qs == s && e == qe) return v;
if (qe <= m) {make_left(); return l->query(qs, qe);}
else if (qs > m) {make_right(); return r->query(qs, qe);}
else {make_left(); make_right(); return l->query(qs, m) + r->query(m+1, qe);}
}
void update(int x, int k){
if (s == e) {v += k; return;}
if (x <= m) {make_left(); l->update(x, k);}
else {make_right(); r->update(x, k);}
v = 0;
if (l != nullptr) v += l->v;
if (r != nullptr) v += r->v;
}
};
main(){
int n, k; cin >> n >> k;
pair<int, int> arr[n];
for (int x = 0; x < n; x++){
cin >> arr[x].first >> arr[x].second;
}
//consider manhattan distance trick?
for (int x = 0; x < n; x++){
arr[x] = {arr[x].first + arr[x].second, arr[x].first - arr[x].second};
}
sort(arr, arr+n); //sorted by x-coord
//now chebyshev distance, dist = max of difference
//I want to bsearch, but bsearch only gives location of boundary; I need sum of boundary
int l = 0, r = 4'000'000'000LL, ans = 4'000'000'000LL;
while (l <= r){
int m = (l+r)/2;
int cnt = 0;
node *seg = new node(-8'000'000'007LL, 8'000'000'007LL);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
for (int x = 0; x < n; x++){
while (!pq.empty() && pq.top().first <= arr[x].first){
seg->update(arr[ pq.top().second ].second, -1);
pq.pop();
}
cnt += seg->query(arr[x].second - m, arr[x].second + m);
seg->update(arr[x].second, 1);
pq.push({arr[x].first+m+1, x});
}
if (cnt < k){
l = m+1;
}
else{
ans = m;
r = m-1;
}
seg->del();
}
vector<int> clown;
for (int x = 0; x < n; x++){
for (int y = x+1; y <= min(x+k+2, n-1); y++){
clown.push_back( max( abs(arr[x].first - arr[y].first), abs(arr[x].second - arr[y].second) ) );
}
}
sort(clown.begin(), clown.end());
for (int x = 0; x < k; x++) cout << clown[x] << '\n';
assert(clown[k-1] == ans);
}
Compilation message (stderr)
road_construction.cpp:52:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
52 | main(){
| ^~~~
# | 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... |