結果

問題 No.2422 regisys?
ユーザー drken1215drken1215
提出日時 2023-08-13 15:00:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 7,212 bytes
コンパイル時間 2,853 ms
コンパイル使用メモリ 220,776 KB
実行使用メモリ 24,672 KB
最終ジャッジ日時 2024-11-21 10:43:20
合計ジャッジ時間 11,934 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
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 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 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
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 WA -
権限があれば一括ダウンロードができます

ソースコード

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;
    }
};



#define REP(i, n) for (long long i = 0; i < (long long)(n); ++i)
#define REP2(i, a, b) for (long long i = a; i < (long long)(b); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, deque<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s; }
template<class T> ostream& operator << (ostream &s, multiset<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } 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));
    }
    COUT(match);
    for (auto v : mma) {
        // まだ購入されていない商品のうち、B[i] <= v であるような i を選ぶ (ここでは最小をとる)
        auto [min_cost, index] = seg_min.get(0, N);
        
        // 購入済みにする
        if (index == -1 || v < min_cost) continue;
        ++match;
        seg_max.update(index, pll(-INF, -1));
        seg_min.update(index, pll(INF, -1));
    }
    COUT(match);
    cout << N - match << endl;
}
0