KD-tree
**了这道题
这个估价函数好鬼畜,把min打成max。。。
关于min的估价函数非常鬼畜,具体我也不知道为什么。
#include<bits/stdc++.h> using namespace std; const int N = 5e5 + 5; int n, d, root, ans = 2e9, mx, mn; struct data { int x, y, mxx, mnx, mny, mxy, lc, rc; bool friend operator < (const data &a, const data &b) { if(d == 0) return a.x == b.x ? a.y < b.y : a.x < b.x; if(d == 1) return a.y == b.y ? a.x < b.x : a.y < b.y; } } a[N]; void update(int x) { a[x].mnx = min(a[x].x, min(a[a[x].lc].mnx, a[a[x].rc].mnx)); a[x].mxx = max(a[x].x, max(a[a[x].lc].mxx, a[a[x].rc].mxx)); a[x].mny = min(a[x].y, min(a[a[x].lc].mny, a[a[x].rc].mny)); a[x].mxy = max(a[x].y, max(a[a[x].lc].mxy, a[a[x].rc].mxy)); } int build(int l, int r, int D) { if(l > r) return 0; int mid = (l + r) >> 1; nth_element(a + l, a + mid, a + r + 1); a[mid].mnx = a[mid].mxx = a[mid].x; a[mid].mny = a[mid].mxy = a[mid].y; a[mid].lc = build(l, mid - 1, D ^ 1); a[mid].rc = build(mid + 1, r, D ^ 1); update(mid); return mid; } int query_mn(int k, int x, int y) { int d = abs(a[k].x - x) + abs(a[k].y - y), dl = a[k].lc ? max(a[a[k].lc].mnx - x, 0) + max(x - a[a[k].lc].mxx, 0) + max(a[a[k].lc].mny - y, 0) + max(y - a[a[k].lc].mxy, 0) : 5e8, dr = a[k].rc ? max(a[a[k].rc].mnx - x, 0) + max(x - a[a[k].rc].mxx, 0) + max(a[a[k].rc].mny - y, 0) + max(y - a[a[k].rc].mxy, 0) : 5e8; if(d) mn = min(mn, d); if(dl < dr) { if(mn > dl) query_mn(a[k].lc, x, y); if(mn > dr) query_mn(a[k].rc, x, y); } else { if(mn > dr) query_mn(a[k].rc, x, y); if(mn > dl) query_mn(a[k].lc, x, y); } } int query_mx(int k, int x, int y) { int d = abs(a[k].x - x) + abs(a[k].y - y), dl = a[k].lc ? max(abs(a[a[k].lc].mnx - x), abs(a[a[k].lc].mxx - x)) + max(abs(a[a[k].lc].mny - y), abs(a[a[k].lc].mxy - y)) : -5e8, dr = a[k].rc ? max(abs(a[a[k].rc].mnx - x), abs(a[a[k].rc].mxx - x)) + max(abs(a[a[k].rc].mny - y), abs(a[a[k].rc].mxy - y)) : -5e8; if(d) mx = max(mx, d); if(dl > dr) { if(mx < dl) query_mx(a[k].lc, x, y); if(mx < dr) query_mx(a[k].rc, x, y); } else { if(mx < dr) query_mx(a[k].rc, x, y); if(mx < dl) query_mx(a[k].lc, x, y); } } int main() { a[0].mnx = a[0].mny = 5e8; a[0].mxx = a[0].mxy = -5e8; scanf("%d", &n); for(int i = 1; i <= n; ++i) scanf("%d%d", &a[i].x, &a[i].y); root = build(1, n, 0); for(int i = 1; i <= n; ++i) { mx = -5e8; mn = 5e8; query_mn(root, a[i].x, a[i].y); query_mx(root, a[i].x, a[i].y); ans = min(ans, mx - mn); } printf("%d ", ans); return 0; }