Skip to content

Instantly share code, notes, and snippets.

@Sd-Invol
Created October 28, 2014 10:20
Show Gist options
  • Select an option

  • Save Sd-Invol/c832fb7c9bf7fa665657 to your computer and use it in GitHub Desktop.

Select an option

Save Sd-Invol/c832fb7c9bf7fa665657 to your computer and use it in GitHub Desktop.
一看就懂的最近邻搜索
void getNearest(int p , int k) {
if (!p) return;
if (t[p].vis) {
LL dis = dist(P , t[p].u);
if (dis < res || (dis == res && t[p].o < t[ret].o))
res = dis , ret = p;
}
if (k) {
if (cmpY(P , t[p].u)) {
getNearest(t[p].c[0] , k ^ 1);
if (sqr(P.second - t[p].u.second) <= res)
getNearest(t[p].c[1] , k ^ 1);
} else {
getNearest(t[p].c[1] , k ^ 1);
if (sqr(P.second - t[p].u.second) <= res)
getNearest(t[p].c[0] , k ^ 1);
}
} else {
if (cmpX(P , t[p].u)) {
getNearest(t[p].c[0] , k ^ 1);
if (sqr(P.first - t[p].u.first) <= res)
getNearest(t[p].c[1] , k ^ 1);
} else {
getNearest(t[p].c[1] , k ^ 1);
if (sqr(P.first - t[p].u.first) <= res)
getNearest(t[p].c[0] , k ^ 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment