This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ld long double
#define pii pair<int, int>
#define f first
#define s second
#define boost() cin.tie(0), cin.sync_with_stdio(0)
const int MN = 750005;
int n, k, x, y, bit[MN];
pii a[MN];
void upd(int x, int val) {
for (int i = x; i < MN; i += i & -i) bit[i] += val;
}
int qry(int x) {
int ret = 0;
for (int i = x; i > 0; i -= i & -i) ret += bit[i];
return ret;
}
int cost(pii p, pii q) {
return max(abs(p.f - q.f), abs(p.s - q.s));
}
int get(vector<int> &v, int num) {
int lo = 0, hi = v.size() - 1, mid;
while (lo <= hi) {
mid = (lo + hi) >> 1;
if (v[mid] == num) return mid + 1;
else if (v[mid] < num) lo = mid + 1;
else hi = mid - 1;
}
abort();
return -1;
}
int count(int d) {
int ret = 0;
memset(bit, 0, sizeof(bit));
vector<int> ys;
for (int i = 1; i <= n; i++) {
ys.push_back(a[i].s);
ys.push_back(a[i].s - d);
ys.push_back(a[i].s + d);
}
sort(ys.begin(), ys.end());
ys.erase(unique(ys.begin(), ys.end()), ys.end());
int id = 1;
for (int i = 1; i <= n; i++) {
while (a[i].f - a[id].f > d) upd(get(ys, a[id].s), -1), id++;
ret += qry(get(ys, a[i].s + d)) - qry(get(ys, a[i].s - d) - 1);
//printf("lol %lld %lld\n", mp[a[i].s + d], mp[a[i].s - d]);
if (ret >= k) return ret;
upd(get(ys, a[i].s), 1);
}
return ret;
}
void get_ans(int d) {
vector<int> out;
set<pii> s;
vector<int> ys;
for (int i = 1; i <= n; i++) {
ys.push_back(a[i].s);
ys.push_back(a[i].s - d);
ys.push_back(a[i].s + d);
}
sort(ys.begin(), ys.end());
ys.erase(unique(ys.begin(), ys.end()), ys.end());
int id = 1;
for (int i = 1; i <= n; i++) {
while (a[i].f - a[id].f > d) s.erase({get(ys, a[id].s), id}), id++;
auto it = s.lower_bound({get(ys, a[i].s - d), 0});
int mx = get(ys, a[i].s + d);
while (1) {
if (it == s.end()) break;
pii cur = *it;
if (cur.f > mx) break;
out.push_back(cost(a[i], a[cur.s]));
it++;
}
s.insert({get(ys, a[i].s), i});
}
sort(out.begin(), out.end());
for (int i = 0; i < k; i++) printf("%lld\n", out[i]);
}
int32_t main() {
boost();
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> x >> y;
a[i] = {x + y, x - y};
//printf("%lld %lld\n", a[i].f, a[i].s);
}
sort(a + 1, a + n + 1);
int lo = 1, hi = 1e10, mid;
while (lo < hi) {
mid = (lo + hi) >> 1;
if (count(mid) >= k) hi = mid;
else lo = mid + 1;
}
//printf("cnt %lld\n", count(2));
//printf("%lld\n", lo);
get_ans(lo);
return 0;
}
# | 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... |