けんちょんの競プロ精進記録

競プロの精進記録や小ネタを書いていきます

AtCoder ABC 327 F - Apples (青色, 550 点)

遅延セグ木 (区間加算 + 区間 max 取得) を用いた平面走査!

問題概要

二次元平面上に  N 個の点がある。点  i の座標は  (T_{i}, X_{i}) である。

この 2 次元平面上でサイズが  W \times D の長方形領域 (右辺と上辺は含まない) を自由に動かしていくとき、この長方形領域に覆われる点の個数の最大値を求めよ。

制約

  •  1 \le N, W, D, T_{i}, X_{i} \le 2 \times 10^{5}

考えたこと:平面走査のフレームワーク

ものすごい典型色の強そうな問題! 一瞬、習い覚えた Wavelet Matrix が使えないかと考えてしまった。が、うまく当てはまりそうになかった。

仕方ないので、素直に平面走査の方向で考えることにした。なお、座標平面を  t-x 平面と呼ぶことにする。基本的なフレームワークとしては、点集合  S を管理する。 t 座標が小さい点から順に  S に挿入していく。そして、 S 内部の点の  t 座標が最大値と最小値の差が  W 以上になってしまったら、 t 座標が最小の点を  S から除去する。以降、これを繰り返す。

そして、ある時点での点集合  S について、次の情報を効率よく更新していくことを考えたい。


  • num[x] ← 点集合  S に含まれる、 x 座標が  x 以上  x + D 未満である点の個数
  • mav ← その時点での配列 num の要素の最大値

これらの値を高速に更新できたならば、各時点での mav の値の最大値を出力すればよい。

遅延評価セグメント木の登場

ある点  (t, x) を点集合  S に挿入するときの、nummav の値がどのように変化するかを考えよう。num については、 x - D + 1, x - D + 2, \dots, x の値が 1 だけ増えることとなる。この操作は「区間加算」と呼ばれるものである。そして、mav の値は num の全区間の最大値を取得したい。

また、 S から点  (t, x) を削除するときもほとんど同様だ。num については、 x - D + 1, x - D + 2, \dots, x の値が -1 だけ増えることとなる。

以上をまとめると、次の 2 つのことができるデータ構造があれば解決だ。


  • 配列中のある区間に対して 1 を加算する
  • 配列のある区間内の値の最大値を求める

実はこれは「遅延評価セグメント木」典型である。次の記事の問題が、そのものである。

drken1215.hatenablog.com

この遅延評価セグメント木を用いると、座標値の最大値を  M (\le 2 \times 10^{5}) として、計算量  O(N \log M) でこの問題が解ける。

コード

#include <bits/stdc++.h>
using namespace std;
using pint = pair<int,int>;

