Submission #987405

# Submission time Handle Problem Language Result Execution time Memory
987405 2024-05-22T17:18:39 Z makrav Event Hopping (BOI22_events) C++14
100 / 100
264 ms 118636 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define all(a) (a).begin(), (a).end()
#define sz(a) (int) a.size()
#define pb push_back
#define ff first
#define sc second
#define pii pair<int, int>
#define f(i, l, r) for (int i = (l); i < (r); i++)
#define double ld

int sp[18][18][100010];

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, q; cin >> n >> q;
    vector<array<int, 3>> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i][0] >> a[i][1];
        a[i][2] = i;
    }
    sort(all(a), [](array<int, 3> x, array<int, 3> y) {
        return x[1] < y[1];
    });

    vector<vector<int>> up(18, vector<int>(n, 0));
    vector<int> nxt(n);
    int prv = n - 1;
    for (int i = n - 1; i >= 0; i--) {
        if (i == n - 1 || a[i][1] == a[i + 1][1]) nxt[i] = prv;
        else {
            prv = i;
            nxt[i] = prv;
        }
        int L = -1, R = n;
        while (R - L > 1) {
            int M = (L + R) / 2;
            if (a[M][1] >= a[i][0]) R = M;
            else L = M;
        }
        up[0][i] = R;
    }
    auto req = [&](int lvl, int l, int r) {
        int lg = (int)log2(r - l + 1);
        return min(sp[lvl][lg][l], sp[lvl][lg][r - (1 << lg) + 1]);
        };
    for (int lvl = 1; lvl < 18; lvl++) {
        for (int j = 0; j < n; j++) sp[lvl - 1][0][j] = up[lvl - 1][j];
        for (int i = 1; i < 18; i++) {
            for (int j = 0; j + (1 << i) - 1 < n; j++) {
                sp[lvl - 1][i][j] = min(sp[lvl - 1][i - 1][j], sp[lvl - 1][i - 1][j + (1 << (i - 1))]);
            }
        }
        
        for (int i = 0; i < n; i++) {
            up[lvl][i] = req(lvl - 1, up[lvl - 1][i], nxt[i]);
        }
    }
    vector<int> pos(n);
    for (int i = 0; i < n; i++) pos[a[i][2]] = i;
    while (q--) {
        int s, e; cin >> s >> e;
        s--; e--;
        s = pos[s];
        e = pos[e];
        if (a[s][1] > a[e][1]) {
            cout << "impossible\n";
            continue;
        }
        if (s == e) {
            cout << "0\n";
            continue;
        }
        int curr = e, curl = e, ans = 0;
        for (int j = 17; j >= 0; j--) {
            if (a[req(j, curl, curr)][1] > a[s][1]) {
                ans += (1 << j);
                curl = req(j, curl, curr);
                curr = nxt[e];
            }
        }
        if (ans > n) cout << "impossible\n";
        else cout << ans + 1 << '\n';
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 186 ms 117960 KB Output is correct
3 Correct 171 ms 118080 KB Output is correct
4 Correct 214 ms 118084 KB Output is correct
5 Correct 179 ms 117960 KB Output is correct
6 Correct 185 ms 117960 KB Output is correct
7 Correct 177 ms 118220 KB Output is correct
8 Correct 135 ms 118472 KB Output is correct
9 Correct 130 ms 118484 KB Output is correct
10 Correct 247 ms 118404 KB Output is correct
11 Correct 264 ms 118468 KB Output is correct
12 Correct 183 ms 117704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 3 ms 1884 KB Output is correct
4 Correct 2 ms 1884 KB Output is correct
5 Correct 2 ms 1884 KB Output is correct
6 Correct 2 ms 1884 KB Output is correct
7 Correct 2 ms 1880 KB Output is correct
8 Correct 2 ms 1884 KB Output is correct
9 Correct 2 ms 2136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 3 ms 1884 KB Output is correct
4 Correct 2 ms 1884 KB Output is correct
5 Correct 2 ms 1884 KB Output is correct
6 Correct 2 ms 1884 KB Output is correct
7 Correct 2 ms 1880 KB Output is correct
8 Correct 2 ms 1884 KB Output is correct
9 Correct 2 ms 2136 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 2 ms 1884 KB Output is correct
13 Correct 2 ms 1884 KB Output is correct
14 Correct 2 ms 1884 KB Output is correct
15 Correct 2 ms 1884 KB Output is correct
16 Correct 3 ms 1880 KB Output is correct
17 Correct 2 ms 2008 KB Output is correct
18 Correct 2 ms 1884 KB Output is correct
19 Correct 60 ms 7124 KB Output is correct
20 Correct 54 ms 7252 KB Output is correct
21 Correct 51 ms 7512 KB Output is correct
22 Correct 35 ms 7504 KB Output is correct
23 Correct 28 ms 7504 KB Output is correct
24 Correct 28 ms 7512 KB Output is correct
25 Correct 43 ms 6996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 3 ms 1884 KB Output is correct
4 Correct 2 ms 1884 KB Output is correct
5 Correct 2 ms 1884 KB Output is correct
6 Correct 2 ms 1884 KB Output is correct
7 Correct 2 ms 1880 KB Output is correct
8 Correct 2 ms 1884 KB Output is correct
9 Correct 2 ms 2136 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 2 ms 1884 KB Output is correct
13 Correct 2 ms 1880 KB Output is correct
14 Correct 2 ms 1884 KB Output is correct
15 Correct 2 ms 1888 KB Output is correct
16 Correct 2 ms 1880 KB Output is correct
17 Correct 2 ms 1888 KB Output is correct
18 Correct 2 ms 1884 KB Output is correct
19 Correct 119 ms 116164 KB Output is correct
20 Correct 121 ms 115716 KB Output is correct
21 Correct 122 ms 116208 KB Output is correct
22 Correct 121 ms 116172 KB Output is correct
23 Correct 116 ms 116172 KB Output is correct
24 Correct 117 ms 116172 KB Output is correct
25 Correct 106 ms 115592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 174 ms 117964 KB Output is correct
2 Correct 174 ms 118084 KB Output is correct
3 Correct 199 ms 117956 KB Output is correct
4 Correct 136 ms 118468 KB Output is correct
5 Correct 223 ms 118472 KB Output is correct
6 Correct 214 ms 118216 KB Output is correct
7 Correct 209 ms 118084 KB Output is correct
8 Correct 208 ms 118212 KB Output is correct
9 Correct 116 ms 116184 KB Output is correct
10 Correct 162 ms 117712 KB Output is correct
11 Correct 145 ms 117708 KB Output is correct
12 Correct 165 ms 117784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 186 ms 117960 KB Output is correct
3 Correct 171 ms 118080 KB Output is correct
4 Correct 214 ms 118084 KB Output is correct
5 Correct 179 ms 117960 KB Output is correct
6 Correct 185 ms 117960 KB Output is correct
7 Correct 177 ms 118220 KB Output is correct
8 Correct 135 ms 118472 KB Output is correct
9 Correct 130 ms 118484 KB Output is correct
10 Correct 247 ms 118404 KB Output is correct
11 Correct 264 ms 118468 KB Output is correct
12 Correct 183 ms 117704 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 3 ms 1884 KB Output is correct
16 Correct 2 ms 1884 KB Output is correct
17 Correct 2 ms 1884 KB Output is correct
18 Correct 2 ms 1884 KB Output is correct
19 Correct 2 ms 1880 KB Output is correct
20 Correct 2 ms 1884 KB Output is correct
21 Correct 2 ms 2136 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 2 ms 1884 KB Output is correct
25 Correct 2 ms 1884 KB Output is correct
26 Correct 2 ms 1884 KB Output is correct
27 Correct 2 ms 1884 KB Output is correct
28 Correct 3 ms 1880 KB Output is correct
29 Correct 2 ms 2008 KB Output is correct
30 Correct 2 ms 1884 KB Output is correct
31 Correct 60 ms 7124 KB Output is correct
32 Correct 54 ms 7252 KB Output is correct
33 Correct 51 ms 7512 KB Output is correct
34 Correct 35 ms 7504 KB Output is correct
35 Correct 28 ms 7504 KB Output is correct
36 Correct 28 ms 7512 KB Output is correct
37 Correct 43 ms 6996 KB Output is correct
38 Correct 0 ms 604 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 2 ms 1884 KB Output is correct
41 Correct 2 ms 1880 KB Output is correct
42 Correct 2 ms 1884 KB Output is correct
43 Correct 2 ms 1888 KB Output is correct
44 Correct 2 ms 1880 KB Output is correct
45 Correct 2 ms 1888 KB Output is correct
46 Correct 2 ms 1884 KB Output is correct
47 Correct 119 ms 116164 KB Output is correct
48 Correct 121 ms 115716 KB Output is correct
49 Correct 122 ms 116208 KB Output is correct
50 Correct 121 ms 116172 KB Output is correct
51 Correct 116 ms 116172 KB Output is correct
52 Correct 117 ms 116172 KB Output is correct
53 Correct 106 ms 115592 KB Output is correct
54 Correct 174 ms 117964 KB Output is correct
55 Correct 174 ms 118084 KB Output is correct
56 Correct 199 ms 117956 KB Output is correct
57 Correct 136 ms 118468 KB Output is correct
58 Correct 223 ms 118472 KB Output is correct
59 Correct 214 ms 118216 KB Output is correct
60 Correct 209 ms 118084 KB Output is correct
61 Correct 208 ms 118212 KB Output is correct
62 Correct 116 ms 116184 KB Output is correct
63 Correct 162 ms 117712 KB Output is correct
64 Correct 145 ms 117708 KB Output is correct
65 Correct 165 ms 117784 KB Output is correct
66 Correct 1 ms 604 KB Output is correct
67 Correct 182 ms 118020 KB Output is correct
68 Correct 174 ms 117964 KB Output is correct
69 Correct 197 ms 118144 KB Output is correct
70 Correct 184 ms 118204 KB Output is correct
71 Correct 175 ms 117976 KB Output is correct
72 Correct 170 ms 118216 KB Output is correct
73 Correct 130 ms 118472 KB Output is correct
74 Correct 130 ms 118608 KB Output is correct
75 Correct 256 ms 118636 KB Output is correct
76 Correct 216 ms 118304 KB Output is correct
77 Correct 178 ms 117820 KB Output is correct
78 Correct 1 ms 600 KB Output is correct
79 Correct 2 ms 1884 KB Output is correct
80 Correct 2 ms 1884 KB Output is correct
81 Correct 2 ms 1884 KB Output is correct
82 Correct 2 ms 1884 KB Output is correct
83 Correct 2 ms 1884 KB Output is correct
84 Correct 2 ms 1884 KB Output is correct
85 Correct 2 ms 1884 KB Output is correct
86 Correct 60 ms 7252 KB Output is correct
87 Correct 55 ms 7140 KB Output is correct
88 Correct 50 ms 7364 KB Output is correct
89 Correct 34 ms 7508 KB Output is correct
90 Correct 27 ms 7516 KB Output is correct
91 Correct 27 ms 7516 KB Output is correct
92 Correct 39 ms 7000 KB Output is correct
93 Correct 115 ms 116300 KB Output is correct
94 Correct 110 ms 115340 KB Output is correct
95 Correct 115 ms 116172 KB Output is correct
96 Correct 117 ms 116364 KB Output is correct
97 Correct 114 ms 116120 KB Output is correct
98 Correct 111 ms 116328 KB Output is correct
99 Correct 107 ms 115664 KB Output is correct
100 Correct 213 ms 118172 KB Output is correct
101 Correct 201 ms 118144 KB Output is correct
102 Correct 224 ms 118220 KB Output is correct
103 Correct 154 ms 117708 KB Output is correct
104 Correct 152 ms 117588 KB Output is correct
105 Correct 173 ms 117832 KB Output is correct
106 Correct 184 ms 117452 KB Output is correct
107 Correct 227 ms 117448 KB Output is correct
108 Correct 189 ms 118432 KB Output is correct
109 Correct 181 ms 118136 KB Output is correct
110 Correct 154 ms 117980 KB Output is correct
111 Correct 170 ms 118068 KB Output is correct