天天看点

HDU 5992 Finding Hotels

Problem Description

There are N hotels all over the world. Each hotel has a location and a price. M guests want to find a hotel with an acceptable price and a minimum distance from their locations. The distances are measured in Euclidean metric.

Input

The first line is the number of test cases. For each test case, the first line contains two integers N (N ≤ 200000) and M (M ≤ 20000). Each of the following N lines describes a hotel with 3 integers x (1 ≤ x ≤ N), y (1 ≤ y ≤ N) and c (1 ≤ c ≤ N), in which x and y are the coordinates of the hotel, c is its price. It is guaranteed that each of the N hotels has distinct x, distinct y, and distinct c. Then each of the following M lines describes the query of a guest with 3 integers x (1 ≤ x ≤ N), y (1 ≤ y ≤ N) and c (1 ≤ c ≤ N), in which x and y are the coordinates of the guest, c is the maximum acceptable price of the guest.

Output

For each guests query, output the hotel that the price is acceptable and is nearest to the guests location. If there are multiple hotels with acceptable prices and minimum distances, output the first one.

Sample Input

2
3 3
1 1 1
3 2 3
2 3 2
2 2 1
2 2 2
2 2 3
5 5
1 4 4
2 1 2
4 5 3
5 2 1
3 3 5
3 3 1
3 3 2
3 3 3
3 3 4
3 3 5      

Sample Output

1 1 1
2 3 2
3 2 3
5 2 1
2 1 2
2 1 2
1 4 4
3 3 5      

KDTree板子

#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 2e5 + 10;
int T, n, m, f[N][3];
int a[N], z, b[N << 2], ans;

void build(int x, int y, int l, int r) {
    if (l > r) return;
    int mid = l + r >> 1;
    z = y;
    nth_element(a + l, a + mid, a + r + 1, [](int u, int v) {return f[u][z] < f[v][z]; });
    b[x] = a[mid];
    build(x << 1, y + 1 & 1, l, mid - 1);
    build(x << 1 | 1, y + 1 & 1, mid + 1, r);
}

long long sqr(int x) {
    return 1LL * x * x;
}

long long get(int x, int y) {
    return sqr(f[x][0] - f[y][0]) + sqr(f[x][1] - f[y][1]);
}

void query(int x, int y, int l, int r) {
    if (l > r) return;
    int mid = l + r >> 1, xx = b[x];

    if (f[n][y] < f[xx][y]) {
        query(x << 1, y + 1 & 1, l, mid - 1);
    }
    if (f[n][y] >= f[xx][y]) {
        query(x << 1 | 1, y + 1 & 1, mid + 1, r);
    }

    if (ans == -1 && f[xx][2] <= f[n][2]) {
        ans = xx;
    }
    if (ans > -1 && f[xx][2] <= f[n][2]) {
        if (get(n, ans) > get(n, xx) || get(n, ans) == get(n, xx) && ans > xx) {
            ans = xx;
        }
    }

    if (ans == -1 || get(n, ans) >= sqr(f[xx][y] - f[n][y])) {
        if (f[n][y] >= f[xx][y]) {
            query(x << 1, y + 1 & 1, l, mid - 1);
        }
        if (f[n][y] < f[xx][y]) {
            query(x << 1 | 1, y + 1 & 1, mid + 1, r);
        }
    }
}

int main() {
    for (scanf("%d", &T); T--;) {
        scanf("%d%d", &n, &m);
        for (int i = 0; i < n; i++) {
            a[i] = i;
            for (int j = 0; j < 3; j++) {
                scanf("%d", &f[i][j]);
            }
        }
        build(1, 0, 0, n - 1);
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < 3; j++) {
                scanf("%d", &f[n][j]);
            }
            ans = -1;
            query(1, 0, 0, n - 1);
            printf("%d %d %d\n", f[ans][0], f[ans][1], f[ans][2]);
        }
    }
    return 0;
}