KD-Tree解决空间最短距离的利器

Posted by Cww97 on 2017-09-27

版权声明:本文为博主原创文章,未经博主允许不得转载。原文所在http://blog.csdn.net/cww97 https://blog.csdn.net/cww97/article/details/78110909
# KD-Tree解决空间最短距离的利器

本文作者:Caozhijie

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
/**************************************************************
Problem: 2648
User: Cardinal
Language: C++
Result: Accepted
Time:14476 ms
Memory:40356 kb
****************************************************************/

#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
using namespace std;

#define Min(a, b) ((a)<(b)?(a):(b))
#define Max(a, b) ((a)>(b)?(a):(b))
#define Abs(x) ((x)>0?(x):-(x))

#define N 500010
#define M 500010
int n, m;

struct Point {
int x, y;
Point(int _x = 0, int _y = 0):x(_x),y(_y){}
void set(int _, int __) {
x = _, y = __;
}
}P[N + M];
int Dis(const Point &A, const Point &B) {
return Abs(A.x - B.x) + Abs(A.y - B.y);
}

bool sign;
inline bool cmp(const Point &A, const Point &B) {
if (sign)
return A.x < B.x || (A.x == B.x && A.y < B.y);
else
return A.y < B.y || (A.y == B.y && A.x < B.x);
}

struct Node {
Node *l, *r;
int x[2], y[2];
Point p;

void SetP(const Point &P) {
p = P;
x[0] = x[1] = P.x;
y[0] = y[1] = P.y;
}
int Dis(const Point &p) const {
int res = 0;
if (p.x < x[0] || p.x > x[1])
res += (p.x < x[0]) ? x[0] - p.x : p.x - x[1];
if (p.y < y[0] || p.y > y[1])
res += (p.y < y[0]) ? y[0] - p.y : p.y - y[1];
return res;
}
void up(Node *B) {
x[0] = Min(x[0], B->x[0]);
x[1] = Max(x[1], B->x[1]);
y[0] = Min(y[0], B->y[0]);
y[1] = Max(y[1], B->y[1]);
}
}mem[N + M], *C = mem, Tnull, *null = &Tnull;

Node *Build(int tl, int tr, bool d) {
if (tl > tr)
return null;

int mid = (tl + tr) >> 1;
sign = d;
std::nth_element(P + tl + 1, P + mid + 1, P + tr + 1, cmp);

Node *q = C++;
q->SetP(P[mid]);
q->l = Build(tl, mid - 1, d ^ 1);
q->r = Build(mid + 1, tr, d ^ 1);
if (q->l != null)
q->up(q->l);
if (q->r != null)
q->up(q->r);
return q;
}

#define INF 0x3f3f3f3f
int res;
void Ask(Node *q, const Point &p) {
res = Min(res, Dis(q->p, p));
int DisL = q->l != null ? q->l->Dis(p) : INF;
int DisR = q->r != null ? q->r->Dis(p) : INF;
if (DisL < DisR) {
if (q->l != null)
Ask(q->l, p);
if (DisR < res && q->r != null)
Ask(q->r, p);
}
else {
if (q->r != null)
Ask(q->r, p);
if (DisL < res && q->l != null)
Ask(q->l, p);
}
}

void Insert(Node *root, const Point &p) {
Node *q = C++;
q->l = q->r = null;
q->SetP(p);

sign = 0;
while(1) {
root->up(q);
if (cmp(q->p, root->p)) {
if (root->l == null) {
root->l = q;
break;
}
else
root = root->l;
}
else {
if (root->r == null) {
root->r = q;
break;
}
else
root = root->r;
}
sign ^= 1;
}
}

int main() {
#ifndef ONLINE_JUDGE
//freopen("tt.in", "r", stdin);
//freopen("tt.out", "w", stdout);
#endif

scanf("%d%d", &n, &m);

register int i;
int ope, x, y;
for(i = 1; i <= n; ++i) {
scanf("%d%d", &x, &y);
P[i] = Point(x, y);
}

Node* root = Build(1, n, 0);

while(m--) {
scanf("%d%d%d", &ope, &x, &y);
if (ope == 1)
Insert(root, Point(x, y));
else {
res = INF;
Ask(root, Point(x, y));
printf("%d\n", res);
}
}

return 0;
}