【HDOJ】2966 In case of failure

KD树,这东西其实在ML经常被使用,不过30s的时限还是第一次见。

  1 /* 2966 */
  2 #include <iostream>
  3 #include <string>
  4 #include <map>
  5 #include <queue>
  6 #include <stack>
  7 #include <set>
  8 #include <vector>
  9 #include <algorithm>
 10 #include <cstdio>
 11 #include <cmath>
 12 #include <ctime>
 13 #include <cstring>
 14 #include <climits>
 15 #include <cctype>
 16 using namespace std;
 17 
 18 typedef struct {
 19     int x, y;
 20 } Point_t;
 21 
 22 typedef struct {
 23     int axis;
 24     Point_t p;
 25     int l, r;
 26 } node_t;
 27 
 28 const int maxn = 1e5+5;
 29 Point_t P[maxn], P2[maxn];
 30 node_t nd[maxn];
 31 int L;
 32 __int64 ans;
 33 
 34 __int64 Eucl(Point_t a, Point_t b) {
 35     return 1LL*(a.x-b.x)*(a.x-b.x) + 1LL*(a.y-b.y)*(a.y-b.y);
 36 }
 37 
 38 bool compx(const Point_t &a, const Point_t &b) {
 39     return a.x < b.x;
 40 }
 41 
 42 bool compy(const Point_t &a, const Point_t &b) {
 43     return a.y < b.y;
 44 }
 45 
 46 void newNode() {
 47     ++L;
 48     nd[L].l = nd[L].r = 0;
 49 }
 50 
 51 int create_KD(int l, int r, int d) {
 52     if (l > r)
 53         return 0;
 54     newNode();
 55     int rt = L;
 56     nd[rt].axis = d&1;
 57     if (l == r) {
 58         nd[rt].p = P[l];
 59         return rt;
 60     }
 61     if (d & 1)
 62         sort(P+l, P+r+1, compy);
 63     else
 64         sort(P+l, P+r+1, compx);
 65     int mid = (l+r)>>1;
 66     nd[rt].p = P[mid];
 67     nd[rt].l = create_KD(l, mid-1, d+1);
 68     nd[rt].r = create_KD(mid+1, r, d+1);
 69     return rt;
 70 }
 71 
 72 void cal(int rt, Point_t p) {
 73     if (rt == 0)
 74         return ;
 75     int i, j, k;
 76     
 77     __int64 d = Eucl(nd[rt].p, p);
 78     if (d && d<ans)
 79         ans = d;
 80     
 81     if (nd[rt].axis) {
 82         j = nd[rt].p.y;
 83         k = p.y;
 84     } else {
 85         j = nd[rt].p.x;
 86         k = p.x;
 87     }
 88     
 89     if (k <= j)
 90         cal(nd[rt].l, p);
 91     else
 92         cal(nd[rt].r, p);
 93     
 94     if (1LL*(j-k)*(j-k) < ans) {
 95         if (k <= j)
 96             cal(nd[rt].r, p);
 97         else
 98             cal(nd[rt].l, p);
 99     }
100 }
101 
102 int main() {
103     int t, n;
104     int i, j, k;
105     
106     #ifndef ONLINE_JUDGE
107         freopen("data.in", "r", stdin);
108         freopen("data.out", "w", stdout);
109     #endif
110     
111     scanf("%d", &t);
112     while (t--) {
113         scanf("%d", &n);
114         for (i=0; i<n; ++i) {
115             scanf("%d %d", &P[i].x, &P[i].y);
116             P2[i] = P[i];
117         }
118         L = 0;
119         int rt = create_KD(0, n-1, 0);
120         for (i=0; i<n; ++i) {
121             ans = LLONG_MAX;
122             cal(rt, P2[i]);
123             printf("%I64d
", ans);
124         }
125     }
126     
127     #ifndef ONLINE_JUDGE
128         printf("time = %d
", (int)(clock()));
129     #endif
130     
131     return 0;
132 }
原文地址:https://www.cnblogs.com/bombe1013/p/4464947.html