Submission #569115

# Submission time Handle Problem Language Result Execution time Memory
569115 2022-05-26T17:44:40 Z Cyanmond New Home (APIO18_new_home) C++17
80 / 100
5000 ms 132880 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;
    }

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

int cnt_sp[2000000];

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

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

    void push(int l, int r) {
        assert(0 <= l and l <= r and r <= n);
        --r;
        data[r].push(l);
        const int v = data[r].top();
        r += siz;
        node[r] = v;
        while (r != 1) {
            r >>= 1;
            node[r] = min(node[2 * r], node[2 * r + 1]);
        }
    }

    void erase(int l, int r) {
        assert(0 <= l and l <= r and r <= n);
        --r;
        data[r].erase(l);
        const int v = data[r].empty() ? inf : data[r].top();
        r += siz;
        node[r] = v;
        while (r != 1) {
            r >>= 1;
            node[r] = min(node[2 * r], node[2 * r + 1]);
        }
    }

    int fold(int l, int r) {
        int res = inf;
        for (l += siz, r += siz; l < r; l >>= 1, r >>= 1) {
            if (l & 1) res = min(res, node[l++]);
            if (r & 1) res = min(res, node[--r]);
        }
        return res;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    fill(cnt_sp, cnt_sp + 2000000, 0);

    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);
                } else {
                    hp.push(get_key(p), get_key((p + itr_r->first + 2) / 2));
                }
                if (itr == pd[i].begin()) continue;
                auto itr_l = itr;
                --itr_l;
                const int l_id = get_key(itr_l->first);
                hp.push(l_id, get_key((itr_l->first + p + 2) / 2));
                if (itr_r != pd[i].end()) {
                    hp.erase(l_id, get_key((itr_l->first + itr_r->first + 2) / 2));
                } else {
                    hp.erase(l_id, M);
                }
            }
            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));
                } else {
                    hp.erase(get_key(p), M);
                }
                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));
                if (itr_r != pd[i].end()) {
                    hp.push(l_id, get_key((itr_l->first + itr_r->first + 2) / 2));
                } else {
                    hp.push(l_id, M);
                }
            }
            if (qt == 2) {
                if (cnt_zero != 0) continue;
                const int pi = get_key(p);
                const int vi = hp.fold(pi, M);
                if (vi == inf) continue;
                res[i] = p - press[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';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 3 ms 8116 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 5 ms 8148 KB Output is correct
6 Correct 5 ms 8308 KB Output is correct
7 Correct 4 ms 8284 KB Output is correct
8 Correct 5 ms 8276 KB Output is correct
9 Correct 4 ms 8276 KB Output is correct
10 Correct 5 ms 8288 KB Output is correct
11 Correct 4 ms 8276 KB Output is correct
12 Correct 5 ms 8276 KB Output is correct
13 Correct 5 ms 8292 KB Output is correct
14 Correct 4 ms 8184 KB Output is correct
15 Correct 5 ms 8300 KB Output is correct
16 Correct 5 ms 8288 KB Output is correct
17 Correct 5 ms 8264 KB Output is correct
18 Correct 5 ms 8276 KB Output is correct
19 Correct 5 ms 8284 KB Output is correct
20 Correct 5 ms 8276 KB Output is correct
21 Correct 4 ms 8148 KB Output is correct
22 Correct 5 ms 8276 KB Output is correct
23 Correct 5 ms 8292 KB Output is correct
24 Correct 6 ms 8276 KB Output is correct
25 Correct 5 ms 8272 KB Output is correct
26 Correct 5 ms 8276 KB Output is correct
27 Correct 5 ms 8192 KB Output is correct
28 Correct 5 ms 8288 KB Output is correct
29 Correct 5 ms 8276 KB Output is correct
30 Correct 4 ms 8276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 3 ms 8116 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 5 ms 8148 KB Output is correct
6 Correct 5 ms 8308 KB Output is correct
7 Correct 4 ms 8284 KB Output is correct
8 Correct 5 ms 8276 KB Output is correct
9 Correct 4 ms 8276 KB Output is correct
10 Correct 5 ms 8288 KB Output is correct
11 Correct 4 ms 8276 KB Output is correct
12 Correct 5 ms 8276 KB Output is correct
13 Correct 5 ms 8292 KB Output is correct
14 Correct 4 ms 8184 KB Output is correct
15 Correct 5 ms 8300 KB Output is correct
16 Correct 5 ms 8288 KB Output is correct
17 Correct 5 ms 8264 KB Output is correct
18 Correct 5 ms 8276 KB Output is correct
19 Correct 5 ms 8284 KB Output is correct
20 Correct 5 ms 8276 KB Output is correct
21 Correct 4 ms 8148 KB Output is correct
22 Correct 5 ms 8276 KB Output is correct
23 Correct 5 ms 8292 KB Output is correct
24 Correct 6 ms 8276 KB Output is correct
25 Correct 5 ms 8272 KB Output is correct
26 Correct 5 ms 8276 KB Output is correct
27 Correct 5 ms 8192 KB Output is correct
28 Correct 5 ms 8288 KB Output is correct
29 Correct 5 ms 8276 KB Output is correct
30 Correct 4 ms 8276 KB Output is correct
31 Correct 601 ms 32472 KB Output is correct
32 Correct 86 ms 18156 KB Output is correct
33 Correct 514 ms 31276 KB Output is correct
34 Correct 531 ms 32516 KB Output is correct
35 Correct 593 ms 32676 KB Output is correct
36 Correct 592 ms 32940 KB Output is correct
37 Correct 427 ms 31932 KB Output is correct
38 Correct 405 ms 31612 KB Output is correct
39 Correct 344 ms 30716 KB Output is correct
40 Correct 361 ms 30692 KB Output is correct
41 Correct 346 ms 30292 KB Output is correct
42 Correct 320 ms 30124 KB Output is correct
43 Correct 77 ms 19436 KB Output is correct
44 Correct 331 ms 30452 KB Output is correct
45 Correct 342 ms 30384 KB Output is correct
46 Correct 316 ms 29868 KB Output is correct
47 Correct 201 ms 28984 KB Output is correct
48 Correct 211 ms 29368 KB Output is correct
49 Correct 238 ms 29220 KB Output is correct
50 Correct 264 ms 29400 KB Output is correct
51 Correct 260 ms 30268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1934 ms 123232 KB Output is correct
2 Correct 1362 ms 114624 KB Output is correct
3 Correct 1010 ms 125692 KB Output is correct
4 Correct 1894 ms 124736 KB Output is correct
5 Correct 1316 ms 117376 KB Output is correct
6 Correct 1393 ms 114868 KB Output is correct
7 Correct 1034 ms 125748 KB Output is correct
8 Correct 1784 ms 124452 KB Output is correct
9 Correct 2045 ms 122328 KB Output is correct
10 Correct 1654 ms 114976 KB Output is correct
11 Correct 1299 ms 116596 KB Output is correct
12 Correct 1453 ms 117840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3275 ms 126116 KB Output is correct
2 Correct 388 ms 53484 KB Output is correct
3 Correct 2931 ms 125180 KB Output is correct
4 Correct 1143 ms 132340 KB Output is correct
5 Correct 2381 ms 130464 KB Output is correct
6 Correct 2091 ms 125916 KB Output is correct
7 Correct 2672 ms 121524 KB Output is correct
8 Correct 2963 ms 121256 KB Output is correct
9 Correct 1229 ms 130948 KB Output is correct
10 Correct 2713 ms 128560 KB Output is correct
11 Correct 3529 ms 129064 KB Output is correct
12 Correct 3757 ms 127348 KB Output is correct
13 Correct 1737 ms 115720 KB Output is correct
14 Correct 1642 ms 112832 KB Output is correct
15 Correct 1978 ms 114236 KB Output is correct
16 Correct 2214 ms 116516 KB Output is correct
17 Correct 1991 ms 116388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 3 ms 8116 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 5 ms 8148 KB Output is correct
6 Correct 5 ms 8308 KB Output is correct
7 Correct 4 ms 8284 KB Output is correct
8 Correct 5 ms 8276 KB Output is correct
9 Correct 4 ms 8276 KB Output is correct
10 Correct 5 ms 8288 KB Output is correct
11 Correct 4 ms 8276 KB Output is correct
12 Correct 5 ms 8276 KB Output is correct
13 Correct 5 ms 8292 KB Output is correct
14 Correct 4 ms 8184 KB Output is correct
15 Correct 5 ms 8300 KB Output is correct
16 Correct 5 ms 8288 KB Output is correct
17 Correct 5 ms 8264 KB Output is correct
18 Correct 5 ms 8276 KB Output is correct
19 Correct 5 ms 8284 KB Output is correct
20 Correct 5 ms 8276 KB Output is correct
21 Correct 4 ms 8148 KB Output is correct
22 Correct 5 ms 8276 KB Output is correct
23 Correct 5 ms 8292 KB Output is correct
24 Correct 6 ms 8276 KB Output is correct
25 Correct 5 ms 8272 KB Output is correct
26 Correct 5 ms 8276 KB Output is correct
27 Correct 5 ms 8192 KB Output is correct
28 Correct 5 ms 8288 KB Output is correct
29 Correct 5 ms 8276 KB Output is correct
30 Correct 4 ms 8276 KB Output is correct
31 Correct 601 ms 32472 KB Output is correct
32 Correct 86 ms 18156 KB Output is correct
33 Correct 514 ms 31276 KB Output is correct
34 Correct 531 ms 32516 KB Output is correct
35 Correct 593 ms 32676 KB Output is correct
36 Correct 592 ms 32940 KB Output is correct
37 Correct 427 ms 31932 KB Output is correct
38 Correct 405 ms 31612 KB Output is correct
39 Correct 344 ms 30716 KB Output is correct
40 Correct 361 ms 30692 KB Output is correct
41 Correct 346 ms 30292 KB Output is correct
42 Correct 320 ms 30124 KB Output is correct
43 Correct 77 ms 19436 KB Output is correct
44 Correct 331 ms 30452 KB Output is correct
45 Correct 342 ms 30384 KB Output is correct
46 Correct 316 ms 29868 KB Output is correct
47 Correct 201 ms 28984 KB Output is correct
48 Correct 211 ms 29368 KB Output is correct
49 Correct 238 ms 29220 KB Output is correct
50 Correct 264 ms 29400 KB Output is correct
51 Correct 260 ms 30268 KB Output is correct
52 Correct 213 ms 31492 KB Output is correct
53 Correct 238 ms 29784 KB Output is correct
54 Correct 383 ms 33216 KB Output is correct
55 Correct 334 ms 31996 KB Output is correct
56 Correct 302 ms 31656 KB Output is correct
57 Correct 385 ms 30860 KB Output is correct
58 Correct 324 ms 30768 KB Output is correct
59 Correct 297 ms 31132 KB Output is correct
60 Correct 349 ms 30676 KB Output is correct
61 Correct 97 ms 21624 KB Output is correct
62 Correct 218 ms 31592 KB Output is correct
63 Correct 314 ms 31784 KB Output is correct
64 Correct 339 ms 32208 KB Output is correct
65 Correct 416 ms 30896 KB Output is correct
66 Correct 387 ms 30576 KB Output is correct
67 Correct 205 ms 19160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 3 ms 8116 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 5 ms 8148 KB Output is correct
6 Correct 5 ms 8308 KB Output is correct
7 Correct 4 ms 8284 KB Output is correct
8 Correct 5 ms 8276 KB Output is correct
9 Correct 4 ms 8276 KB Output is correct
10 Correct 5 ms 8288 KB Output is correct
11 Correct 4 ms 8276 KB Output is correct
12 Correct 5 ms 8276 KB Output is correct
13 Correct 5 ms 8292 KB Output is correct
14 Correct 4 ms 8184 KB Output is correct
15 Correct 5 ms 8300 KB Output is correct
16 Correct 5 ms 8288 KB Output is correct
17 Correct 5 ms 8264 KB Output is correct
18 Correct 5 ms 8276 KB Output is correct
19 Correct 5 ms 8284 KB Output is correct
20 Correct 5 ms 8276 KB Output is correct
21 Correct 4 ms 8148 KB Output is correct
22 Correct 5 ms 8276 KB Output is correct
23 Correct 5 ms 8292 KB Output is correct
24 Correct 6 ms 8276 KB Output is correct
25 Correct 5 ms 8272 KB Output is correct
26 Correct 5 ms 8276 KB Output is correct
27 Correct 5 ms 8192 KB Output is correct
28 Correct 5 ms 8288 KB Output is correct
29 Correct 5 ms 8276 KB Output is correct
30 Correct 4 ms 8276 KB Output is correct
31 Correct 601 ms 32472 KB Output is correct
32 Correct 86 ms 18156 KB Output is correct
33 Correct 514 ms 31276 KB Output is correct
34 Correct 531 ms 32516 KB Output is correct
35 Correct 593 ms 32676 KB Output is correct
36 Correct 592 ms 32940 KB Output is correct
37 Correct 427 ms 31932 KB Output is correct
38 Correct 405 ms 31612 KB Output is correct
39 Correct 344 ms 30716 KB Output is correct
40 Correct 361 ms 30692 KB Output is correct
41 Correct 346 ms 30292 KB Output is correct
42 Correct 320 ms 30124 KB Output is correct
43 Correct 77 ms 19436 KB Output is correct
44 Correct 331 ms 30452 KB Output is correct
45 Correct 342 ms 30384 KB Output is correct
46 Correct 316 ms 29868 KB Output is correct
47 Correct 201 ms 28984 KB Output is correct
48 Correct 211 ms 29368 KB Output is correct
49 Correct 238 ms 29220 KB Output is correct
50 Correct 264 ms 29400 KB Output is correct
51 Correct 260 ms 30268 KB Output is correct
52 Correct 1934 ms 123232 KB Output is correct
53 Correct 1362 ms 114624 KB Output is correct
54 Correct 1010 ms 125692 KB Output is correct
55 Correct 1894 ms 124736 KB Output is correct
56 Correct 1316 ms 117376 KB Output is correct
57 Correct 1393 ms 114868 KB Output is correct
58 Correct 1034 ms 125748 KB Output is correct
59 Correct 1784 ms 124452 KB Output is correct
60 Correct 2045 ms 122328 KB Output is correct
61 Correct 1654 ms 114976 KB Output is correct
62 Correct 1299 ms 116596 KB Output is correct
63 Correct 1453 ms 117840 KB Output is correct
64 Correct 3275 ms 126116 KB Output is correct
65 Correct 388 ms 53484 KB Output is correct
66 Correct 2931 ms 125180 KB Output is correct
67 Correct 1143 ms 132340 KB Output is correct
68 Correct 2381 ms 130464 KB Output is correct
69 Correct 2091 ms 125916 KB Output is correct
70 Correct 2672 ms 121524 KB Output is correct
71 Correct 2963 ms 121256 KB Output is correct
72 Correct 1229 ms 130948 KB Output is correct
73 Correct 2713 ms 128560 KB Output is correct
74 Correct 3529 ms 129064 KB Output is correct
75 Correct 3757 ms 127348 KB Output is correct
76 Correct 1737 ms 115720 KB Output is correct
77 Correct 1642 ms 112832 KB Output is correct
78 Correct 1978 ms 114236 KB Output is correct
79 Correct 2214 ms 116516 KB Output is correct
80 Correct 1991 ms 116388 KB Output is correct
81 Correct 213 ms 31492 KB Output is correct
82 Correct 238 ms 29784 KB Output is correct
83 Correct 383 ms 33216 KB Output is correct
84 Correct 334 ms 31996 KB Output is correct
85 Correct 302 ms 31656 KB Output is correct
86 Correct 385 ms 30860 KB Output is correct
87 Correct 324 ms 30768 KB Output is correct
88 Correct 297 ms 31132 KB Output is correct
89 Correct 349 ms 30676 KB Output is correct
90 Correct 97 ms 21624 KB Output is correct
91 Correct 218 ms 31592 KB Output is correct
92 Correct 314 ms 31784 KB Output is correct
93 Correct 339 ms 32208 KB Output is correct
94 Correct 416 ms 30896 KB Output is correct
95 Correct 387 ms 30576 KB Output is correct
96 Correct 205 ms 19160 KB Output is correct
97 Correct 1359 ms 131568 KB Output is correct
98 Correct 479 ms 53944 KB Output is correct
99 Correct 4467 ms 126812 KB Output is correct
100 Correct 1248 ms 128904 KB Output is correct
101 Correct 2569 ms 131020 KB Output is correct
102 Execution timed out 5009 ms 132880 KB Time limit exceeded
103 Halted 0 ms 0 KB -