HDU 2966 in case of failure KD tree

Source: Internet
Author: User
First KD Tree
#include <iostream> #include <string.h> #include <algorithm> #include <stdio.h> #include <

Math.h> #include <queue> using namespace std; #define N 100005 #define Lson RT << 1 #define Rson RT << 1 |

1 #define SQRT2 (x) (x) * (x) int n, idx;
	struct Node {long long feature[2];
	BOOL operator < (const Node &u) const {return FEATURE[IDX] < U.FEATURE[IDX];

}}_data[n], _data2[n];

Long long ans;
	Class Kdtree {public:void Build (int, int, int, int);
void Query (Node, int, int, int, int);


}KD;
	void Kdtree::build (int l, int r, int rt, int dept) {if (L > R) return;
	IDX = dept% 2;
	int mid = (L + r) >> 1;
	Nth_element (_data + L, _data + Mid, _data + R + 1);
	Build (L, Mid-1, Lson, dept + 1);
Build (mid + 1, R, Rson, dept + 1);
	} void Kdtree::query (Node p, int l, int r, int rt, int dept) {if (L > R) return;
    A long long sum = 0;
	int mid = (L + r) >> 1; for (int i = 0; i < 2; i++) sum + = SQRT2 (_data[mid].feature[i]-p.feature[i]);
	printf ("%d\n", Cur.first);
	int Dim = Dept% 2;
	BOOL FG = 0;
	int x = Lson;
	int y = Rson;
    if (P.feature[dim] < _data[mid].feature[dim]) {Query (P, L, mid-1, X, dept + 1);
    } else {Query (P, mid+1, R, x, dept + 1); } if (sum = = 0) {if (Sqrt2 (P.feature[dim]-_data[mid].feature[dim]) < ans) {FG =
        1;
        }} else {if (ans = =-1) {ans = sum;
        } else if (sum < ans) {ans = sum;
        } if (Sqrt2 (P.feature[dim]-_data[mid].feature[dim]) < ans) {FG = 1;  }} if (FG) {if (P.feature[dim] < _data[mid].feature[dim]) {Query (P, mid+1, R, X,
        Dept + 1);
        } else {Query (P, L, mid-1, X, dept + 1);
	}}} int main () {int T;
	scanf ("%d", &t);
		while (t--) {scanf ("%d", &n); for (int i = 0; i < n; i++) {for (int j = 0; J < 2; j + +) scanf ("%i64d", &_data[i].featu
            RE[J]);
        _data2[i] = _data[i]; } KD.
		Build (0, n-1, 1, 0);
			for (int i = 0; i < n; i++) {Node p;
			ans =-1;
			for (int j = 0; J < 2; j + +) P.feature[j] = _data2[i].feature[j]; Kd.
			Query (p, 0, n-1, 1, 0);
		printf ("%i64d\n", ans);
}} return 0; }
Http://www.cnblogs.com/v-July-v/archive/2012/11/20/3125419.html http://blog.csdn.net/acdreamers/article/details /44664645 http://blog.csdn.net/zhjchengfeng5/article/details/7855241 http://my.oschina.net/keyven/blog/221792

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.