// Lazy Segment Tree
template<class Monoid, class Action> struct LazySegmentTree {
    // various function types
    using FuncMonoid = function<Monoid(Monoid, Monoid)>;
    using FuncAction = function<Monoid(Action, Monoid)>;
    using FuncComposition = function<Action(Action, Action)>;

    // core member
    int N;
    FuncMonoid OP;
    FuncAction ACT;
    FuncComposition COMP;
    Monoid IDENTITY_MONOID;
    Action IDENTITY_ACTION;
    
    // inner data
    int log, offset;
    vector<Monoid> dat;
    vector<Action> lazy;
    
    // constructor
    LazySegmentTree() {}
    LazySegmentTree(int n, const FuncMonoid op, const FuncAction act, const FuncComposition comp,
                    const Monoid &identity_monoid, const Action &identity_action) {
        init(n, op, act, comp, identity_monoid, identity_action);
    }
    LazySegmentTree(const vector<Monoid> &v,
                    const FuncMonoid op, const FuncAction act, const FuncComposition comp,
                    const Monoid &identity_monoid, const Action &identity_action) {
        init(v, op, act, comp, identity_monoid, identity_action);
    }
    void init(int n, const FuncMonoid op, const FuncAction act, const FuncComposition comp,
              const Monoid &identity_monoid, const Action &identity_action) {
        N = n, OP = op, ACT = act, COMP = comp;
        IDENTITY_MONOID = identity_monoid, IDENTITY_ACTION = identity_action;
        log = 0, offset = 1;
        while (offset < N) ++log, offset <<= 1;
        dat.assign(offset * 2, IDENTITY_MONOID);
        lazy.assign(offset * 2, IDENTITY_ACTION);
    }
    void init(const vector<Monoid> &v,
              const FuncMonoid op, const FuncAction act, const FuncComposition comp,
              const Monoid &identity_monoid, const Action &identity_action) {
        init((int)v.size(), op, act, comp, identity_monoid, identity_action);
        build(v);
    }
    void build(const vector<Monoid> &v) {
        assert(N == (int)v.size());
        for (int i = 0; i < N; ++i) dat[i + offset] = v[i];
        for (int k = offset - 1; k > 0; --k) pull_dat(k);
    }
    int size() const {
        return N;
    }
    
    // basic functions for lazy segment tree
    void pull_dat(int k) {
        dat[k] = OP(dat[k * 2], dat[k * 2 + 1]);
    }
    void apply_lazy(int k, const Action &f) {
        dat[k] = ACT(f, dat[k]);
        if (k < offset) lazy[k] = COMP(f, lazy[k]);
    }
    void push_lazy(int k) {
        if (lazy[k] == IDENTITY_ACTION) return;
        apply_lazy(k * 2, lazy[k]);
        apply_lazy(k * 2 + 1, lazy[k]);
        lazy[k] = IDENTITY_ACTION;
    }
    void pull_dat_deep(int k) {
        for (int h = 1; h <= log; ++h) pull_dat(k >> h);
    }
    void push_lazy_deep(int k) {
        for (int h = log; h >= 1; --h) push_lazy(k >> h);
    }
    
    // setter and getter, update A[i], i is 0-indexed, O(log N)
    void set(int i, const Monoid &v) {
        assert(0 <= i && i < N);
        int k = i + offset;
        push_lazy_deep(k);
        dat[k] = v;
        pull_dat_deep(k);
    }
    Monoid get(int i) {
        assert(0 <= i && i < N);
        int k = i + offset;
        push_lazy_deep(k);
        return dat[k];
    }
    Monoid operator [] (int i) {
        return get(i);
    }
    
    // apply f for index i
    void apply(int i, const Action &f) {
        assert(0 <= i && i < N);
        int k = i + offset;
        push_lazy_deep(k);
        dat[k] = ACT(f, dat[k]);
        pull_dat_deep(k);
    }
    // apply f for interval [l, r)
    void apply(int l, int r, const Action &f) {
        assert(0 <= l && l <= r && r <= N);
        if (l == r) return;
        l += offset, r += offset;
        for (int h = log; h >= 1; --h) {
            if (((l >> h) << h) != l) push_lazy(l >> h);
            if (((r >> h) << h) != r) push_lazy((r - 1) >> h);
        }
        int original_l = l, original_r = r;
        for (; l < r; l >>= 1, r >>= 1) {
            if (l & 1) apply_lazy(l++, f);
            if (r & 1) apply_lazy(--r, f);
        }
        l = original_l, r = original_r;
        for (int h = 1; h <= log; ++h) {
            if (((l >> h) << h) != l) pull_dat(l >> h);
            if (((r >> h) << h) != r) pull_dat((r - 1) >> h);
        }
    }
    
    // get prod of interval [l, r)
    Monoid prod(int l, int r) {
        assert(0 <= l && l <= r && r <= N);
        if (l == r) return IDENTITY_MONOID;
        l += offset, r += offset;
        for (int h = log; h >= 1; --h) {
            if (((l >> h) << h) != l) push_lazy(l >> h);
            if (((r >> h) << h) != r) push_lazy(r >> h);
        }
        Monoid val_left = IDENTITY_MONOID, val_right = IDENTITY_MONOID;
        for (; l < r; l >>= 1, r >>= 1) {
            if (l & 1) val_left = OP(val_left, dat[l++]);
            if (r & 1) val_right = OP(dat[--r], val_right);
        }
        return OP(val_left, val_right);
    }
    Monoid all_prod() {
        return dat[1];
    }
    
    // get max r that f(get(l, r)) = True (0-indexed), O(log N)
    // f(IDENTITY) need to be True
    int max_right(const function<bool(Monoid)> f, int l = 0) {
        if (l == N) return N;
        l += offset;
        push_lazy_deep(l);
        Monoid sum = IDENTITY_MONOID;
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(OP(sum, dat[l]))) {
                while (l < offset) {
                    push_lazy(l);
                    l = l * 2;
                    if (f(OP(sum, dat[l]))) {
                        sum = OP(sum, dat[l]);
                        ++l;
                    }
                }
                return l - offset;
            }
            sum = OP(sum, dat[l]);
            ++l;
        } while ((l & -l) != l);  // stop if l = 2^e
        return N;
    }

    // get min l that f(get(l, r)) = True (0-indexed), O(log N)
    // f(IDENTITY) need to be True
    int min_left(const function<bool(Monoid)> f, int r = -1) {
        if (r == 0) return 0;
        if (r == -1) r = N;
        r += offset;
        push_lazy_deep(r - 1);
        Monoid sum = IDENTITY_MONOID;
        do {
            --r;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(OP(dat[r], sum))) {
                while (r < offset) {
                    push_lazy(r);
                    r = r * 2 + 1;
                    if (f(OP(dat[r], sum))) {
                        sum = OP(dat[r], sum);
                        --r;
                    }
                }
                return r + 1 - offset;
            }
            sum = OP(dat[r], sum);
        } while ((r & -r) != r);
        return 0;
    }
    
    // debug stream
    friend ostream& operator << (ostream &s, LazySegmentTree seg) {
        for (int i = 0; i < (int)seg.size(); ++i) {
            s << seg[i];
            if (i != (int)seg.size() - 1) s << " ";
        }
        return s;
    }
    
    // dump
    void dump() {
        for (int i = 0; i <= log; ++i) {
            for (int j = (1 << i); j < (1 << (i + 1)); ++j) {
                cout << "{" << dat[j] << "," << lazy[j] << "} ";
            }
            cout << endl;
        }
    }
};

