[BZOJ1941] [Sdoi2010] Hide and Seek
Question Description
Piglet Ipig in PKU just finished bored pig sex algebra class, talent intelligent Ipig was this door to his very simple class made very lonely, in order to eliminate loneliness, he decided to play with his good friend Gipi (chicken skin) A more lonely game---hide and seek. But, they feel, play ordinary hide and seek no meaning, or is not lonely enough, so they decided to play the lonely and incomparable crab version of hide and Seek, as the name implies, that is, they play the game only along the horizontal or vertical direction. After a lonely pair of scissors and stone cloth, they decided to Ipig to catch Gipi. Because they are very familiar with the terrain of PKU, so Gipi will only hide in the PKU within the N secret location, obviously Ipig will only in that N locations to find Gipi. At the beginning of the game, they selected a location, Ipig remained motionless, and then Gipi fled the scene for 30 seconds (obviously, Gipi would not stay in place). Then Ipig will randomly go to find gipi until it is found. Because Ipig is very lazy, so he always take the shortest path, moreover, he chose the starting point is not random, he wanted to find a location, so that the location to the farthest point and the nearest location of the least distance difference. Ipig now wants to know what the minimum distance difference is. Because Ipig now has no computer on hand, so it can't be programmed to solve such a simple problem, so he immediately called and asked you to help him solve the problem. Ipig tells you the coordinates of PKU's n secret locations, and asks you to program Ipig problems.
Input
The first line enters an integer N 2~n+1 line, two integers per line x, y, representing the coordinates of the I-place
Output
The first line enters an integer N 2~n+1 line, two integers per line x, y, representing the coordinates of the I-place
Input example
4 0 0 1 0 0 1 1 1
Output example
1
Data size and conventions
For 30% of the data, n<=1000 for 100% of the data, n<=500000,0<=x,y<=10^8 ensure that the data does not focus on ensuring n>=2
Exercises
kd Tree template problem, for each point count the furthest and the nearest distance apart from it .
Since you want to "apart from it", then design a delete operation will delete the point hit a mark, after the query and then delete the tag.
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <stack > #include <vector> #include <queue> #include <cstring> #include <string> #include <map > #include <set>using namespace std; #define LL Long Long const int buffersize = 1 << 16;char buffer[buffersi Ze], *head, *tail;inline char Getchar () {if (Head = = Tail) {int L = fread (buffer, 1, buffersize, stdin); Tail = (Head = buffer) + L; } return *head++;} ll read () {ll x = 0, f = 1; char c = Getchar (); while (!isdigit (c)) {if (c = = '-') f =-1; c = Getchar ();} while (IsDigit (c)) {x = x * + C-' 0 '; c = Getchar ();} return x * f;} #define MAXN 500010#define oo 1047483647int N, ToT, LC[MAXN], RC[MAXN], NX[MAXN], NY[MAXN], Root;bool cur;struct Node {int X[2], mx[2], Mn[2];bool del;bool operator < (const node& t) const {return X[cur] = = T.x[cur]? x[cur^1] < t.x[ CUR^1]: X[cur] < t.x[cur]; }bool OperaTor = = (const node& t) const {return x[0] = = T.x[0] && x[1] = = t.x[1];} int operator * (const node& t) const {return abs (X[0]-t.x[0]) + ABS (X[1]-t.x[1]);}} Ns[maxn];void maintain (int o) {int l = Lc[o], r = rc[o];for (int i = 0; i < 2; i++) {Ns[o].mx[i] = max (max (ns[l].mx[i), Ns[r].mx[i]), Ns[o].del? -oo:ns[o].x[i]); Ns[o].mn[i] = min (min (ns[l].mn[i], ns[r].mn[i]), Ns[o].del oo:ns[o].x[i]);} return;} void Build (int& o, int L, int R, bool cur) {if (L > R) {o = 0; return;} int M = L + R >> 1; o = M; Cur = Cur; Nth_element (ns + L, NS + M, NS + R + 1), build (Lc[o], L, M-1, cur ^ 1); Build (Rc[o], M + 1, R, cur ^ 1); maintain (o); return;} Node x;void Remove (int o, bool cur) {cur = cur;if (!o) return, if (x = = Ns[o]) {Ns[o].del = 1; return maintain (o);} Remove (x < Ns[o]? Lc[o]: rc[o], cur ^ 1); return maintain (o);} void Insert (int o, bool cur) {cur = cur;if (!o) return;//if (x = = Ns[o]) printf ("%d:%d%d\n", O, Ns[o].x[0], ns[o].x[1]); I F (x = = Ns[o]) {NS[O].del = 0; return maintain (O); }insert (x < Ns[o]? Lc[o]: rc[o], cur ^ 1); return maintain (o);} int calcmx (int o) {return max (ABS (Ns[o].mx[0]-x.x[0]), ABS (Ns[o].mn[0]-x.x[0]) + MAX (ABS (Ns[o].mx[1]-x.x[1]), ABS ( NS[O].MN[1]-x.x[1]); }int querymx (int o) {if (!o) return-oo;int ans =-oo;if (!ns[o].del) ans = max (ans, Ns[o] * x); int dl = CALCMX (Lc[o]), Dr = CALCMX (Rc[o]); if (DL > DR) {if (dl > ans) ans = max (ans, querymx (Lc[o])), if (Dr > ans) ans = max (ans, querymx (rc[o) ));} else {if (Dr > ans) ans = max (ans, querymx (Rc[o])); if (dl > ans) ans = max (ans, querymx (Lc[o]));} return ans;} int calcmn (int o) {int ans = 0;for (int i = 0; i < 2; i++) {if (X.x[i] > Ns[o].mx[i]) ans + = X.x[i]-ns[o].mx[i];if (x . X[i] < Ns[o].mn[i]) ans + = ns[o].mn[i]-x.x[i];} return ans;} int querymn (int o) {if (!o) return oo;int ans = oo;if (!ns[o].del) ans = min (ans, ns[o] * x); int dl = CALCMN (Lc[o]), Dr = CA LCMN (Rc[o]); if (DL < DR) {if (DL < ans) ans = min (ans, querymn (Lc[o])); if (Dr < ans) ans = min (ans, querymn (rc[o));} else {if (Dr < ans) ans = min (ans, querymn (Rc[o])); if (dl < ans) ans = min (ans, querymn (Lc[o]));} return ans;} int main () {ns[0].mx[0] = ns[0].mx[1] =-oo;ns[0].mn[0] = ns[0].mn[1] = Oo;n = Read (); for (int i = 1; I <= n; i++) {Nx[i ] = ns[++tot].x[0] = read (); Ny[i] = ns[tot].x[1] = read (); ns[tot].del = 0;} Build (root, 1, N, 0), int ans = oo;for (int i = 1; I <= n; i++) {x.x[0] = Nx[i]; x.x[1] = ny[i];remove (root, 0); ans = min (ans, querymx (Root)-querymn (Root)),//printf ("%d%d\n", querymx (Root), querymn (root)), insert (root, 0);} printf ("%d\n", ans); return 0;} /*60 01 10 99 34 29 8*/
[BZOJ1941] [Sdoi2010] Hide and Seek