結果

問題 No.568 じゃんじゃん 落とす 委員会
ユーザー PachicobuePachicobue
提出日時 2017-09-09 01:31:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 223 ms / 1,000 ms
コード長 7,936 bytes
コンパイル時間 2,279 ms
コンパイル使用メモリ 183,940 KB
実行使用メモリ 13,528 KB
最終ジャッジ日時 2024-05-07 16:50:29
合計ジャッジ時間 5,315 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
13,304 KB
testcase_01 AC 59 ms
13,436 KB
testcase_02 AC 11 ms
12,148 KB
testcase_03 AC 11 ms
12,276 KB
testcase_04 AC 11 ms
12,272 KB
testcase_05 AC 10 ms
12,272 KB
testcase_06 AC 11 ms
12,276 KB
testcase_07 AC 11 ms
12,276 KB
testcase_08 AC 11 ms
12,276 KB
testcase_09 AC 10 ms
12,272 KB
testcase_10 AC 10 ms
12,144 KB
testcase_11 AC 11 ms
12,148 KB
testcase_12 AC 55 ms
13,212 KB
testcase_13 AC 206 ms
13,296 KB
testcase_14 AC 175 ms
13,264 KB
testcase_15 AC 223 ms
13,528 KB
testcase_16 AC 156 ms
13,360 KB
testcase_17 AC 54 ms
13,240 KB
testcase_18 AC 178 ms
13,228 KB
testcase_19 AC 183 ms
13,400 KB
testcase_20 AC 112 ms
13,256 KB
testcase_21 AC 223 ms
13,360 KB
testcase_22 AC 62 ms
13,436 KB
testcase_23 AC 11 ms
12,272 KB
testcase_24 AC 10 ms
12,276 KB
testcase_25 AC 10 ms
12,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz=" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = 1e9 + 7;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

struct Student {
    int x;
    int a;
    int b;
    bool operator>(const Student& s) const
    {
        return (a != s.a) ? a > s.a : b > s.b;
    }
};
ostream& operator<<(ostream& os, const Student& s)
{
    os << s.x << " " << s.a << " " << s.b << endl;
    return os;
}

template <typename Base>
class SegmentTree
{
public:
    using BaseAlgebra = Base;
    using AccMonoid = typename BaseAlgebra::AccMonoid;
    using OpMonoid = typename BaseAlgebra::OpMonoid;
    using T = typename BaseAlgebra::T;
    using F = typename BaseAlgebra::OpMonoid::T;

    SegmentTree(const int n) : data_num(n), height(__lg(2 * data_num - 1)), size(1 << (1 + height)), half(size >> 1), value(size, AccMonoid::identity()), action(size, OpMonoid::identity()) { assert(n > 0); }
    SegmentTree(const std::vector<T>& val) : data_num(val.size()), height(__lg(2 * data_num - 1)), size(1 << (1 + height)), half(size >> 1), value(size), action(size, OpMonoid::identity())
    {
        for (int data = 0; data < half; data++) {
            if (data < data_num) {
                value[data + half] = val[data];
            } else {
                value[data + half] = AccMonoid::identity();
            }
        }
        for (int node = half - 1; node >= 1; node--) {
            value[node] = acc(value[2 * node], value[2 * node + 1]);
        }
    }

    T get(const int a) const
    {
        assert(0 <= a and a < data_num);
        return accumulate(a, a + 1);
    }

    void set(const int a, const T& val)
    {
        assert(0 <= a and a < data_num);
        const int node = a + half;
        value[node] = val;
        for (int i = node / 2; i > 0; i /= 2) {
            value[i] = acc(value[2 * i], value[2 * i + 1]);
        }
    }

    T accumulate(const int a, const int b) const  // Accumulate (a,b]
    {
        assert(0 <= a and a < b and b <= data_num);
        return accumulateRec(1, 0, half, a, b);
    }

    void modify(const int a, const int b, const F& f)  // Apply f on (a,b]
    {
        assert(0 <= a and a < b and b <= data_num);
        if (f == OpMonoid::identity()) {
            return;
        }
        modifyRec(1, 0, half, a, b, f);
    }

