bzoj 2716 [Violet 3]天使玩偶 cdq分治+树状数组

题面

题目传送门

解法

每一次询问有已知点中与询问点曼哈顿距离最小是多少

把绝对值拆开来分别维护一下

我写的比较丑,写了4个cdq,然后光荣T飞……

好像这道题k-dtree比cdq要优一点吧

cdq复杂度:\(O((n+m)\ log^2 n)\)

代码

#include <bits/stdc++.h>
#define inf 1 << 30
#define N 1000010
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
    x = 0; int f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
    while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
struct Node {
    int op, x, y, tim, id;
} a[N], t[N];
int mx, f[N], ans[N];
bool cmp1(Node a, Node b) {return a.x < b.x;}
bool cmp2(Node a, Node b) {return a.y > b.y;}
bool cmp3(Node a, Node b) {return a.x > b.x;}
int lowbit(int x) {return x & -x;}
void modify(int x, int v) {
    for (int i = x; i <= mx; i += lowbit(i))
        if (v == -inf) f[i] = v;
            else chkmax(f[i], v);
}
int query(int x) {
    int ret = -inf;
    for (int i = x; i; i -= lowbit(i))
        ret = max(ret, f[i]);
    return ret;
}
void cdq1(int l, int r) {
    if (l >= r) return;
    int mid = (l + r) >> 1, tx = l, ty = mid + 1;
    for (int i = l; i <= r; i++)
        if (a[i].tim <= mid) t[tx++] = a[i];
            else t[ty++] = a[i];
    for (int i = l; i <= r; i++) a[i] = t[i];
    tx = l, ty = mid + 1;
    while (ty <= r) {
        while (tx <= mid && a[tx].x <= a[ty].x) {
            if (a[tx].op == 1) modify(a[tx].y, a[tx].x + a[tx].y);
            tx++;
        }
        if (a[ty].op == 2) chkmin(ans[a[ty].id], a[ty].x + a[ty].y - query(a[ty].y));
        ty++;
    }
    for (int i = l; i < tx; i++) if (a[i].op == 1) modify(a[i].y, -inf);
    cdq1(l, mid); cdq1(mid + 1, r);
}
void cdq2(int l, int r) {
    if (l >= r) return;
    int mid = (l + r) >> 1, tx = l, ty = mid + 1;
    for (int i = l; i <= r; i++)
        if (a[i].tim <= mid) t[tx++] = a[i];
            else t[ty++] = a[i];
    for (int i = l; i <= r; i++) a[i] = t[i];
    tx = l, ty = mid + 1;
    while (ty <= r) {
        while (tx <= mid && a[tx].y >= a[ty].y) {
            if (a[tx].op == 1) modify(a[tx].x, a[tx].x - a[tx].y);
            tx++;
        }
        if (a[ty].op == 2) chkmin(ans[a[ty].id], a[ty].x - a[ty].y - query(a[ty].x));
        ty++;
    }
    for (int i = l; i < tx; i++) if (a[i].op == 1) modify(a[i].x, -inf);
    cdq2(l, mid); cdq2(mid + 1, r);
}
void cdq3(int l, int r) {
    if (l >= r) return;
    int mid = (l + r) >> 1, tx = l, ty = mid + 1;
    for (int i = l; i <= r; i++)
        if (a[i].tim <= mid) t[tx++] = a[i];
            else t[ty++] = a[i];
    for (int i = l; i <= r; i++) a[i] = t[i];
    tx = l, ty = mid + 1;
    while (ty <= r) {
        while (tx <= mid && a[tx].x >= a[ty].x) {
            if (a[tx].op == 1) modify(a[tx].y, a[tx].y - a[tx].x);
            tx++;
        }
        if (a[ty].op == 2) chkmin(ans[a[ty].id], a[ty].y - a[ty].x - query(a[ty].y));
        ty++;
    }
    for (int i = l; i < tx; i++) if (a[i].op == 1) modify(a[i].y, -inf);
    cdq3(l, mid); cdq3(mid + 1, r);
}
void cdq4(int l, int r) {
    if (l >= r) return;
    int mid = (l + r) >> 1, tx = l, ty = mid + 1;
    for (int i = l; i <= r; i++)
        if (a[i].tim <= mid) t[tx++] = a[i];
            else t[ty++] = a[i];
    for (int i = l; i <= r; i++) a[i] = t[i];
    tx = l, ty = mid + 1;
    while (ty <= r) {
        while (tx <= mid && a[tx].x >= a[ty].x) {
            if (a[tx].op == 1) modify(mx - a[tx].y + 1, -a[tx].x - a[tx].y);
            tx++;
        }
        if (a[ty].op == 2) chkmin(ans[a[ty].id], -a[ty].x - a[ty].y - query(mx - a[ty].y + 1));
        ty++;
    }
    for (int i = l; i < tx; i++) if (a[i].op == 1) modify(mx - a[i].y + 1, -inf);
    cdq4(l, mid); cdq4(mid + 1, r);
}
int main() {
    int n, m, tot = 0; read(n), read(m);
    for (int i = 1; i <= n; i++) {
        int x, y; read(x), read(y); chkmax(mx, max(x, y));
        a[++tot] = (Node) {1, x, y, tot, 0};
    }
    int cntq = 0;
    for (int i = 1; i <= m; i++) {
        read(a[++tot].op), read(a[tot].x), read(a[tot].y);
        chkmax(mx, max(a[tot].x, a[tot].y));
        if (a[tot].op == 2) a[tot].id = ++cntq; a[tot].tim = tot;
    }
    for (int i = 1; i <= cntq; i++) ans[i] = inf;
    for (int i = 1; i <= mx; i++) f[i] = -inf;
    sort(a + 1, a + tot + 1, cmp1); cdq1(1, tot);
    for (int i = 1; i <= mx; i++) f[i] = -inf;
    sort(a + 1, a + tot + 1, cmp2); cdq2(1, tot);
    for (int i = 1; i <= mx; i++) f[i] = -inf;
    sort(a + 1, a + tot + 1, cmp3); cdq3(1, tot);
    for (int i = 1; i <= mx; i++) f[i] = -inf;
    sort(a + 1, a + tot + 1, cmp3); cdq4(1, tot);
    for (int i = 1; i <= cntq; i++) cout << ans[i] << "\n";
    return 0;
}

posted @ 2018-08-14 22:18  谜のNOIP  阅读(113)  评论(0编辑  收藏  举报