답안 #569116

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
569116 2022-05-26T17:45:40 Z Cyanmond 새 집 (APIO18_new_home) C++17
100 / 100
4166 ms 138388 KB
#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';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8148 KB Output is correct
2 Correct 4 ms 8020 KB Output is correct
3 Correct 3 ms 8020 KB Output is correct
4 Correct 5 ms 8148 KB Output is correct
5 Correct 7 ms 8148 KB Output is correct
6 Correct 6 ms 8276 KB Output is correct
7 Correct 4 ms 8276 KB Output is correct
8 Correct 5 ms 8236 KB Output is correct
9 Correct 4 ms 8276 KB Output is correct
10 Correct 5 ms 8276 KB Output is correct
11 Correct 5 ms 8276 KB Output is correct
12 Correct 5 ms 8276 KB Output is correct
13 Correct 5 ms 8276 KB Output is correct
14 Correct 5 ms 8276 KB Output is correct
15 Correct 4 ms 8200 KB Output is correct
16 Correct 5 ms 8276 KB Output is correct
17 Correct 5 ms 8204 KB Output is correct
18 Correct 6 ms 8276 KB Output is correct
19 Correct 6 ms 8276 KB Output is correct
20 Correct 5 ms 8276 KB Output is correct
21 Correct 4 ms 8220 KB Output is correct
22 Correct 5 ms 8276 KB Output is correct
23 Correct 5 ms 8276 KB Output is correct
24 Correct 5 ms 8276 KB Output is correct
25 Correct 5 ms 8276 KB Output is correct
26 Correct 5 ms 8292 KB Output is correct
27 Correct 5 ms 8112 KB Output is correct
28 Correct 5 ms 8276 KB Output is correct
29 Correct 5 ms 8276 KB Output is correct
30 Correct 4 ms 8148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8148 KB Output is correct
2 Correct 4 ms 8020 KB Output is correct
3 Correct 3 ms 8020 KB Output is correct
4 Correct 5 ms 8148 KB Output is correct
5 Correct 7 ms 8148 KB Output is correct
6 Correct 6 ms 8276 KB Output is correct
7 Correct 4 ms 8276 KB Output is correct
8 Correct 5 ms 8236 KB Output is correct
9 Correct 4 ms 8276 KB Output is correct
10 Correct 5 ms 8276 KB Output is correct
11 Correct 5 ms 8276 KB Output is correct
12 Correct 5 ms 8276 KB Output is correct
13 Correct 5 ms 8276 KB Output is correct
14 Correct 5 ms 8276 KB Output is correct
15 Correct 4 ms 8200 KB Output is correct
16 Correct 5 ms 8276 KB Output is correct
17 Correct 5 ms 8204 KB Output is correct
18 Correct 6 ms 8276 KB Output is correct
19 Correct 6 ms 8276 KB Output is correct
20 Correct 5 ms 8276 KB Output is correct
21 Correct 4 ms 8220 KB Output is correct
22 Correct 5 ms 8276 KB Output is correct
23 Correct 5 ms 8276 KB Output is correct
24 Correct 5 ms 8276 KB Output is correct
25 Correct 5 ms 8276 KB Output is correct
26 Correct 5 ms 8292 KB Output is correct
27 Correct 5 ms 8112 KB Output is correct
28 Correct 5 ms 8276 KB Output is correct
29 Correct 5 ms 8276 KB Output is correct
30 Correct 4 ms 8148 KB Output is correct
31 Correct 648 ms 29588 KB Output is correct
32 Correct 93 ms 17124 KB Output is correct
33 Correct 655 ms 28508 KB Output is correct
34 Correct 606 ms 29404 KB Output is correct
35 Correct 689 ms 29736 KB Output is correct
36 Correct 655 ms 30196 KB Output is correct
37 Correct 413 ms 29032 KB Output is correct
38 Correct 428 ms 28736 KB Output is correct
39 Correct 351 ms 27768 KB Output is correct
40 Correct 380 ms 27836 KB Output is correct
41 Correct 354 ms 27352 KB Output is correct
42 Correct 333 ms 27156 KB Output is correct
43 Correct 82 ms 17164 KB Output is correct
44 Correct 405 ms 27448 KB Output is correct
45 Correct 344 ms 27476 KB Output is correct
46 Correct 321 ms 27028 KB Output is correct
47 Correct 197 ms 26236 KB Output is correct
48 Correct 212 ms 26620 KB Output is correct
49 Correct 232 ms 26372 KB Output is correct
50 Correct 253 ms 26460 KB Output is correct
51 Correct 246 ms 27488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2088 ms 109728 KB Output is correct
2 Correct 1483 ms 102088 KB Output is correct
3 Correct 1175 ms 111916 KB Output is correct
4 Correct 1936 ms 110800 KB Output is correct
5 Correct 1412 ms 104840 KB Output is correct
6 Correct 1520 ms 102488 KB Output is correct
7 Correct 1153 ms 111908 KB Output is correct
8 Correct 1944 ms 110832 KB Output is correct
9 Correct 2223 ms 108900 KB Output is correct
10 Correct 1760 ms 101876 KB Output is correct
11 Correct 1394 ms 103984 KB Output is correct
12 Correct 1539 ms 105000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3827 ms 112924 KB Output is correct
2 Correct 425 ms 49232 KB Output is correct
3 Correct 3197 ms 112884 KB Output is correct
4 Correct 1166 ms 118628 KB Output is correct
5 Correct 2359 ms 117152 KB Output is correct
6 Correct 2137 ms 112424 KB Output is correct
7 Correct 2790 ms 109160 KB Output is correct
8 Correct 2978 ms 109064 KB Output is correct
9 Correct 1255 ms 117596 KB Output is correct
10 Correct 2350 ms 115172 KB Output is correct
11 Correct 3028 ms 116140 KB Output is correct
12 Correct 3105 ms 115008 KB Output is correct
13 Correct 1543 ms 103920 KB Output is correct
14 Correct 1520 ms 100924 KB Output is correct
15 Correct 1746 ms 102080 KB Output is correct
16 Correct 1918 ms 104004 KB Output is correct
17 Correct 1834 ms 104392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8148 KB Output is correct
2 Correct 4 ms 8020 KB Output is correct
3 Correct 3 ms 8020 KB Output is correct
4 Correct 5 ms 8148 KB Output is correct
5 Correct 7 ms 8148 KB Output is correct
6 Correct 6 ms 8276 KB Output is correct
7 Correct 4 ms 8276 KB Output is correct
8 Correct 5 ms 8236 KB Output is correct
9 Correct 4 ms 8276 KB Output is correct
10 Correct 5 ms 8276 KB Output is correct
11 Correct 5 ms 8276 KB Output is correct
12 Correct 5 ms 8276 KB Output is correct
13 Correct 5 ms 8276 KB Output is correct
14 Correct 5 ms 8276 KB Output is correct
15 Correct 4 ms 8200 KB Output is correct
16 Correct 5 ms 8276 KB Output is correct
17 Correct 5 ms 8204 KB Output is correct
18 Correct 6 ms 8276 KB Output is correct
19 Correct 6 ms 8276 KB Output is correct
20 Correct 5 ms 8276 KB Output is correct
21 Correct 4 ms 8220 KB Output is correct
22 Correct 5 ms 8276 KB Output is correct
23 Correct 5 ms 8276 KB Output is correct
24 Correct 5 ms 8276 KB Output is correct
25 Correct 5 ms 8276 KB Output is correct
26 Correct 5 ms 8292 KB Output is correct
27 Correct 5 ms 8112 KB Output is correct
28 Correct 5 ms 8276 KB Output is correct
29 Correct 5 ms 8276 KB Output is correct
30 Correct 4 ms 8148 KB Output is correct
31 Correct 648 ms 29588 KB Output is correct
32 Correct 93 ms 17124 KB Output is correct
33 Correct 655 ms 28508 KB Output is correct
34 Correct 606 ms 29404 KB Output is correct
35 Correct 689 ms 29736 KB Output is correct
36 Correct 655 ms 30196 KB Output is correct
37 Correct 413 ms 29032 KB Output is correct
38 Correct 428 ms 28736 KB Output is correct
39 Correct 351 ms 27768 KB Output is correct
40 Correct 380 ms 27836 KB Output is correct
41 Correct 354 ms 27352 KB Output is correct
42 Correct 333 ms 27156 KB Output is correct
43 Correct 82 ms 17164 KB Output is correct
44 Correct 405 ms 27448 KB Output is correct
45 Correct 344 ms 27476 KB Output is correct
46 Correct 321 ms 27028 KB Output is correct
47 Correct 197 ms 26236 KB Output is correct
48 Correct 212 ms 26620 KB Output is correct
49 Correct 232 ms 26372 KB Output is correct
50 Correct 253 ms 26460 KB Output is correct
51 Correct 246 ms 27488 KB Output is correct
52 Correct 208 ms 28352 KB Output is correct
53 Correct 183 ms 26716 KB Output is correct
54 Correct 319 ms 30132 KB Output is correct
55 Correct 306 ms 29036 KB Output is correct
56 Correct 270 ms 28740 KB Output is correct
57 Correct 319 ms 28060 KB Output is correct
58 Correct 284 ms 27680 KB Output is correct
59 Correct 267 ms 28084 KB Output is correct
60 Correct 305 ms 27660 KB Output is correct
61 Correct 81 ms 19168 KB Output is correct
62 Correct 217 ms 28536 KB Output is correct
63 Correct 272 ms 28736 KB Output is correct
64 Correct 322 ms 29224 KB Output is correct
65 Correct 331 ms 27932 KB Output is correct
66 Correct 322 ms 27648 KB Output is correct
67 Correct 177 ms 18012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8148 KB Output is correct
2 Correct 4 ms 8020 KB Output is correct
3 Correct 3 ms 8020 KB Output is correct
4 Correct 5 ms 8148 KB Output is correct
5 Correct 7 ms 8148 KB Output is correct
6 Correct 6 ms 8276 KB Output is correct
7 Correct 4 ms 8276 KB Output is correct
8 Correct 5 ms 8236 KB Output is correct
9 Correct 4 ms 8276 KB Output is correct
10 Correct 5 ms 8276 KB Output is correct
11 Correct 5 ms 8276 KB Output is correct
12 Correct 5 ms 8276 KB Output is correct
13 Correct 5 ms 8276 KB Output is correct
14 Correct 5 ms 8276 KB Output is correct
15 Correct 4 ms 8200 KB Output is correct
16 Correct 5 ms 8276 KB Output is correct
17 Correct 5 ms 8204 KB Output is correct
18 Correct 6 ms 8276 KB Output is correct
19 Correct 6 ms 8276 KB Output is correct
20 Correct 5 ms 8276 KB Output is correct
21 Correct 4 ms 8220 KB Output is correct
22 Correct 5 ms 8276 KB Output is correct
23 Correct 5 ms 8276 KB Output is correct
24 Correct 5 ms 8276 KB Output is correct
25 Correct 5 ms 8276 KB Output is correct
26 Correct 5 ms 8292 KB Output is correct
27 Correct 5 ms 8112 KB Output is correct
28 Correct 5 ms 8276 KB Output is correct
29 Correct 5 ms 8276 KB Output is correct
30 Correct 4 ms 8148 KB Output is correct
31 Correct 648 ms 29588 KB Output is correct
32 Correct 93 ms 17124 KB Output is correct
33 Correct 655 ms 28508 KB Output is correct
34 Correct 606 ms 29404 KB Output is correct
35 Correct 689 ms 29736 KB Output is correct
36 Correct 655 ms 30196 KB Output is correct
37 Correct 413 ms 29032 KB Output is correct
38 Correct 428 ms 28736 KB Output is correct
39 Correct 351 ms 27768 KB Output is correct
40 Correct 380 ms 27836 KB Output is correct
41 Correct 354 ms 27352 KB Output is correct
42 Correct 333 ms 27156 KB Output is correct
43 Correct 82 ms 17164 KB Output is correct
44 Correct 405 ms 27448 KB Output is correct
45 Correct 344 ms 27476 KB Output is correct
46 Correct 321 ms 27028 KB Output is correct
47 Correct 197 ms 26236 KB Output is correct
48 Correct 212 ms 26620 KB Output is correct
49 Correct 232 ms 26372 KB Output is correct
50 Correct 253 ms 26460 KB Output is correct
51 Correct 246 ms 27488 KB Output is correct
52 Correct 2088 ms 109728 KB Output is correct
53 Correct 1483 ms 102088 KB Output is correct
54 Correct 1175 ms 111916 KB Output is correct
55 Correct 1936 ms 110800 KB Output is correct
56 Correct 1412 ms 104840 KB Output is correct
57 Correct 1520 ms 102488 KB Output is correct
58 Correct 1153 ms 111908 KB Output is correct
59 Correct 1944 ms 110832 KB Output is correct
60 Correct 2223 ms 108900 KB Output is correct
61 Correct 1760 ms 101876 KB Output is correct
62 Correct 1394 ms 103984 KB Output is correct
63 Correct 1539 ms 105000 KB Output is correct
64 Correct 3827 ms 112924 KB Output is correct
65 Correct 425 ms 49232 KB Output is correct
66 Correct 3197 ms 112884 KB Output is correct
67 Correct 1166 ms 118628 KB Output is correct
68 Correct 2359 ms 117152 KB Output is correct
69 Correct 2137 ms 112424 KB Output is correct
70 Correct 2790 ms 109160 KB Output is correct
71 Correct 2978 ms 109064 KB Output is correct
72 Correct 1255 ms 117596 KB Output is correct
73 Correct 2350 ms 115172 KB Output is correct
74 Correct 3028 ms 116140 KB Output is correct
75 Correct 3105 ms 115008 KB Output is correct
76 Correct 1543 ms 103920 KB Output is correct
77 Correct 1520 ms 100924 KB Output is correct
78 Correct 1746 ms 102080 KB Output is correct
79 Correct 1918 ms 104004 KB Output is correct
80 Correct 1834 ms 104392 KB Output is correct
81 Correct 208 ms 28352 KB Output is correct
82 Correct 183 ms 26716 KB Output is correct
83 Correct 319 ms 30132 KB Output is correct
84 Correct 306 ms 29036 KB Output is correct
85 Correct 270 ms 28740 KB Output is correct
86 Correct 319 ms 28060 KB Output is correct
87 Correct 284 ms 27680 KB Output is correct
88 Correct 267 ms 28084 KB Output is correct
89 Correct 305 ms 27660 KB Output is correct
90 Correct 81 ms 19168 KB Output is correct
91 Correct 217 ms 28536 KB Output is correct
92 Correct 272 ms 28736 KB Output is correct
93 Correct 322 ms 29224 KB Output is correct
94 Correct 331 ms 27932 KB Output is correct
95 Correct 322 ms 27648 KB Output is correct
96 Correct 177 ms 18012 KB Output is correct
97 Correct 1227 ms 116084 KB Output is correct
98 Correct 440 ms 49408 KB Output is correct
99 Correct 3821 ms 112640 KB Output is correct
100 Correct 1199 ms 113312 KB Output is correct
101 Correct 2232 ms 115712 KB Output is correct
102 Correct 4166 ms 118712 KB Output is correct
103 Correct 2753 ms 119904 KB Output is correct
104 Correct 2715 ms 125700 KB Output is correct
105 Correct 1899 ms 121588 KB Output is correct
106 Correct 2005 ms 126164 KB Output is correct
107 Correct 1903 ms 122152 KB Output is correct
108 Correct 1772 ms 124260 KB Output is correct
109 Correct 2100 ms 120888 KB Output is correct
110 Correct 1857 ms 123200 KB Output is correct
111 Correct 1814 ms 126072 KB Output is correct
112 Correct 2131 ms 121040 KB Output is correct
113 Correct 428 ms 78624 KB Output is correct
114 Correct 1392 ms 138388 KB Output is correct
115 Correct 2009 ms 129472 KB Output is correct
116 Correct 2353 ms 127960 KB Output is correct
117 Correct 2497 ms 121148 KB Output is correct
118 Correct 2381 ms 119480 KB Output is correct
119 Correct 1002 ms 60524 KB Output is correct
120 Correct 897 ms 109028 KB Output is correct
121 Correct 1176 ms 110004 KB Output is correct
122 Correct 1156 ms 113072 KB Output is correct
123 Correct 1270 ms 115392 KB Output is correct
124 Correct 1364 ms 116500 KB Output is correct
125 Correct 1387 ms 115096 KB Output is correct
126 Correct 1347 ms 115376 KB Output is correct