    void print() const
    {
        // cout << "#VALUE" << endl;
        // for (int i = half; i < size; i++) {
        //     cout << value[i] << " ";
        // }
        // cout << endl;
        // cout << "#ACTION" << endl;
        // for (int i = 1; i < half; i++) {
        //     cout << action[i] << " ";
        // }
        // cout << endl;
    }

private:
    void modifyRec(const int int_index, const int int_left, const int int_right, const int mod_left, const int mod_right, const F& f)
    {
        if (mod_left <= int_left and int_right <= mod_right) {
            value[int_index] = act(f, value[int_index]);
            action[int_index] = compose(f, action[int_index]);
        } else if (int_right <= mod_left or mod_right <= int_left) {
            // Do nothing
        } else {
            modifyRec(2 * int_index, int_left, (int_left + int_right) / 2, 0, half, action[int_index]);
            modifyRec(2 * int_index, int_left, (int_left + int_right) / 2, mod_left, mod_right, f);
            modifyRec(2 * int_index + 1, (int_left + int_right) / 2, int_right, 0, half, action[int_index]);
            modifyRec(2 * int_index + 1, (int_left + int_right) / 2, int_right, mod_left, mod_right, f);
            value[int_index] = acc(value[2 * int_index], value[2 * int_index + 1]);
            action[int_index] = OpMonoid::identity();
        }
    }

    T accumulateRec(const int int_index, const int int_left, const int int_right, const int mod_left, const int mod_right) const
    {
        if (mod_left <= int_left and int_right <= mod_right) {
            return value[int_index];
        } else if (int_right <= mod_left or mod_right <= int_left) {
            return AccMonoid::identity();
        } else {
            return act(action[int_index], acc(accumulateRec(2 * int_index, int_left, (int_left + int_right) / 2, mod_left, mod_right),
                                              accumulateRec(2 * int_index + 1, (int_left + int_right) / 2, int_right, mod_left, mod_right)));
        }
    }

    const int data_num;  // Num of valid data on leaves.
    const int height;
    const int size;
    const int half;
    vector<T> value;   // Tree for value(length: size)
    vector<F> action;  // Tree for action(length: half)
    bool has_lazy;

    const AccMonoid acc{};
    const OpMonoid compose{};
    const BaseAlgebra act{};
};
struct Min_Plus {
    using T = ll;

    struct AccMonoid {
        T operator()(const T& a, const T& b) const { return min(a, b); }
        static constexpr T identity() { return INF<T>; }
    };

    struct OpMonoid {
        using T = ll;
        T operator()(const T& f1, const T& f2) const { return f1 + f2; }
        static constexpr T identity() { return 0; }
    };

    T operator()(const OpMonoid::T& f, const T& x) const { return f + x; }
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    vector<Student> student(N);
    int three = 0;
    int two = 0;
    constexpr int MAX = 100000;
    SegmentTree<Min_Plus> seg{vector<ll>(MAX + 1, 0)};
    SegmentTree<Min_Plus> seg2{vector<ll>(MAX + 1, 0)};
    for (int i = 0; i < N; i++) {
        int X, A, B;
        cin >> X >> A >> B;
        student[i] = Student{X, A, B};
        if (X == 3) {
            two++;
            three++;
        } else if (X == 1) {
            seg.modify(0, B + 1, 1);
        } else if (X == 2) {
            two++;
            seg2.modify(0, B + 1, 1);
        }
    }
    if (two >= M) {
        cout << three << endl;
        return 0;
    }
    sort(student.begin(), student.end(), greater<Student>());
    int pos = 0;
    ll minimum = INF<ll>;
    bool flag = true;
    for (int A = MAX + 1; A >= 0 and pos < N; A--) {
        const int prev_pos = pos;
        while (pos < N and student[pos].a >= A) {
            if (student[pos].x == 2) {
                three++;
                seg2.modify(0, student[pos].b + 1, -1);
            } else if (student[pos].x == 1) {
                two++;
                seg.modify(0, student[pos].b + 1, -1);
                seg2.modify(0, student[pos].b + 1, 1);
            } else if (student[pos].x == 0) {
                seg.modify(0, student[pos].b + 1, 1);
            }
            pos++;
        }
        if (pos == prev_pos) {
            if (pos == 0 and flag) {
                flag = false;
            } else {
                continue;
            }
        }

        const ll target = M - two;
        if (target <= 0) {
            minimum = min(minimum, (ll)three);
            continue;
        } else if (target > seg.get(0)) {
            continue;
        }

        int inf = 0;
        int sup = MAX;
        while (inf < sup) {
            const int mid = (inf + sup) / 2;
            const ll cnt = seg.get(mid);
            if (cnt >= target) {
                if (inf == mid) {
                    break;
                }
                inf = mid;
            } else {
                sup = mid;
            }
        }
        minimum = min(minimum, three + seg2.get(inf));
    }
    cout << minimum << endl;

    return 0;
}
0