提出 #71508811


ソースコード 拡げる

#include <bits/stdc++.h>

#include <atcoder/all>

using namespace std;
using namespace atcoder;

#define int long long
#define ALL(x) (x).begin(), (x).end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

typedef pair<int, int> PI;
typedef pair<int, pair<int, int>> PII;
static const int INF = 1010000000000000017LL;
static const double eps = 1e-12;
static const double pi = 3.14159265358979323846;
static const int dx[4] = {1, -1, 0, 0};
static const int dy[4] = {0, 0, 1, -1};
static const int ddx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
static const int ddy[8] = {0, 0, 1, -1, 1, -1, 1, -1};

template <class T>
inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

// 閉区間の範囲を管理
template <typename T>
struct RangeSet {
    set<pair<T, T>> st;
    T TINF;

    RangeSet() {
        TINF = numeric_limits<T>::max() / 2;
        st.emplace(TINF, TINF);
        st.emplace(-TINF, -TINF);
    }
    // [l,r] covered?
    bool covered(T l, T r) {
        assert(l <= r);
        auto ite = prev(st.lower_bound({l + 1, l + 1}));
        return ite->first <= l and r <= ite->second;
    }
    bool covered(T x) { return covered(x, x); }
    // [l, r]がカバーされているなら,その区間を返す. されていないなら[-TINF,-TINF]を返す
    pair<T, T> covered_by(T l, T r) {
        assert(l <= r);
        auto ite = prev(st.lower_bound({l + 1, l + 1}));
        if (ite->first <= l and r <= ite->second) return *ite;
        return make_pair(-TINF, -TINF);
    }
    pair<T, T> covered_by(T x) { return covered_by(x, x); }
    // insert[l,r], 増加量を返す
    T insert(T l, T r) {
        assert(l <= r);
        auto ite = prev(st.lower_bound({l + 1, l + 1}));
        if (ite->first <= l and r <= ite->second) return T(0);
        T sum_erased = T(0);
        if (ite->first <= l and l <= ite->second + 1) {
            l = ite->first;
            sum_erased += ite->second - ite->first + 1;
            ite = st.erase(ite);
        } else
            ite = next(ite);
        while (r > ite->second) {
            sum_erased += ite->second - ite->first + 1;
            ite = st.erase(ite);
        }
        if (ite->first - 1 <= r and r <= ite->second) {
            sum_erased += ite->second - ite->first + 1;
            r = ite->second;
            st.erase(ite);
        }
        st.emplace(l, r);
        return r - l + 1 - sum_erased;
    }
    T insert(T x) { return insert(x, x); }
    // erase [l,r], 減少量を返す
    T erase(T l, T r) {
        assert(l <= r);
        auto ite = prev(st.lower_bound({l + 1, l + 1}));
        if (ite->first <= l and r <= ite->second) {
            // 完全に1つの区間に包含されている
            if (ite->first < l) st.emplace(ite->first, l - 1);
            if (r < ite->second) st.emplace(r + 1, ite->second);
            st.erase(ite);
            return r - l + 1;
        }

        T ret = T(0);
        if (ite->first <= l and l <= ite->second) {
            ret += ite->second - l + 1;  // 消えた
            if (ite->first < l) st.emplace(ite->first, l - 1);
            ite = st.erase(ite);  // 次へ
        } else
            ite = next(ite);
        while (ite->second <= r) {
            ret += ite->second - ite->first + 1;
            ite = st.erase(ite);
        }
        // 右端が区間の間にあるか
        if (ite->first <= r and r <= ite->second) {
            ret += r - ite->first + 1;
            if (r < ite->second) st.emplace(r + 1, ite->second);
            st.erase(ite);
        }
        return ret;
    }
    T erase(T x) { return erase(x, x); }
    // number of range
    int size() { return (int)st.size() - 2; }
    // mex [x,~)
    T mex(T x = 0) {
        auto ite = prev(st.lower_bound({x + 1, x + 1}));
        if (ite->first <= x and x <= ite->second)
            return ite->second + 1;
        else
            return x;
    }
    void output() {
        cout << "RangeSet : ";
        for (auto& p : st) {
            if (p.first == -TINF or p.second == TINF) continue;
            cout << "[" << p.first << ", " << p.second << "] ";
        }
        cout << "\n";
    }
};

signed main() {
    int N, Q;
    cin >> N >> Q;
    RangeSet<int> rs;
    int cnt = 0;
    while (Q--) {
        int l, r;
        cin >> l >> r;
        l--, r--;
        cnt += rs.insert(l, r);
        cout << N - cnt << endl;
    }
}

提出情報

提出日時
問題 E - Cover query
ユーザ tsuyosshi
言語 C++23 (GCC 15.2.0)
得点 450
コード長 4738 Byte
結果 AC
実行時間 237 ms
メモリ 16124 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 450 / 450
結果
AC × 2
AC × 42
セット名 テストケース
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt
ケース名 結果 実行時間 メモリ
example_00.txt AC 1 ms 3424 KiB
example_01.txt AC 1 ms 3404 KiB
hand_00.txt AC 230 ms 15984 KiB
hand_01.txt AC 173 ms 3472 KiB
hand_02.txt AC 173 ms 3388 KiB
hand_03.txt AC 172 ms 3588 KiB
hand_04.txt AC 226 ms 16124 KiB
hand_05.txt AC 204 ms 16072 KiB
hand_06.txt AC 200 ms 9840 KiB
hand_07.txt AC 137 ms 3532 KiB
hand_08.txt AC 104 ms 3532 KiB
hand_09.txt AC 173 ms 3404 KiB
hand_10.txt AC 101 ms 3556 KiB
hand_11.txt AC 102 ms 3540 KiB
random_00.txt AC 165 ms 3588 KiB
random_01.txt AC 164 ms 3388 KiB
random_02.txt AC 164 ms 3588 KiB
random_03.txt AC 164 ms 3532 KiB
random_04.txt AC 164 ms 3440 KiB
random_05.txt AC 237 ms 8048 KiB
random_06.txt AC 236 ms 8096 KiB
random_07.txt AC 237 ms 8080 KiB
random_08.txt AC 236 ms 8148 KiB
random_09.txt AC 237 ms 8148 KiB
random_10.txt AC 164 ms 3540 KiB
random_11.txt AC 165 ms 3440 KiB
random_12.txt AC 165 ms 3592 KiB
random_13.txt AC 165 ms 3392 KiB
random_14.txt AC 166 ms 3424 KiB
random_15.txt AC 165 ms 3440 KiB
random_16.txt AC 165 ms 3588 KiB
random_17.txt AC 165 ms 3588 KiB
random_18.txt AC 166 ms 3440 KiB
random_19.txt AC 165 ms 3472 KiB
random_20.txt AC 205 ms 5116 KiB
random_21.txt AC 205 ms 5192 KiB
random_22.txt AC 205 ms 5252 KiB
random_23.txt AC 206 ms 5372 KiB
random_24.txt AC 205 ms 5136 KiB
random_25.txt AC 164 ms 3472 KiB
random_26.txt AC 164 ms 3592 KiB
random_27.txt AC 164 ms 3524 KiB