結果

問題 No.2667 Constrained Permutation
ユーザー SnowBeenDidingSnowBeenDiding
提出日時 2024-03-09 07:43:52
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 245 ms / 2,000 ms
コード長 4,885 bytes
コンパイル時間 3,318 ms
コンパイル使用メモリ 265,832 KB
実行使用メモリ 9,572 KB
最終ジャッジ日時 2024-03-09 07:44:03
合計ジャッジ時間 10,528 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 3 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 4 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 3 ms
6,676 KB
testcase_09 AC 3 ms
6,676 KB
testcase_10 AC 3 ms
6,676 KB
testcase_11 AC 3 ms
6,676 KB
testcase_12 AC 78 ms
6,676 KB
testcase_13 AC 4 ms
6,676 KB
testcase_14 AC 210 ms
9,340 KB
testcase_15 AC 194 ms
8,828 KB
testcase_16 AC 127 ms
6,676 KB
testcase_17 AC 40 ms
6,676 KB
testcase_18 AC 225 ms
9,572 KB
testcase_19 AC 245 ms
9,572 KB
testcase_20 AC 244 ms
9,572 KB
testcase_21 AC 236 ms
9,572 KB
testcase_22 AC 234 ms
9,572 KB
testcase_23 AC 235 ms
9,572 KB
testcase_24 AC 90 ms
6,676 KB
testcase_25 AC 172 ms
9,004 KB
testcase_26 AC 217 ms
9,572 KB
testcase_27 AC 210 ms
9,572 KB
testcase_28 AC 146 ms
6,676 KB
testcase_29 AC 139 ms
6,676 KB
testcase_30 AC 158 ms
6,676 KB
testcase_31 AC 96 ms
6,676 KB
testcase_32 AC 21 ms
6,676 KB
testcase_33 AC 221 ms
9,372 KB
testcase_34 AC 119 ms
6,676 KB
testcase_35 AC 137 ms
6,780 KB
testcase_36 AC 133 ms
6,732 KB
testcase_37 AC 145 ms
6,980 KB
testcase_38 AC 178 ms
8,780 KB
testcase_39 AC 4 ms
6,676 KB
testcase_40 AC 115 ms
6,676 KB
testcase_41 AC 185 ms
9,004 KB
testcase_42 AC 122 ms
6,676 KB
testcase_43 AC 177 ms
8,868 KB
testcase_44 AC 73 ms
6,676 KB
testcase_45 AC 4 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
using namespace std;

typedef long long ll;

int f(vector<int> a) {
    sort(a.begin(), a.end());
    int ret = 0;
    rep(i, 0, a.size()) ret = max(ret, a[i] - i);
    return ret;
}

int g(vector<int> a) {
    sort(a.begin(), a.end());
    int ret = 2e9;
    rep(i, 0, a.size()) ret = min(ret, a[i] - i);
    return ret;
}

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,~)
    int 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;
    }
    // mexr (~ , x]
    int mexr(T x = 0) {
        auto ite = prev(st.lower_bound({x + 1, x + 1}));
        if (ite->first <= x and x <= ite->second)
            return ite->first - 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";
    }
};

bool check(int x, vector<int> l, vector<int> r) {
    rep(i, 0, l.size()) {
        l[i] -= x;
        r[i] -= x;
        l[i] = max(0, l[i]);
        r[i] = min((int)l.size() - 1, r[i]);
        if (l[i] > r[i]) return false;
    }
    RangeSet<int> rs;
    vector<pair<int, int>> v;
    rep(i, 0, l.size()) v.emplace_back(r[i], l[i]);
    sort(v.begin(), v.end());
    rep(i, 0, l.size()) {
        int L = v[i].second, R = v[i].first;
        int mx = rs.mex(L);
        if (mx > R) return false;
        rs.insert(mx);
    }
    return true;
}

int main() {
    int n;
    cin >> n;
    vector<int> l(n), r(n);
    rep(i, 0, n) cin >> l[i] >> r[i], l[i]--, r[i]--;
    int x = f(l), y = g(r);
    if (check(x, l, r))
        cout << y - x + 1 << endl;
    else
        cout << 0 << endl;
}
0