1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
#include <bits/stdc++.h>
namespace IO {
inline char read() { static const int IN_LEN = 1000000; static char buf[IN_LEN], *s, *t; s == t ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin) : 0; return s == t ? -1 : *s++; }
template<class T> inline void read(T &x) { static char c; static bool iosig; for (c = read(), iosig = false; !isdigit(c); c = read()) { if (c == -1) return; c == '-' ? iosig = true : 0; } for (x = 0; isdigit(c); c = read()) x = (x + (x << 2) << 1) + (c ^ '0'); iosig ? x = -x : 0; }
const int OUT_LEN = 1000000;
char obuf[OUT_LEN], *oh = obuf;
inline void print(char c) { oh == obuf + OUT_LEN ? (fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf) : 0; *oh++ = c; }
template<class T> inline void print(T x) { static int buf[30], cnt; if (x == 0) { print('0'); } else { x < 0 ? (print('-'), x = -x) : 0; for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 | 48; while (cnt) print((char)buf[cnt--]); } }
inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); } }
static const int MAXN = 100010;
static struct Point { int x, y;
Point(int x = 0, int y = 0) : x(x), y(y) {} } p[MAXN];
bool flag;
#define long long long
template<typename T> static inline T square(const T &x) { return x * x; }
static inline bool cmp(const Point &p1, const Point &p2) { return flag ? (p1.y < p2.y || (p1.y == p2.y && p1.x < p2.x)) : (p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y)); }
static inline bool getSplit(int l, int r) { register double vx = 0, vy = 0; register double ax = 0, ay = 0; for (register int i = l; i <= r; i++) ax += p[i].x, ay += p[i].y; ax /= r - l + 1, ay /= r - l + 1; for (register int i = l; i <= r; i++) vx += square(p[i].x - ax), vy += square(p[i].y - ay); return vx < vy; }
class PriorityQueue : public std::priority_queue<long, std::vector<long>, std::greater<long> > { public: inline void resize(const int n) { c.resize(n); } } a;
PriorityQueue q;
struct Node { Node *c[2]; int kind; Point p, r1, r2;
Node(int kind = 0) : kind(0), c() {}
Node(int kind, const Point &p) : kind(kind), p(p), r1(p), r2(p), c() {}
inline void *operator new(size_t) { static Node pool[MAXN], *cur = pool; return cur++; }
inline void maintain() { if (c[0]) { r1.x = std::min(c[0]->r1.x, r1.x); r1.y = std::min(c[0]->r1.y, r1.y); r2.x = std::max(c[0]->r2.x, r2.x); r2.y = std::max(c[0]->r2.y, r2.y); } if (c[1]) { r1.x = std::min(c[1]->r1.x, r1.x); r1.y = std::min(c[1]->r1.y, r1.y); r2.x = std::max(c[1]->r2.x, r2.x); r2.y = std::max(c[1]->r2.y, r2.y); } }
inline long heuristic(const Point &p) { return std::max(square<long>(r1.x - p.x), square<long>(r2.x - p.x)) + std::max(square<long>(r1.y - p.y), square<long>(r2.y - p.y)); }
} *root;
inline Node *build(int l, int r) { if (l > r) return NULL; register int mid = l + r >> 1; flag = getSplit(l, r), std::nth_element(p + l, p + mid, p + r + 1, cmp); Node *o = new Node(flag, p[mid]); o->c[0] = build(l, mid - 1), o->c[1] = build(mid + 1, r); return o->maintain(), o; }
inline long dis(const Point &a, const Point &b) { return square<long>(a.x - b.x) + square<long>(a.y - b.y); }
int cur;
inline void query(Node *o) { register long d = dis(o->p, p[cur]); if (d > q.top()) q.pop(), q.push(d); register long l = (o->c[0] ? o->c[0]->heuristic(p[cur]) : 0); register long r = (o->c[1] ? o->c[1]->heuristic(p[cur]) : 0); if (l > r) { if (o->c[0] && l > q.top()) query(o->c[0]); if (o->c[1] && r > q.top()) query(o->c[1]); } else { if (o->c[1] && r > q.top()) query(o->c[1]); if (o->c[0] && l > q.top()) query(o->c[0]); } }
inline void query(const int pos) { cur = pos, query(root); }
#undef long
int main() { using namespace IO; register int n, k; read(n), read(k);
for (register int i = 1; i <= n; i++) read(p[i].x), read(p[i].y); root = build(1, n); q.resize(k * 2); for (register int i = 1; i <= n; i++) query(i); print(q.top()); flush(); return 0; }
|