답안 #568086

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
568086 2022-05-24T16:00:00 Z Cyanmond 새 집 (APIO18_new_home) C++17
57 / 100
5000 ms 302608 KB
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;
using i64 = int64_t;

constexpr int inf = 1000000100;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REP3(i, l, r) for (int i = (l); i < (r); ++i)
#define RVP(i, n) for (int i = (n - 1); i >= 0; --i)

#define ALL(x) (x).begin(), (x).end()

struct PriqueErase {
    priority_queue<int, vector<int>, greater<int>> a, b;

    void push(int x) { a.push(x); }

    void erase(int x) { b.push(x); }

    int top() {
        while (not a.empty()) {
            const int v = a.top();
            if (b.empty()) {
                return v;
            } else if (v == b.top()) {
                a.pop();
                b.pop();
            } else {
                return v;
            }
        }
        return inf;
    }
};

struct RIEPM {
    int n, siz;
    vector<PriqueErase> data;

    RIEPM(int n_) : n(n_) {
        siz = 1;
        while (siz < n) siz <<= 1;
        data.assign(2 * siz, PriqueErase());
    }

    void push(int l, int r, int v) {
        assert(0 <= l and l <= r and r <= n);
        for (l += siz, r += siz; l < r; l >>= 1, r >>= 1) {
            if (l & 1) data[l++].push(v);
            if (r & 1) data[--r].push(v);
        }
    }

    void erase(int l, int r, int v) {
        assert(0 <= l and l <= r and r <= n);
        for (l += siz, r += siz; l < r; l >>= 1, r >>= 1) {
            if (l & 1) data[l++].erase(v);
            if (r & 1) data[--r].erase(v);
        }
    }

