#include <set>
#include <cmath>
#include <algorithm>
using namespace std;
uint64_t seed = 123456789;
uint64_t xorshift64() {
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 7;
return seed;
}
int rand_int(int l, int r) {
return l + int(xorshift64() % (r - l));
}
int inv_binomial(int N, double p, double alpha) {
// least N such that P(x <= N) >= alpha
const double log_p1 = log(p);
const double log_p2 = log(1.0 - p);
double sum = 0.0;
double logval = log_p2 * N;
for (int i = 0; i < N; i++) {
sum += exp(logval);
if (sum >= alpha) {
return i;
}
logval += log_p1 - log_p2 + log(double(N - i) / (i + 1));
}
return N;
}
int X[262144], Y[262144], A[262144], B[262144], perm[262144], permtmp[262144], cnt[66560];
unsigned dist[3014656], res[262144], restmp[262144];
int readint() {
char ch = getchar_unlocked();
bool minus = false;
if (ch == '-') {
minus = true;
ch = getchar_unlocked();
}
int res = 0;
while ('0' <= ch && ch <= '9') {
res = res * 10 + int(ch - '0');
ch = getchar_unlocked();
}
return (minus ? -res : res);
}
char buf[64];
void writeint(unsigned x) {
int pos = 0;
while (x > 0) {
buf[pos++] = x % 10 + '0';
x /= 10;
}
while (pos--) {
putchar_unlocked(buf[pos]);
}
putchar_unlocked('\n');
}
int main() {
// step #1. read input
int N = readint();
int K = readint();
for (int i = 0; i < N; i++) {
X[i] = readint();
Y[i] = readint();
}
// step #2. random sampling setup
const double p = double(K) / (1LL * N * (N - 1) / 2);
const int samples = 3000000;
const int rank = (p != 1.0 ? inv_binomial(samples, p, 0.95) : samples);
// step #3. random sampling
long long border;
if (rank == samples) {
border = (1LL << 32);
}
else {
for (int i = 0; i < samples; i++) {
int va = rand_int(0, N);
int vb = (va + rand_int(1, N)) % N;
dist[i] = abs(X[va] - X[vb]) + abs(Y[va] - Y[vb]);
}
nth_element(dist, dist + rank, dist + samples);
border = dist[rank] - 1;
}
// step #4. rotate 45 degrees
vector<int> A(N), B(N);
for (int i = 0; i < N; i++) {
A[i] = X[i] + Y[i];
B[i] = X[i] - Y[i];
}
// step #5. sort by A[i]
for (int i = 0; i < N; i++) cnt[(A[i] & 65535) + 1]++;
for (int i = 0; i < 65536; i++) cnt[i + 1] += cnt[i];
for (int i = 0; i < N; i++) permtmp[cnt[A[i] & 65535]++] = i;
for (int i = 0; i < 65537; i++) cnt[i] = 0;
for (int i = 0; i < N; i++) cnt[(A[i] >> 16) + 32769]++;
for (int i = 0; i < 65536; i++) cnt[i + 1] += cnt[i];
for (int i = 0; i < N; i++) perm[cnt[(A[permtmp[i]] >> 16) + 32768]++] = permtmp[i];
// step #6. sweepline algorithm
int C = 0, pl = 0;
set<pair<int, int> > s;
for (int i = 0; i < N; i++) {
int u = perm[i];
int BL = max(B[u] - border, -2000000000LL);
int BR = min(B[u] + border, +2000000000LL);
while (A[u] - A[perm[pl]] > border) {
s.erase(make_pair(B[perm[pl]], perm[pl]));
pl++;
}
set<pair<int, int> >::iterator it = s.lower_bound(make_pair(BL, -1));
while (it != s.end() && it->first <= BR) {
res[C++] = abs(X[it->second] - X[u]) + abs(Y[it->second] - Y[u]);
it++;
}
s.insert(make_pair(B[u], u));
}
// step #7. sort the distance
for (int i = 0; i < 65537; i++) cnt[i] = 0;
for (int i = 0; i < C; i++) cnt[(res[i] & 65535) + 1]++;
for (int i = 0; i < 65536; i++) cnt[i + 1] += cnt[i];
for (int i = 0; i < C; i++) restmp[cnt[res[i] & 65535]++] = res[i];
for (int i = 0; i < 65537; i++) cnt[i] = 0;
for (int i = 0; i < C; i++) cnt[(res[i] >> 16) + 1]++;
for (int i = 0; i < 65536; i++) cnt[i + 1] += cnt[i];
for (int i = 0; i < C; i++) res[cnt[res[i] >> 16]++] = restmp[i];
// step #8. final answer
for (int i = 0; i < K; i++) {
writeint(i < C ? res[i] : border + 1);
}
return 0;
}
Compilation message
road_construction.cpp:6:1: error: 'uint64_t' does not name a type; did you mean 'u_int64_t'?
6 | uint64_t seed = 123456789;
| ^~~~~~~~
| u_int64_t
road_construction.cpp:7:1: error: 'uint64_t' does not name a type; did you mean 'u_int64_t'?
7 | uint64_t xorshift64() {
| ^~~~~~~~
| u_int64_t
road_construction.cpp: In function 'int rand_int(int, int)':
road_construction.cpp:14:17: error: 'xorshift64' was not declared in this scope; did you mean 'coshf64'?
14 | return l + int(xorshift64() % (r - l));
| ^~~~~~~~~~
| coshf64
road_construction.cpp: In function 'int readint()':
road_construction.cpp:36:12: error: 'getchar_unlocked' was not declared in this scope
36 | char ch = getchar_unlocked();
| ^~~~~~~~~~~~~~~~
road_construction.cpp: In function 'void writeint(unsigned int)':
road_construction.cpp:58:3: error: 'putchar_unlocked' was not declared in this scope
58 | putchar_unlocked(buf[pos]);
| ^~~~~~~~~~~~~~~~
road_construction.cpp:60:2: error: 'putchar_unlocked' was not declared in this scope
60 | putchar_unlocked('\n');
| ^~~~~~~~~~~~~~~~
road_construction.cpp: In function 'int main()':
road_construction.cpp:93:2: error: 'vector' was not declared in this scope
93 | vector<int> A(N), B(N);
| ^~~~~~
road_construction.cpp:4:1: note: 'std::vector' is defined in header '<vector>'; did you forget to '#include <vector>'?
3 | #include <algorithm>
+++ |+#include <vector>
4 | using namespace std;
road_construction.cpp:93:9: error: expected primary-expression before 'int'
93 | vector<int> A(N), B(N);
| ^~~