Submission #838702

# Submission time Handle Problem Language Result Execution time Memory
838702 2023-08-27T15:29:14 Z skittles1412 Event Hopping (BOI22_events) C++17
100 / 100
119 ms 40904 KB
#include "bits/extc++.h"

using namespace std;

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t;
    ((cerr << " | " << u), ...);
    cerr << endl;
}

#ifdef DEBUG
#define dbg(...)                                              \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
    dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr   \
    if (false) \
    cerr
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))

template <typename Cb>
struct Cmp {
    Cb cb;

    Cmp(Cb cb) : cb(cb) {}

    template <typename T>
    bool operator()(const T& a, const T& b) const {
        return cb(a) < cb(b);
    }
};

template <typename T>
struct ST {
    int n, logn;
    vector<vector<T>> v;

    ST(const vector<T>& arr)
        : n(sz(arr)), logn(__lg(n)), v(logn + 1, vector<T>(n)) {
        v[0] = arr;
        for (int i = 1; i <= logn; i++) {
            for (int j = 0; j + (1 << i) <= n; j++) {
                v[i][j] = min(v[i - 1][j], v[i - 1][j + (1 << (i - 1))]);
            }
        }
    }

    T query(int l, int r) const {
        int k = __lg(r - l);
        return min(v[k][l], v[k][r - (1 << k)]);
    }
};

int coord_comp(const vector<int*>& arr) {
    vector<int> vals;
    for (auto& a : arr) {
        vals.push_back(*a);
    }
    sort(begin(vals), end(vals));
    vals.erase(unique(begin(vals), end(vals)), vals.end());

    for (auto& a : arr) {
        *a = int(lower_bound(begin(vals), end(vals), *a) - vals.begin());
    }

    return sz(vals);
}

void solve() {
    int n, q;
    cin >> n >> q;

    vector<pair<int, int>> arr(n);
    for (auto& [l, r] : arr) {
        cin >> l >> r;
    }

    int m = coord_comp(({
        vector<int*> v;
        for (auto& [l, r] : arr) {
            v.push_back(&l);
            v.push_back(&r);
        }
        v;
    }));
    ST<pair<int, int>> st_arr(({
        vector<pair<int, int>> v(m, {int(1e9), -1});
        for (int i = 0; i < n; i++) {
            auto& [l, r] = arr[i];
            v[r] = min(v[r], {l, i});
        }
        v;
    }));

    int logn = __lg(n);

    vector<vector<int>> lift(logn + 1, vector<int>(n));
    for (int i = 0; i < n; i++) {
        pair<int, int> c_opt {int(1e9), -1};

        for (int j = 0; j < n; j++) {
            if (arr[i].first <= arr[j].second &&
                arr[j].second <= arr[i].second) {
                c_opt = min(c_opt, {arr[j].first, j});
            }
        }
        c_opt = st_arr.query(arr[i].first, arr[i].second + 1);

        if (c_opt.first >= arr[i].first) {
            lift[0][i] = -1;
        } else {
            lift[0][i] = c_opt.second;
        }
    }

    for (int i = 1; i <= logn; i++) {
        for (int j = 0; j < n; j++) {
            if (lift[i - 1][j] == -1) {
                lift[i][j] = -1;
            } else {
                lift[i][j] = lift[i - 1][lift[i - 1][j]];
            }
        }
    }

    while (q--) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;

        if (u == v) {
            cout << 0 << endl;
            continue;
        }

        int ans = 0;
        for (int i = logn; i >= 0; i--) {
            int nv = lift[i][v];
            if (nv != -1 && arr[u].second < arr[nv].first) {
                v = nv;
                ans += 1 << i;
            }
        }
        if (arr[u].second < arr[v].first) {
            v = lift[0][v];
            ans++;
        }
        // int ql = arr[v].first, qr = arr[v].second, ans = 0;

        // while (ql > arr[u].second) {
        //     int n_opt = 1e9;

        //     for (auto& [l, r] : arr) {
        //         if (ql <= r && r <= qr) {
        //             n_opt = min(n_opt, l);
        //         }
        //     }

        //     if (n_opt >= ql) {
        //         break;
        //     }
        //     ans++;
        //     ql = n_opt;
        // }

        if (v != -1 && arr[v].first <= arr[u].second &&
            arr[u].second <= arr[v].second) {
            ans++;
            cout << ans << endl;
        } else {
            cout << "impossible" << endl;
        }
    }
}

