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
| const int maxn = (50000 + 5) << 1; const ll inf = 0x3f3f3f3f3f3f3f3f; int n, K, q, CD = 0;
class Node { public: int x[6], cld[2];
bool operator< (const Node &rhs) const { if (x[CD] != rhs.x[CD]) return x[CD] < rhs.x[CD]; _for(i, CD, K) if (x[i] != rhs.x[i]) return x[i] < rhs.x[i]; _for(i, 0, CD) if (x[i] != rhs.x[i]) return x[i] < rhs.x[i]; return x[CD] < rhs.x[CD]; } } t[maxn];
int root = 0; int build(int l, int r, int dep) { if (l > r) return 0; int mid = (l + r) >> 1; CD = dep % K;
nth_element(t+l, t+mid, t+r+1); t[mid].cld[0] = t[mid].cld[1] = 0; if (l < mid) t[mid].cld[0] = build(l, mid-1, dep+1); if (r > mid) t[mid].cld[1] = build(mid+1, r, dep+1);
return mid; }
inline ll euclid(const Node &u, const Node &g) { ll ans = 0; _for(i, 0, K) ans += 1ll*(u.x[i]-g.x[i]) * (u.x[i]-g.x[i]); return ans; }
typedef pair<ll, Node> PR; priority_queue<PR> ans;
void initans(int m) { while (ans.size()) ans.pop(); _for(_, 0, m) ans.push(make_pair(inf, Node())); }
void query(int u, const Node &g, int dep) { if (!u) return;
ll dist = euclid(t[u], g); //debug(dist); if (dist < ans.top().first) ans.pop(), ans.push(make_pair(dist, t[u]));
int cd = dep % K; ll delta = t[u].x[cd] - g.x[cd]; //debug(delta);
if (delta > 0) { query(t[u].cld[0], g, dep+1); if (delta * delta < ans.top().first) query(t[u].cld[1], g, dep+1); } else { query(t[u].cld[1], g, dep+1); if (delta * delta < ans.top().first) query(t[u].cld[0], g, dep+1); } }
void solve() { scanf("%d", &q); while (q--) { Node g; _for(i, 0, K) scanf("%d", &g.x[i]); int m; scanf("%d", &m); initans(m); assert(ans.size() == m);
query(root, g, 0); stack<PR> res; while (!ans.empty()) res.push(ans.top()), ans.pop();
printf("the closest %d points are:\n", m); while (!res.empty()) { PR u = res.top(); res.pop(); _for(i, 0, K) { //debug(x.first); printf("%d", u.second.x[i]); i != K-1 ? printf(" ") : printf("\n"); } } } }
void init() { CD = 0; }
int main() { freopen("input.txt", "r", stdin); while (~scanf("%d%d", &n, &K)) { init();
// get data _rep(i, 1, n) _for(j, 0, K) scanf("%d", &t[i].x[j]);
// build kdtree root = build(1, n, 0); // solve solve(); } }
|