結果

問題 No.2422 regisys?
ユーザー drken1215drken1215
提出日時 2023-08-13 12:32:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,909 bytes
コンパイル時間 2,799 ms
コンパイル使用メモリ 220,588 KB
実行使用メモリ 24,672 KB
最終ジャッジ日時 2024-05-01 05:47:29
合計ジャッジ時間 12,225 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 WA -
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 WA -
testcase_16 AC 2 ms
5,376 KB
testcase_17 WA -
testcase_18 AC 1 ms
5,376 KB
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 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 208 ms
22,860 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 198 ms
22,544 KB
testcase_46 WA -
testcase_47 WA -
testcase_48 AC 218 ms
22,912 KB
testcase_49 WA -
testcase_50 AC 180 ms
22,552 KB
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using pint = pair<int, int>;
using pll = pair<long long, long long>;

// Segment Tree
template<class Monoid> struct SegTree {
    using Func = function<Monoid(Monoid, Monoid)>;

    // core member
    int SIZE;
    Func F;
    Monoid IDENTITY;
    
    // data
    int offset;
    vector<Monoid> dat;

    // constructor
    SegTree() {}
    SegTree(int n, const Func &f, const Monoid &identity)
    : SIZE(n), F(f), IDENTITY(identity) {
        offset = 1;
        while (offset < n) offset *= 2;
        dat.assign(offset * 2, IDENTITY);
    }
    void init(int n, const Func &f, const Monoid &identity) {
        SIZE = n;
        F = f;
        IDENTITY = identity;
        offset = 1;
        while (offset < n) offset *= 2;
        dat.assign(offset * 2, IDENTITY);
    }
    int size() const { return SIZE; }
    
    // set, a is 0-indexed //
    // build(): O(N)
    void set(int a, const Monoid &v) { dat[a + offset] = v; }
    void build() {
        for (int k = offset - 1; k > 0; --k)
            dat[k] = F(dat[k*2], dat[k*2+1]);
    }
    void build(const vector<Monoid> &vec) {
        for (int a = 0; a < vec.size() && a + offset < dat.size(); ++a)
            set(a, vec[a]);
        build();
    }
    
    // update a, a is 0-indexed, O(log N)
    void update(int a, const Monoid &v) {
        int k = a + offset;
        dat[k] = v;
        while (k >>= 1) dat[k] = F(dat[k*2], dat[k*2+1]);
    }
    
    // get [a, b), a and b are 0-indexed, O(log N)
    Monoid get(int a, int b) {
        Monoid vleft = IDENTITY, vright = IDENTITY;
        for (int left = a + offset, right = b + offset; left < right;
        left >>= 1, right >>= 1) {
            if (left & 1) vleft = F(vleft, dat[left++]);
            if (right & 1) vright = F(dat[--right], vright);
        }
        return F(vleft, vright);
    }
    Monoid get_all() { return dat[1]; }
    Monoid operator [] (int a) const { return dat[a + offset]; }
    
    // 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 == SIZE) return SIZE;
        l += offset;
        Monoid sum = IDENTITY;
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(F(sum, dat[l]))) {
                while (l < offset) {
                    l = l * 2;
                    if (f(F(sum, dat[l]))) {
                        sum = F(sum, dat[l]);
                        ++l;
                    }
                }
                return l - offset;
            }
            sum = F(sum, dat[l]);
            ++l;
        } while ((l & -l) != l);  // stop if l = 2^e
        return SIZE;
    }

    // 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 = SIZE;
        r += offset;
        Monoid sum = IDENTITY;
        do {
            --r;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(F(dat[r], sum))) {
                while (r < offset) {
                    r = r * 2 + 1;
                    if (f(F(dat[r], sum))) {
                        sum = F(dat[r], sum);
                        --r;
                    }
                }
                return r + 1 - offset;
            }
            sum = F(dat[r], sum);
        } while ((r & -r) != r);
        return 0;
    }
    
    // debug
    friend ostream& operator << (ostream &s, const SegTree &seg) {
        for (int i = 0; i < seg.size(); ++i) {
            s << seg[i];
            if (i != seg.size()-1) s << " ";
        }
        return s;
    }
};

const long long INF = 1LL<<60;
int main() {
    int N, M;
    cin >> N >> M;
    vector<pll> items(N);  // {A[i], B[i]}
    vector<long long> gen, mma;
    for (int i = 0; i < N; ++i) cin >> items[i].first >> items[i].second;
    for (int i = 0; i < M; ++i) {
        long long T, C;
        cin >> T >> C;
        if (T == 0) gen.push_back(C);
        else mma.push_back(C);
    }
    sort(items.begin(), items.end());
    sort(gen.begin(), gen.end());
    sort(mma.begin(), mma.end());
    
    // 特定区間の B の最大値を求めるためのセグ木
    using Node = pair<long long, int>;  // {最大値 or 最小値, index}
    SegTree<Node> seg_max(N, [&](Node a, Node b){ return max(a,b); }, Node(-INF, -1));
    SegTree<Node> seg_min(N, [&](Node a, Node b){ return min(a,b); }, Node(INF, -1));
    for (int i = 0; i < N; ++i) {
        seg_max.set(i, pll(items[i].second, i));
        seg_min.set(i, pll(items[i].second, i));
    }
    seg_max.build(), seg_min.build();
    
    // gen -> mma のそれぞれ所持金が小さい順に見ていく
    int match = 0;
    for (auto v : gen) {
        // まだ購入されていない商品のうち、A[i] <= v である範囲内で B[i] が最大の i を選ぶ
        int right = upper_bound(items.begin(), items.end(), pll(v, INF)) - items.begin();
        auto [max_cost, index] = seg_max.get(0, right);
        
        // 購入済みにする
        if (index == -1) continue;
        ++match;
        seg_max.update(index, pll(-INF, -1));
        seg_min.update(index, pll(INF, -1));
    }
    for (auto v : mma) {
        // まだ購入されていない商品のうち、B[i] が最小 (でなくてもよいが) の i を選ぶ
        auto [min_cost, index] = seg_min.get(0, N);
        
        // 購入済みにする
        if (index == -1) continue;
        ++match;
        seg_max.update(index, pll(-INF, -1));
        seg_min.update(index, pll(INF, -1));
    }
    cout << N - match << endl;
}
0