結果

問題 No.2667 Constrained Permutation
ユーザー SnowBeenDidingSnowBeenDiding
提出日時 2024-03-09 07:42:02
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,846 bytes
コンパイル時間 3,808 ms
コンパイル使用メモリ 265,812 KB
実行使用メモリ 9,572 KB
最終ジャッジ日時 2024-03-09 07:42:14
合計ジャッジ時間 11,555 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 146 ms
6,676 KB
testcase_29 AC 139 ms
6,676 KB
testcase_30 AC 156 ms
6,676 KB
testcase_31 AC 95 ms
6,676 KB
testcase_32 AC 21 ms
6,676 KB
testcase_33 AC 214 ms
9,372 KB
testcase_34 AC 117 ms
6,676 KB
testcase_35 AC 135 ms
6,780 KB
testcase_36 AC 155 ms
6,732 KB
testcase_37 AC 145 ms
6,980 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 119 ms
6,676 KB
testcase_41 AC 184 ms
9,004 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 75 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], r[i]);
    sort(v.begin(), v.end());
    rep(i, 0, l.size()) {
        int mx = rs.mex(l[i]);
        if (mx > r[i]) 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