题解 CF1045G 【AI robots】

· · 题解

给个不一样的做法,不依赖 k\leq 20。

我才不会告诉你这个是我化学中考的时候想出来的。

考虑忽略 q 的限制,给定一个机器人集合,再给定另外一个机器人,如何查询这个机器人能够看到多少个机器人。

有如下方法:对于该集合内的所有机器人 (x_i,r_i),在二维平面上添加一条平行于 y 轴的线段 (x_i,x_i-r_i)-(x_i,x_i+r_i)。对于给定的机器人 (X,R),求平行于 x 轴的线段 (X-R,X)-(X+R,X) 上面的交点个数即可。

现在引入 q 的限制,可以把机器人按 q 排序之后双指针维护出查询一个机器人能看到的数量时对应的被看到的机器人集合。再通过上述方法,问题被转化为动态加入和删除平行于 y 轴的线段,以及给定平行于 x 轴线段查询交点数量。

如果问题是静态的,即给定的 y 轴线段不被修改,则可以对修改线段差分后在 y 轴上扫描线解决。动态后直接在扫描线上套 cdq 分治,复杂度为 O(n\log^2n)。

看起来常数很大,但跑起来还是蛮快的,cf 上最大点 481ms。

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1 << 21], *p1 = buf, *p2 = buf;

inline int qread() {
    register char c = getchar();
    register int x = 0, f = 1;
    while (c < '0' || c > '9') {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = (x << 3) + (x << 1) + c - 48;
        c = getchar();
    }
    return x * f;
}

inline int Abs(const int& x) {return (x > 0 ? x : -x);}
inline int Max(const int& x, const int& y) {return (x > y ? x : y);}
inline int Min(const int& x, const int& y) {return (x < y ? x : y);}

struct Bot {
    int x, r, q;
    bool operator < (const Bot& b) const {
        return q < b.q;
    }
};
struct Opt {
    int type, x, yl, yr;
    Opt() {}
    Opt(int type, int x, int yl, int yr) : type(type), x(x), yl(yl), yr(yr) {}
};
Opt opt[500005], tmp[500005];
int n, k, q, vtop, c[1500005];
long long ans = 0, val[1500005];
Bot a[100005];
int top = 0;

inline void Read() {
    n = qread(); k = qread();
    for (int i = 1;i <= n;i++) {
        a[i].x = qread();
        a[i].r = qread();
        a[i].q = qread();
    }
}

inline void Prefix() {
    int l = 1, r = 1;
    for (int i = 1;i <= n;i++) {
        while (r <= n && a[r].q <= a[i].q + k) {
            opt[++q] = Opt(1, a[r].x - a[r].r, a[r].x, a[r].x);
            opt[++q] = Opt(-1, a[r].x + a[r].r + 1, a[r].x, a[r].x);
            r++;
        }
        while (l <= n && a[l].q < a[i].q - k) {
            opt[++q] = Opt(-1, a[l].x - a[l].r, a[l].x, a[l].x);
            opt[++q] = Opt(1, a[l].x + a[l].r + 1, a[l].x, a[l].x);
            l++;
        }
        //printf("%d [%d,%d] %d\n", i, l, r, a[r].q);
        opt[++q] = Opt(0, a[i].x, a[i].x - a[i].r, a[i].x + a[i].r);
    }
    for (int i = 1;i <= q;i++) {
        val[i] = opt[i].yl;
        val[i + q] = opt[i].yr;
        val[i + 2 * q] = opt[i].x;
    }
    sort(val + 1, val + 3 * q + 1);
    vtop = unique(val + 1, val + 3 * q + 1) - val - 1;
    for (int i = 1;i <= q;i++) {
        opt[i].yl = lower_bound(val + 1, val + vtop + 1, opt[i].yl) - val;
        opt[i].yr = lower_bound(val + 1, val + vtop + 1, opt[i].yr) - val;
        opt[i].x = lower_bound(val + 1, val + vtop + 1, opt[i].x) - val;
    }
    //for (int i = 1;i <= q;i++) printf("%d %d %d %d\n", opt[i].type, opt[i].x, opt[i].yl, opt[i].yr);
}

inline int Lowbit(int x) {
    return x & -x;
}

inline void Update(int i, int x) {
    for (int j = i;j <= vtop;j += Lowbit(j)) c[j] += x;
}

inline int Query(int i) {
    int ans = 0;
    for (int j = i;j >= 1;j -= Lowbit(j)) ans += c[j];
    return ans;
}

inline void Clear(int i) {
    for (int j = i;j <= vtop;j += Lowbit(j)) {
        if (!c[j]) break;
        c[j] = 0;
    }
}

inline void Cdq(int l, int r) {
    if (l == r) return;
    int mid = l + r >> 1;
    Cdq(l, mid);
    Cdq(mid + 1, r);
    int i = l, j = mid + 1, k = l;
    while (i <= mid && j <= r) {
        if (opt[i].x <= opt[j].x) {
            if (opt[i].type) Update(opt[i].yl, opt[i].type);
            //else ans += sgt.Query(sgt._root, -1e9, 2e9, opt[i].yl, opt[i].yr);
            tmp[k++] = opt[i++];
        } else {
            if (!opt[j].type) ans += Query(opt[j].yr) - Query(opt[j].yl - 1);
            tmp[k++] = opt[j++];
        }
    }
    while (i <= mid) {
        if (opt[i].type) Update(opt[i].yl, opt[i].type);
        //else ans += sgt.Query(sgt._root, -1e9, 2e9, opt[i].yl, opt[i].yr);
        tmp[k++] = opt[i++];
    }
    while (j <= r) {
        if (!opt[j].type) ans += Query(opt[j].yr) - Query(opt[j].yl - 1);
        tmp[k++] = opt[j++];
    }
    for (int i = l;i <= mid;i++) Clear(opt[i].yl);
    for (int i = l;i <= r;i++) opt[i] = tmp[i];
}

int main() {
    Read();
    sort(a + 1, a + n + 1);
    Prefix();
    Cdq(1, q);
    printf("%lld", (ans - n) / 2);
    #ifdef CFA_44
    while (1);
    #endif
    return 0;
}