二维数点问题

· · 个人记录

二维数点问题

很多问题都可以转化为二维数点问题。

模板

P2163 [SHOI2007] 园丁的烦恼

题意:给定 n 个点,m 次询问矩形内有几个点。1\le n,m\le5\times10^5,0\le x,y\le10^7。

本题不能用二维树状数组解,因为离散化后坐标范围仍有 5\times 10^5。

二维数点的基本思路是把一个询问拆成四个前缀和(与树状数组一样)。

考虑如何处理这四个前缀和。因为静态的不好处理(否则树状数组也能做),所以考虑动态加点。

于是要按一定顺序排序,不妨把点和询问都按 x 坐标从小到大排序。

考虑当前还没有加入的第一个点,如果该点 x 坐标小于正在处理的询问的 x 坐标,把这个点的 y 坐标作为下标加入树状数组,如此处理直到没有这样的点。

这个询问的答案也就是已经加入的点中 y 坐标小于等于它的 y 坐标的点的个数,树状数组查询即可。

注意本题坐标有 0,需要坐标整体加 1。

#include <algorithm>
#include <iostream>

using namespace std;

const int RANGE = 1e7 + 2;

int n, m;
struct node {
    int x, y;
} a[500005];
struct question {
    int x, y, id, opt;
    question() {}
    question(int _x, int _y, int _id, int _opt) : x(_x), y(_y), id(_id), opt(_opt) {}
    question(const question &qss) {
        x = qss.x;
        y = qss.y;
        id = qss.id;
        opt = qss.opt;
    }
    ~question() {}
    inline question &operator=(const question &qss) {
        x = qss.x;
        y = qss.y;
        id = qss.id;
        opt = qss.opt;
        return *this;
    }
} qs[2000005];
int qc;
int ans[500005];

int c[10000005];
static inline int lowbit(int x) {
    return x & (-x);
}
static inline void insert(int x) {
    while (x <= RANGE) {
        ++c[x];
        x += lowbit(x);
    }
}
static inline int query(int x) {
    int ret = 0;
    while (x) {
        ret += c[x];
        x -= lowbit(x);
    }
    return ret;
}

signed main() {
#ifndef ONLINE_JUDGE
    freopen("P2163.in", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i].x >> a[i].y;
        ++a[i].x; // 防止 0 坐标
        ++a[i].y;
    }
    for (int i = 1; i <= m; ++i) {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        ++x1, ++y1, ++x2, ++y2;
        qs[++qc] = question(x2, y2, i, 1);
        qs[++qc] = question(x2, y1 - 1, i, -1);
        qs[++qc] = question(x1 - 1, y2, i, -1);
        qs[++qc] = question(x1 - 1, y1 - 1, i, 1);
    }
    sort(a + 1, a + n + 1, [](const node &x, const node &y) { return x.x < y.x; });
    sort(qs + 1, qs + qc + 1, [](const question &x, const question &y) { return x.x < y.x; });
    int now = 1;
    for (int i = 1; i <= qc; ++i) {
        while (now <= n && a[now].x <= qs[i].x) {
            insert(a[now].y);
            ++now;
        }
        ans[qs[i].id] += qs[i].opt * query(qs[i].y);
    }
    for (int i = 1; i <= m; ++i) {
        cout << ans[i] << endl;
    }
    return 0;
}

应用

CF1896E Permutation Sorting

题意:给定一个排列 a,每次操作把所有下标不等于值的数向右循环移位,输出每个下标等于其值的时间。1\le n\le10^6。

对于一个数 x,它跳到 x 位置需要经过 x-now_x 个点,now_x 表示 x 点当前的位置。

但是这中间有一些点会在 x 跳过它们前固定位置,这些点一定满足起始位置都在区间 [now_x,x] 内(认为这个排列是循环的,因此 now_x>x 也合法)。

只需二维数点找出这些点的数目并减去即可。

参考

#include <algorithm>
#include <iostream>

using namespace std;

int T, n;
int a[2000005];
int ans[10000005];

int c[2000005];
static inline int lowbit(int x) {
    return x & (-x);
}
static inline void insert(int x) {
    while (x <= n * 2) {
        ++c[x];
        x += lowbit(x);
    }
}
static inline int query(int x) {
    int ret = 0;
    while (x) {
        ret += c[x];
        x -= lowbit(x);
    }
    return ret;
}

int r[2000005];

signed main() {
#ifndef ONLINE_JUDGE
    freopen("CF1896E.in", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> T;
    while (T--) {
        cin >> n;
        for (int i = 1; i <= n * 2; ++i) {
            c[i] = 0;
            r[i] = 0;
        }
        for (int i = 1; i <= n; ++i) {
            cin >> a[i];
            if (i <= a[i]) {
                r[i] = a[i];
                r[n + i] = n + a[i];
            } else {
                r[i] = n + a[i];
            }
        }
        for (int i = 2 * n; i; --i) {
            if (!r[i]) {
                continue;
            }
            if (i <= n) {
                ans[a[i]] = r[i] - i + query(i - 1) - query(r[i]);
            }
            insert(r[i]);
        }
        for (int i = 1; i <= n; ++i) {
            cout << ans[i] << ' ';
        }
        cout << endl;
    }
    return 0;
}