const int MAX = 310000;

int main() {
    int N, D, W;
    cin >> N >> D >> W;
    vector<pint> ps(N);
    for (int i = 0; i < N; ++i) cin >> ps[i].first >> ps[i].second;
    sort(ps.begin(), ps.end());

    const int identity_monoid = 0;
    const int identity_action = 0;
    auto op = [&](int x, int y) { return max(x, y); };
    auto mapping = [&](int f, int x) { return x + f; };
    auto composition = [&](int g, int f) { return f + g; };
    LazySegmentTree<long long, long long> seg(MAX, op, mapping, composition,
                                              identity_monoid, identity_action);
    
    auto add = [&](int i) -> void {
        int left = max(ps[i].second - W + 1, 0);
        int right = ps[i].second + 1;
        seg.apply(left, right, 1);
    };
    auto sub = [&](int i) -> void {
        int left = max(ps[i].second - W + 1, 0);
        int right = ps[i].second + 1;
        seg.apply(left, right, -1);
    };
    
    int res = 0, r = 0;
    for (int l = 0; l < N; ++l) {
        while (r < N && ps[r].first - ps[l].first < D) {
            add(r);
            ++r;
        }
        int num = seg.all_prod();
        res = max(res, num);
        
        sub(l);
    }
    
    cout << res << endl;
}