    int top(int i) {
        assert(0 <= i and i < n);
        i += siz;
        int res = inf;
        while (i != 0) {
            res = min(res, data[i].top());
            i >>= 1;
        }
        return res;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, K, Q;
    cin >> N >> K >> Q;
    vector<int> X(N), T(N), A(N), B(N);
    REP(i, N) {
        cin >> X[i] >> T[i] >> A[i] >> B[i];
        --T[i];
    }
    vector<int> L(Q), Y(Q);
    REP(i, Q) cin >> L[i] >> Y[i];

    auto reverse_points = [&]() {
        REP(i, N) X[i] = inf - X[i];
        REP(i, Q) L[i] = inf - L[i];
    };

    auto solve = [&]() {
        vector<int> press;
        REP(i, N) press.push_back(X[i]);
        REP(i, Q) press.push_back(L[i]);
        sort(ALL(press));
        press.erase(unique(ALL(press)), press.end());

        auto get_key = [&](int i) { return (int)(lower_bound(ALL(press), i) - press.begin()); };

        const int M = (int)press.size();
        RIEPM hp(M);

        vector<tuple<int, int, int, int>> event;
        REP(i, N) {
            event.push_back({A[i], 0, X[i], T[i]});
            event.push_back({B[i] + 1, 1, X[i], T[i]});
        }
        REP(i, Q) event.push_back({Y[i], 2, L[i], i});
        sort(ALL(event));

        vector<map<int, int>> pd(K);
        vector<int> res(Q, -1);

        int cnt_zero = K;
        for (const auto &[c, qt, p, i] : event) {
            if (qt == 0) {
                if (pd[i].empty()) --cnt_zero;
                auto [itr, vld] = pd[i].insert({p, 1});
                if (not vld) {
                    ++itr->second;
                    continue;
                }
                auto itr_r = itr;
                ++itr_r;
                if (itr_r == pd[i].end()) {
                    hp.push(get_key(p), M, p);
                } else {
                    hp.push(get_key(p), get_key((p + itr_r->first + 2) / 2), p);
                }
                if (itr == pd[i].begin()) continue;
                auto itr_l = itr;
                --itr_l;
                const int l_id = get_key(itr_l->first);
                if (itr_r != pd[i].end()) {
                    hp.erase(l_id, get_key((itr_l->first + itr_r->first + 2) / 2), itr_l->first);
                } else {
                    hp.erase(l_id, M, itr_l->first);
                }
                hp.push(l_id, get_key((itr_l->first + p + 2) / 2), itr_l->first);
            }
            if (qt == 1) {
                auto itr = pd[i].find(p);
                if (--(itr->second) != 0) {
                    continue;
                }
                pd[i].erase(p);
                if (pd[i].empty()) ++cnt_zero;
                auto itr_r = pd[i].upper_bound(p);
                if (itr_r != pd[i].end()) {
                    hp.erase(get_key(p), get_key((p + itr_r->first + 2) / 2), p);
                } else {
                    hp.erase(get_key(p), M, p);
                }
                if (itr_r == pd[i].begin()) continue;
                auto itr_l = itr_r;
                --itr_l;
                const int l_id = get_key(itr_l->first);
                hp.erase(l_id, get_key((itr_l->first + p + 2) / 2), itr_l->first);
                if (itr_r != pd[i].end()) {
                    hp.push(l_id, get_key((itr_l->first + itr_r->first + 2) / 2), itr_l->first);
                } else {
                    hp.push(l_id, M, itr_l->first);
                }
            }
            if (qt == 2) {
                if (cnt_zero != 0) continue;
                const int pi = get_key(p);
                const int vi = hp.top(pi);
                if (vi == inf) continue;
                res[i] = p - vi;
            }
        }

        return res;
    };

    const auto res1 = solve();
    reverse_points();
    const auto res2 = solve();
    REP(i, Q) {
        const int ans = max(res1[i], res2[i]);
        cout << ans << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 288 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 3 ms 592 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 3 ms 596 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 3 ms 592 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 3 ms 592 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 2 ms 592 KB Output is correct
15 Correct 3 ms 592 KB Output is correct
16 Correct 3 ms 592 KB Output is correct
17 Correct 3 ms 592 KB Output is correct
18 Correct 3 ms 592 KB Output is correct
19 Correct 2 ms 592 KB Output is correct
20 Correct 3 ms 592 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 592 KB Output is correct
23 Correct 2 ms 592 KB Output is correct
24 Correct 3 ms 592 KB Output is correct
25 Correct 4 ms 592 KB Output is correct
26 Correct 3 ms 592 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 2 ms 592 KB Output is correct
29 Correct 3 ms 592 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 288 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 3 ms 592 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 3 ms 596 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 3 ms 592 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 3 ms 592 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 2 ms 592 KB Output is correct
15 Correct 3 ms 592 KB Output is correct
16 Correct 3 ms 592 KB Output is correct
17 Correct 3 ms 592 KB Output is correct
18 Correct 3 ms 592 KB Output is correct
19 Correct 2 ms 592 KB Output is correct
20 Correct 3 ms 592 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 592 KB Output is correct
23 Correct 2 ms 592 KB Output is correct
24 Correct 3 ms 592 KB Output is correct
25 Correct 4 ms 592 KB Output is correct
26 Correct 3 ms 592 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 2 ms 592 KB Output is correct
29 Correct 3 ms 592 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
31 Correct 1456 ms 45348 KB Output is correct
32 Correct 83 ms 9568 KB Output is correct
33 Correct 1104 ms 38248 KB Output is correct
34 Correct 1478 ms 45332 KB Output is correct
35 Correct 1374 ms 42692 KB Output is correct
36 Correct 1085 ms 36896 KB Output is correct
37 Correct 775 ms 38928 KB Output is correct
38 Correct 631 ms 34732 KB Output is correct
39 Correct 559 ms 36884 KB Output is correct
40 Correct 518 ms 34928 KB Output is correct
41 Correct 1045 ms 39756 KB Output is correct
42 Correct 1008 ms 41856 KB Output is correct
43 Correct 81 ms 9476 KB Output is correct
44 Correct 1004 ms 38912 KB Output is correct
45 Correct 994 ms 37784 KB Output is correct
46 Correct 889 ms 35572 KB Output is correct
47 Correct 423 ms 35540 KB Output is correct
48 Correct 480 ms 37200 KB Output is correct
49 Correct 577 ms 39224 KB Output is correct
50 Correct 588 ms 40664 KB Output is correct
51 Correct 619 ms 38712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4521 ms 290472 KB Output is correct
2 Correct 3213 ms 252876 KB Output is correct
3 Correct 1791 ms 248900 KB Output is correct
4 Correct 4115 ms 286020 KB Output is correct
5 Correct 2799 ms 248548 KB Output is correct
6 Correct 3034 ms 247592 KB Output is correct
7 Correct 1714 ms 253452 KB Output is correct
8 Correct 4233 ms 284444 KB Output is correct
9 Correct 4782 ms 287068 KB Output is correct
10 Correct 4136 ms 277816 KB Output is correct
11 Correct 3290 ms 256896 KB Output is correct
12 Correct 3661 ms 261064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5068 ms 302608 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 288 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 3 ms 592 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 3 ms 596 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 3 ms 592 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 3 ms 592 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 2 ms 592 KB Output is correct
15 Correct 3 ms 592 KB Output is correct
16 Correct 3 ms 592 KB Output is correct
17 Correct 3 ms 592 KB Output is correct
18 Correct 3 ms 592 KB Output is correct
19 Correct 2 ms 592 KB Output is correct
20 Correct 3 ms 592 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 592 KB Output is correct
23 Correct 2 ms 592 KB Output is correct
24 Correct 3 ms 592 KB Output is correct
25 Correct 4 ms 592 KB Output is correct
26 Correct 3 ms 592 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 2 ms 592 KB Output is correct
29 Correct 3 ms 592 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
31 Correct 1456 ms 45348 KB Output is correct
32 Correct 83 ms 9568 KB Output is correct
33 Correct 1104 ms 38248 KB Output is correct
34 Correct 1478 ms 45332 KB Output is correct
35 Correct 1374 ms 42692 KB Output is correct
36 Correct 1085 ms 36896 KB Output is correct
37 Correct 775 ms 38928 KB Output is correct
38 Correct 631 ms 34732 KB Output is correct
39 Correct 559 ms 36884 KB Output is correct
40 Correct 518 ms 34928 KB Output is correct
41 Correct 1045 ms 39756 KB Output is correct
42 Correct 1008 ms 41856 KB Output is correct
43 Correct 81 ms 9476 KB Output is correct
44 Correct 1004 ms 38912 KB Output is correct
45 Correct 994 ms 37784 KB Output is correct
46 Correct 889 ms 35572 KB Output is correct
47 Correct 423 ms 35540 KB Output is correct
48 Correct 480 ms 37200 KB Output is correct
49 Correct 577 ms 39224 KB Output is correct
50 Correct 588 ms 40664 KB Output is correct
51 Correct 619 ms 38712 KB Output is correct
52 Correct 446 ms 40600 KB Output is correct
53 Correct 462 ms 38960 KB Output is correct
54 Correct 857 ms 48376 KB Output is correct
55 Correct 774 ms 39668 KB Output is correct
56 Correct 697 ms 39544 KB Output is correct
57 Correct 922 ms 40640 KB Output is correct
58 Correct 813 ms 41880 KB Output is correct
59 Correct 690 ms 41096 KB Output is correct
60 Correct 921 ms 41392 KB Output is correct
61 Correct 90 ms 11388 KB Output is correct
62 Correct 432 ms 39536 KB Output is correct
63 Correct 715 ms 44688 KB Output is correct
64 Correct 822 ms 44924 KB Output is correct
65 Correct 992 ms 45492 KB Output is correct
66 Correct 1014 ms 40116 KB Output is correct
67 Correct 188 ms 10344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 288 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 3 ms 592 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 3 ms 596 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 3 ms 592 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 3 ms 592 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 2 ms 592 KB Output is correct
15 Correct 3 ms 592 KB Output is correct
16 Correct 3 ms 592 KB Output is correct
17 Correct 3 ms 592 KB Output is correct
18 Correct 3 ms 592 KB Output is correct
19 Correct 2 ms 592 KB Output is correct
20 Correct 3 ms 592 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 592 KB Output is correct
23 Correct 2 ms 592 KB Output is correct
24 Correct 3 ms 592 KB Output is correct
25 Correct 4 ms 592 KB Output is correct
26 Correct 3 ms 592 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 2 ms 592 KB Output is correct
29 Correct 3 ms 592 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
31 Correct 1456 ms 45348 KB Output is correct
32 Correct 83 ms 9568 KB Output is correct
33 Correct 1104 ms 38248 KB Output is correct
34 Correct 1478 ms 45332 KB Output is correct
35 Correct 1374 ms 42692 KB Output is correct
36 Correct 1085 ms 36896 KB Output is correct
37 Correct 775 ms 38928 KB Output is correct
38 Correct 631 ms 34732 KB Output is correct
39 Correct 559 ms 36884 KB Output is correct
40 Correct 518 ms 34928 KB Output is correct
41 Correct 1045 ms 39756 KB Output is correct
42 Correct 1008 ms 41856 KB Output is correct
43 Correct 81 ms 9476 KB Output is correct
44 Correct 1004 ms 38912 KB Output is correct
45 Correct 994 ms 37784 KB Output is correct
46 Correct 889 ms 35572 KB Output is correct
47 Correct 423 ms 35540 KB Output is correct
48 Correct 480 ms 37200 KB Output is correct
49 Correct 577 ms 39224 KB Output is correct
50 Correct 588 ms 40664 KB Output is correct
51 Correct 619 ms 38712 KB Output is correct
52 Correct 4521 ms 290472 KB Output is correct
53 Correct 3213 ms 252876 KB Output is correct
54 Correct 1791 ms 248900 KB Output is correct
55 Correct 4115 ms 286020 KB Output is correct
56 Correct 2799 ms 248548 KB Output is correct
57 Correct 3034 ms 247592 KB Output is correct
58 Correct 1714 ms 253452 KB Output is correct
59 Correct 4233 ms 284444 KB Output is correct
60 Correct 4782 ms 287068 KB Output is correct
61 Correct 4136 ms 277816 KB Output is correct
62 Correct 3290 ms 256896 KB Output is correct
63 Correct 3661 ms 261064 KB Output is correct
64 Execution timed out 5068 ms 302608 KB Time limit exceeded
65 Halted 0 ms 0 KB -