BZOJ 2648 (kdtree)

来源:互联网 发布:linux物理内存分配 编辑:程序博客网 时间:2024/06/05 09:54

题目链接:点击这里

题意:给出n个点,接下来m个操作,每次插入一个点,或者询问离询问点的最近曼哈顿距离。

直接暴力插点询问即可。

#include <cstdio>#include <iostream>#include <cstring>#include <queue>#include <cmath>#include <algorithm>#include <stack>#define Clear(x,y) memset (x,y,sizeof(x))#define Close() ios::sync_with_stdio(0)#define Open() freopen ("more.in", "r", stdin)#define get_min(a,b) a = min (a, b)#define get_max(a,b) a = max (a, b);#define fi first#define se second#define pii pair<int, int>#define pli pair<long long, int>#define pb push_back#define mod 1000000007template <class T>inline bool scan (T &ret) {    char c;    int sgn;    if (c = getchar(), c == EOF) return 0; //EOF    while (c != '-' && (c < '0' || c > '9') ) c = getchar();    sgn = (c == '-') ? -1 : 1;    ret = (c == '-') ? 0 : (c - '0');    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');    ret *= sgn;    return 1;}using namespace std;#define maxn 500005struct node {    int d[2], l, r;//节点的点的坐标 左右孩子    int Max[2], Min[2];//节点中点x的最值 y的最值}tree[maxn<<1], tmp;int n, m;int root, cmp_d;bool cmp (const node &a, const node &b) {    return a.d[cmp_d] < b.d[cmp_d] || (a.d[cmp_d] == b.d[cmp_d] &&            a.d[cmp_d^1] < b.d[cmp_d^1]);}void push_up (int p, int pp) {    get_min (tree[p].Min[0], tree[pp].Min[0]);    get_min (tree[p].Min[1], tree[pp].Min[1]);    get_max (tree[p].Max[0], tree[pp].Max[0]);    get_max (tree[p].Max[1], tree[pp].Max[1]);}int build_tree (int l, int r, int D) {    int mid = (l+r)>>1;    tree[mid].l = tree[mid].r = 0;    cmp_d = D;    nth_element (tree+l+1, tree+mid+1, tree+1+r, cmp);    //按照cmp把第mid元素放在中间 比他小的放左边 比他大的放右边    tree[mid].Max[0] = tree[mid].Min[0] = tree[mid].d[0];    tree[mid].Max[1] = tree[mid].Min[1] = tree[mid].d[1];    if (l != mid) tree[mid].l = build_tree (l, mid-1, D^1);    if (r != mid) tree[mid].r = build_tree (mid+1, r, D^1);    if (tree[mid].l) push_up (mid, tree[mid].l);    if (tree[mid].r) push_up (mid, tree[mid].r);    return mid;}void insert (int now) {    int D = 0, p = root;    while (1) {        push_up (p, now);//先更新p节点        if (tree[now].d[D] >= tree[p].d[D]) {            if (!tree[p].r) {                tree[p].r = now;                return ;            }            else p = tree[p].r;        }        else {            if (!tree[p].l) {                tree[p].l = now;                return ;            }            else p = tree[p].l;        }        D ^= 1;    }    return ;}#define INF 1e9int ans, x, y;int dis (int p, int x, int y) {//点(x,y)在p的管辖范围内的可能最小值    int ans = 0;    if (x < tree[p].Min[0]) ans += tree[p].Min[0]-x;    if (x > tree[p].Max[0]) ans += x-tree[p].Max[0];    if (y < tree[p].Min[1]) ans += tree[p].Min[1]-y;    if (y > tree[p].Max[1]) ans += y-tree[p].Max[1];    return ans;}void query (int p) {    int dl = INF, dr = INF, d0;    d0 = abs (tree[p].d[0]-x) + abs (tree[p].d[1]-y);//初始答案    get_min (ans, d0);    if (tree[p].l) dl = dis (tree[p].l, x, y);    if (tree[p].r) dr = dis (tree[p].r, x, y);    if (dl < dr) {        if (dl < ans) query (tree[p].l);        if (dr < ans) query (tree[p].r);    }    else {        if (dr < ans) query (tree[p].r);        if (dl < ans) query (tree[p].l);    }}int main () {    //Open ();    while (scanf ("%d%d", &n, &m) == 2) {        for (int i = 1; i <= n; i++) {            scan (tree[i].d[0]);            scan (tree[i].d[1]);        }        root = build_tree (1, n, 0);        for (int i = 1; i <= m; i++) {             int op;            scan (op); scan (x); scan (y);            if (op == 1) {                n++; tree[n].d[0] = x, tree[n].d[1] = y;                tree[n].Max[0] = tree[n].Min[0] = x;                tree[n].Max[1] = tree[n].Min[1] = y;                insert (n);            }            else {                ans = INF;                query (root);                printf ("%d\n", ans);            }        }    }    return 0;}
0 0
原创粉丝点击