K-D Tree模板
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define maxn 500010
using namespace std;
int n, m, D;
void read(int &num){
num = 0;
char ch = getchar();
int flag = 1;
for(; ch < '!'; ch = getchar());
if(ch == '-')flag = -1;
for(; ch > '!'; ch = getchar())
num = num * 10 + ch - 48;
num = num * flag;
}
struct Point{
#define N 2
int d[N], mn[N], mx[N], l, r;
int& operator[](int i){return d[i];}
bool operator<(Point p)const{
return d[D] < p[D];
}
Point(int x = 0, int y = 0){
d[0] = x;
d[1] = y;
l = 0, r = 0;
for(int i = 0; i < 2; i ++)
mn[i] = mx[i] = 0;
}
}p[maxn];
const int inf = 0x7fffffff;
int Abs(int x){
return x > 0 ? x : -x;
}
int dis(Point a, Point b){
return Abs(a[0] - b[0]) + Abs(a[1] - b[1]);
}
namespace K_Dtree{
int root, ans;
Point P, t[maxn << 1];
#define L(p) t[p].l
#define R(p) t[p].r
void update(int p){
for(int i = 0; i < 2; i ++){
t[p].mn[i] = t[p].mx[i] = t[p].d[i];
if(L(p)){
t[p].mn[i] = min(t[p].mn[i], t[L(p)].mn[i]);
t[p].mx[i] = max(t[p].mx[i], t[L(p)].mx[i]);
}
if(R(p)){
t[p].mn[i] = min(t[p].mn[i], t[R(p)].mn[i]);
t[p].mx[i] = max(t[p].mx[i], t[R(p)].mx[i]);
}
}
}
int build(int l, int r, int now){
int mid = l + r >> 1;D = now;
nth_element(p + l, p + mid, p + r + 1);
t[mid] = p[mid];
if(l < mid)L(mid) = build(l, mid - 1, now ^ 1);
if(r > mid)R(mid) = build(mid + 1, r, now ^ 1);
update(mid);
return mid;
}
void insert(int p, int now){
if(P[now] >= t[p][now]){
if(R(p))insert(R(p), now ^ 1);
else{
t[p].r = ++ n;
t[n] = P;
update(n);
}
}
else{
if(L(p))insert(L(p), now ^ 1);
else{
t[p].l = ++ n;
t[n] = P;
update(n);
}
}
update(p);
}
int dist(int p1, Point p){
int ans = 0;
for(int i = 0; i < 2; i ++)
ans += max(0, t[p1].mn[i] - p[i]);
<span style="white-space:pre"> </span>for(int i = 0; i < 2; i ++)
<span style="white-space:pre"> </span>ans += max(0, p[i] - t[p1].mx[i]);
return ans;
}
void ask(int p, int now){
int dl = inf, dr = inf;
ans = min(ans, dis(t[p], P));
if(L(p))dl = dist(L(p), P);
if(R(p))dr = dist(R(p), P);
if(dl < dr){
if(L(p) && dl < ans)ask(L(p), now ^ 1);
if(R(p) && dr < ans)ask(R(p), now ^ 1);
}
else{
if(R(p) && dr < ans)ask(R(p), now ^ 1);
if(L(p) && dl < ans)ask(L(p), now ^ 1);
}
}
void insert(const Point& __){
P = __;
insert(root, 0);
}
void init(){root = build(1, n, 0);}
int ask(const Point& __){
P = __;
ans = inf;
ask(root, 0);
return ans;
}
}
int main(){
read(n), read(m);
for(int i = 1; i <= n; i ++)
read(p[i][0]), read(p[i][1]);
K_Dtree::init();
int opt, x, y;
while(m --){
read(opt), read(x), read(y);
if(opt == 1)
K_Dtree::insert(Point(x, y));
else
printf("%d\n", K_Dtree::ask(Point(x, y)));
}
return 0;
}
[3.28 upd]又复习了一遍,发现insert和ask不会写了
最重要的是nth_element(t+l,t+mid,t+1+r)写成1~n了!
稀里哇啦wa一片
#include <bits/stdc++.h>
#define maxn 1000010
using namespace std;
int n, m, D;
struct Node{
int d[2], mn[2], mx[2], l, r;
Node(int x = 0, int y = 0){
d[0] = x, d[1] = y;
mn[0] = mn[1] = mx[0] = mx[1] = l = r = 0;
}
int& operator[](const int& k){return d[k];}
bool operator<(Node k)const{return d[D] < k[D];}
}t[maxn], T;
int root;
void update(int id){
for(int i = 0; i < 2; i ++){
t[id].mn[i] = t[id].mx[i] = t[id][i];
if(t[id].l){
t[id].mn[i] = min(t[id].mn[i], t[t[id].l].mn[i]);
t[id].mx[i] = max(t[id].mx[i], t[t[id].l].mx[i]);
}
if(t[id].r){
t[id].mn[i] = min(t[id].mn[i], t[t[id].r].mn[i]);
t[id].mx[i] = max(t[id].mx[i], t[t[id].r].mx[i]);
}
}
}
int build(int l, int r, int type){
if(l > r)return 0;
D = type; int mid = l + r >> 1;
nth_element(t+l, t+mid, t+1+r);
t[mid].l = build(l, mid-1, type^1);
t[mid].r = build(mid+1, r, type^1);
update(mid);
return mid;
}
int dist(int id){
int ret = 0;
for(int i = 0; i < 2; i ++){
ret += max(0, t[id].mn[i] - T[i]);
ret += max(0, T[i] - t[id].mx[i]);
}
return ret;
}
inline int Abs(int x){return x > 0 ? x : -x;}
inline int ask_dis(Node a, Node b){return Abs(a[0] - b[0]) + Abs(a[1] - b[1]);}
void insert(int id, int type){
if(T[type] >= t[id][type]){
if(t[id].r)insert(t[id].r, type^1);
else{
t[id].r = ++ n;
t[n] = T;
update(n);
}
}
else{
if(t[id].l)insert(t[id].l, type^1);
else{
t[id].l = ++ n;
t[n] = T;
update(n);
}
}
update(id);
}
void Insert(Node P){
T = P;
insert(root, 0);
}
int ans;
const int inf = 0x7fffffff;
void ask(int id, int type){
int dl = inf, dr = inf;
ans = min(ans, ask_dis(t[id], T));
if(t[id].l)dl = dist(t[id].l);
if(t[id].r)dr = dist(t[id].r);
if(dl < dr){
if(t[id].l && dl < ans)ask(t[id].l, type^1);
if(t[id].r && dr < ans)ask(t[id].r, type^1);
}
else{
if(t[id].r && dr < ans)ask(t[id].r, type^1);
if(t[id].l && dl < ans)ask(t[id].l, type^1);
}
}
int ask(Node P){
T = P;
ans = inf;
ask(root, 0);
return ans;
}
int main(){
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i ++)
scanf("%d%d", &t[i][0], &t[i][1]);
root = build(1, n, 0);
int opt, x, y;
while(m --){
scanf("%d%d%d", &opt, &x, &y);
if(opt == 1)Insert(Node(x, y));
else printf("%d\n", ask(Node(x, y)));
}
return 0;
}