HDU 5992 Finding Hotels

来源:互联网 发布:大数据开发入门的好书 编辑:程序博客网 时间:2024/05/29 11:15
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
23 31 1 13 2 32 3 22 2 12 2 22 2 35 51 4 42 1 24 5 35 2 13 3 53 3 13 3 23 3 33 3 43 3 5
 

Sample Output
1 1 12 3 23 2 35 2 12 1 22 1 21 4 43 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;}



原创粉丝点击