int main() {
    cin.tie(nullptr);
    cin.exceptions(ios::failbit);
    ios_base::sync_with_stdio(false);
    solve();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 81 ms 23424 KB Output is correct
3 Correct 106 ms 25528 KB Output is correct
4 Correct 106 ms 25516 KB Output is correct
5 Correct 100 ms 26572 KB Output is correct
6 Correct 99 ms 28036 KB Output is correct
7 Correct 106 ms 28492 KB Output is correct
8 Correct 93 ms 40836 KB Output is correct
9 Correct 91 ms 40904 KB Output is correct
10 Correct 100 ms 25860 KB Output is correct
11 Correct 103 ms 32108 KB Output is correct
12 Correct 63 ms 25220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 456 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 26 ms 1844 KB Output is correct
20 Correct 25 ms 1612 KB Output is correct
21 Correct 27 ms 1928 KB Output is correct
22 Correct 21 ms 1996 KB Output is correct
23 Correct 21 ms 2380 KB Output is correct
24 Correct 20 ms 2376 KB Output is correct
25 Correct 21 ms 1988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 62 ms 21864 KB Output is correct
20 Correct 56 ms 22876 KB Output is correct
21 Correct 70 ms 23280 KB Output is correct
22 Correct 74 ms 29452 KB Output is correct
23 Correct 84 ms 38532 KB Output is correct
24 Correct 73 ms 38640 KB Output is correct
25 Correct 21 ms 9364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 23520 KB Output is correct
2 Correct 113 ms 25476 KB Output is correct
3 Correct 119 ms 25476 KB Output is correct
4 Correct 100 ms 40796 KB Output is correct
5 Correct 104 ms 25860 KB Output is correct
6 Correct 109 ms 40528 KB Output is correct
7 Correct 109 ms 40452 KB Output is correct
8 Correct 117 ms 40652 KB Output is correct
9 Correct 77 ms 38640 KB Output is correct
10 Correct 100 ms 40072 KB Output is correct
11 Correct 95 ms 39888 KB Output is correct
12 Correct 101 ms 40140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 81 ms 23424 KB Output is correct
3 Correct 106 ms 25528 KB Output is correct
4 Correct 106 ms 25516 KB Output is correct
5 Correct 100 ms 26572 KB Output is correct
6 Correct 99 ms 28036 KB Output is correct
7 Correct 106 ms 28492 KB Output is correct
8 Correct 93 ms 40836 KB Output is correct
9 Correct 91 ms 40904 KB Output is correct
10 Correct 100 ms 25860 KB Output is correct
11 Correct 103 ms 32108 KB Output is correct
12 Correct 63 ms 25220 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 456 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 26 ms 1844 KB Output is correct
32 Correct 25 ms 1612 KB Output is correct
33 Correct 27 ms 1928 KB Output is correct
34 Correct 21 ms 1996 KB Output is correct
35 Correct 21 ms 2380 KB Output is correct
36 Correct 20 ms 2376 KB Output is correct
37 Correct 21 ms 1988 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 1 ms 460 KB Output is correct
44 Correct 1 ms 512 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 62 ms 21864 KB Output is correct
48 Correct 56 ms 22876 KB Output is correct
49 Correct 70 ms 23280 KB Output is correct
50 Correct 74 ms 29452 KB Output is correct
51 Correct 84 ms 38532 KB Output is correct
52 Correct 73 ms 38640 KB Output is correct
53 Correct 21 ms 9364 KB Output is correct
54 Correct 80 ms 23520 KB Output is correct
55 Correct 113 ms 25476 KB Output is correct
56 Correct 119 ms 25476 KB Output is correct
57 Correct 100 ms 40796 KB Output is correct
58 Correct 104 ms 25860 KB Output is correct
59 Correct 109 ms 40528 KB Output is correct
60 Correct 109 ms 40452 KB Output is correct
61 Correct 117 ms 40652 KB Output is correct
62 Correct 77 ms 38640 KB Output is correct
63 Correct 100 ms 40072 KB Output is correct
64 Correct 95 ms 39888 KB Output is correct
65 Correct 101 ms 40140 KB Output is correct
66 Correct 1 ms 212 KB Output is correct
67 Correct 98 ms 25480 KB Output is correct
68 Correct 88 ms 25552 KB Output is correct
69 Correct 117 ms 25532 KB Output is correct
70 Correct 99 ms 26580 KB Output is correct
71 Correct 96 ms 28036 KB Output is correct
72 Correct 101 ms 28548 KB Output is correct
73 Correct 93 ms 40828 KB Output is correct
74 Correct 93 ms 40836 KB Output is correct
75 Correct 98 ms 25804 KB Output is correct
76 Correct 102 ms 32068 KB Output is correct
77 Correct 53 ms 25224 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 1 ms 468 KB Output is correct
80 Correct 1 ms 468 KB Output is correct
81 Correct 1 ms 340 KB Output is correct
82 Correct 1 ms 468 KB Output is correct
83 Correct 1 ms 468 KB Output is correct
84 Correct 1 ms 468 KB Output is correct
85 Correct 1 ms 340 KB Output is correct
86 Correct 30 ms 2680 KB Output is correct
87 Correct 26 ms 2636 KB Output is correct
88 Correct 29 ms 2888 KB Output is correct
89 Correct 24 ms 3012 KB Output is correct
90 Correct 21 ms 3412 KB Output is correct
91 Correct 21 ms 3440 KB Output is correct
92 Correct 23 ms 3020 KB Output is correct
93 Correct 58 ms 23680 KB Output is correct
94 Correct 57 ms 22920 KB Output is correct
95 Correct 62 ms 23304 KB Output is correct
96 Correct 77 ms 29440 KB Output is correct
97 Correct 71 ms 38648 KB Output is correct
98 Correct 72 ms 38596 KB Output is correct
99 Correct 21 ms 9364 KB Output is correct
100 Correct 111 ms 40528 KB Output is correct
101 Correct 110 ms 40432 KB Output is correct
102 Correct 100 ms 40652 KB Output is correct
103 Correct 112 ms 40136 KB Output is correct
104 Correct 93 ms 39956 KB Output is correct
105 Correct 101 ms 40080 KB Output is correct
106 Correct 92 ms 21608 KB Output is correct
107 Correct 107 ms 24908 KB Output is correct
108 Correct 102 ms 25288 KB Output is correct
109 Correct 89 ms 25320 KB Output is correct
110 Correct 101 ms 40392 KB Output is correct
111 Correct 103 ms 40392 KB Output is correct