Submission #857698

# Submission time Handle Problem Language Result Execution time Memory
857698 2023-10-06T15:41:56 Z vgtcross Event Hopping (BOI22_events) C++17
100 / 100
206 ms 27728 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;

const int N = 100100;
const int K = 18;

int jmp[K][N];

struct segtree {
    pii v;
    int l, r, m;
    segtree *lc = nullptr, *rc = nullptr;
    
    segtree(int L, int R, vector<int> &u){
        l = L;
        r = R;
        m = (l + r) / 2;
        if (l == r) {
            v = {u[m], m};
            return;
        }
        
        lc = new segtree(l, m, u);
        rc = new segtree(m+1, r, u);
        v = min(lc->v, rc->v);
    }
    
    pii range_min(int L, int R) {
        if (L > R) return {2e9, 2e9};
        if (l == L && r == R) return v;
        return min(lc->range_min(L, min(R, m)), rc->range_min(max(m+1, L), R));
    }
    
    void del() {
        if (lc != nullptr) lc->del();
        if (rc != nullptr) rc->del();
        delete lc;
        delete rc;
    }
};

void solve() {
    int n, q;
    cin >> n >> q;
    
    vector<array<int, 3>> v(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i][1] >> v[i][0];
        v[i][2] = i+1;
    }
    sort(v.begin(), v.end());
    
    vector<int> pos(n+1);
    vector<int> d(n);
    set<pii> st;
    for (int i = 0; i < n; ++i) {
        st.insert({v[i][0], i});
        pii b = *st.lower_bound({v[i][1], -1});
        d[i] = b.second;
        pos[v[i][2]] = i;
    }
    
    segtree seg(0, n-1, d);
    for (int i = 0; i < n; ++i) {
        jmp[0][i] = seg.range_min(d[i], i).second;
    }
    
    for (int i = 1; i < K; ++i) {
        for (int j = 0; j < n; ++j) jmp[i][j] = jmp[i-1][jmp[i-1][j]];
    }
    
    while (q--) {
        int a, b;
        cin >> a >> b;
        a = pos[a];
        b = pos[b];
        
        if (v[a][0] > v[b][0]) {
            cout << "impossible\n";
            continue;
        } else if (a == b) {
            cout << "0\n";
            continue;
        } else if (d[b] <= a) {
            cout << "1\n";
            continue;
        }
        
        int ans = 0;
        for (int i = K-1; i >= 0; --i) {
            if (a < d[jmp[i][b]]) {
                b = jmp[i][b];
                ans += 1 << i;
            }
        }
        
        b = jmp[0][b];
        
        if (a < d[b]) cout << "impossible\n";
        else cout << ans+2 << '\n';
        cout << flush;
    }
    
    seg.del();
}

int main() {
    cin.tie(0) -> sync_with_stdio(0);
    
    solve();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 186 ms 24144 KB Output is correct
3 Correct 189 ms 24144 KB Output is correct
4 Correct 192 ms 23896 KB Output is correct
5 Correct 191 ms 24056 KB Output is correct
6 Correct 206 ms 24356 KB Output is correct
7 Correct 190 ms 24148 KB Output is correct
8 Correct 184 ms 24624 KB Output is correct
9 Correct 191 ms 24552 KB Output is correct
10 Correct 140 ms 24464 KB Output is correct
11 Correct 165 ms 24676 KB Output is correct
12 Correct 185 ms 26920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6744 KB Output is correct
4 Correct 1 ms 6748 KB Output is correct
5 Correct 1 ms 6748 KB Output is correct
6 Correct 2 ms 6748 KB Output is correct
7 Correct 2 ms 6748 KB Output is correct
8 Correct 1 ms 6748 KB Output is correct
9 Correct 1 ms 6744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6744 KB Output is correct
4 Correct 1 ms 6748 KB Output is correct
5 Correct 1 ms 6748 KB Output is correct
6 Correct 2 ms 6748 KB Output is correct
7 Correct 2 ms 6748 KB Output is correct
8 Correct 1 ms 6748 KB Output is correct
9 Correct 1 ms 6744 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6748 KB Output is correct
13 Correct 2 ms 6748 KB Output is correct
14 Correct 1 ms 6748 KB Output is correct
15 Correct 2 ms 6748 KB Output is correct
16 Correct 2 ms 6748 KB Output is correct
17 Correct 1 ms 6748 KB Output is correct
18 Correct 1 ms 6748 KB Output is correct
19 Correct 135 ms 8636 KB Output is correct
20 Correct 129 ms 8784 KB Output is correct
21 Correct 81 ms 9020 KB Output is correct
22 Correct 60 ms 9304 KB Output is correct
23 Correct 38 ms 9180 KB Output is correct
24 Correct 19 ms 9048 KB Output is correct
25 Correct 131 ms 8700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6744 KB Output is correct
4 Correct 1 ms 6748 KB Output is correct
5 Correct 1 ms 6748 KB Output is correct
6 Correct 2 ms 6748 KB Output is correct
7 Correct 2 ms 6748 KB Output is correct
8 Correct 1 ms 6748 KB Output is correct
9 Correct 1 ms 6744 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 2 ms 6748 KB Output is correct
13 Correct 1 ms 6748 KB Output is correct
14 Correct 1 ms 6748 KB Output is correct
15 Correct 1 ms 6748 KB Output is correct
16 Correct 1 ms 6748 KB Output is correct
17 Correct 2 ms 6744 KB Output is correct
18 Correct 2 ms 6748 KB Output is correct
19 Correct 62 ms 23380 KB Output is correct
20 Correct 61 ms 24528 KB Output is correct
21 Correct 74 ms 25264 KB Output is correct
22 Correct 64 ms 25428 KB Output is correct
23 Correct 59 ms 25584 KB Output is correct
24 Correct 69 ms 25424 KB Output is correct
25 Correct 58 ms 25172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 188 ms 24148 KB Output is correct
2 Correct 186 ms 24148 KB Output is correct
3 Correct 191 ms 23892 KB Output is correct
4 Correct 182 ms 24628 KB Output is correct
5 Correct 141 ms 24444 KB Output is correct
6 Correct 155 ms 24144 KB Output is correct
7 Correct 156 ms 24084 KB Output is correct
8 Correct 146 ms 24400 KB Output is correct
9 Correct 63 ms 23376 KB Output is correct
10 Correct 196 ms 24128 KB Output is correct
11 Correct 193 ms 23636 KB Output is correct
12 Correct 193 ms 23840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 186 ms 24144 KB Output is correct
3 Correct 189 ms 24144 KB Output is correct
4 Correct 192 ms 23896 KB Output is correct
5 Correct 191 ms 24056 KB Output is correct
6 Correct 206 ms 24356 KB Output is correct
7 Correct 190 ms 24148 KB Output is correct
8 Correct 184 ms 24624 KB Output is correct
9 Correct 191 ms 24552 KB Output is correct
10 Correct 140 ms 24464 KB Output is correct
11 Correct 165 ms 24676 KB Output is correct
12 Correct 185 ms 26920 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6744 KB Output is correct
16 Correct 1 ms 6748 KB Output is correct
17 Correct 1 ms 6748 KB Output is correct
18 Correct 2 ms 6748 KB Output is correct
19 Correct 2 ms 6748 KB Output is correct
20 Correct 1 ms 6748 KB Output is correct
21 Correct 1 ms 6744 KB Output is correct
22 Correct 1 ms 6488 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6748 KB Output is correct
25 Correct 2 ms 6748 KB Output is correct
26 Correct 1 ms 6748 KB Output is correct
27 Correct 2 ms 6748 KB Output is correct
28 Correct 2 ms 6748 KB Output is correct
29 Correct 1 ms 6748 KB Output is correct
30 Correct 1 ms 6748 KB Output is correct
31 Correct 135 ms 8636 KB Output is correct
32 Correct 129 ms 8784 KB Output is correct
33 Correct 81 ms 9020 KB Output is correct
34 Correct 60 ms 9304 KB Output is correct
35 Correct 38 ms 9180 KB Output is correct
36 Correct 19 ms 9048 KB Output is correct
37 Correct 131 ms 8700 KB Output is correct
38 Correct 1 ms 6488 KB Output is correct
39 Correct 1 ms 6492 KB Output is correct
40 Correct 2 ms 6748 KB Output is correct
41 Correct 1 ms 6748 KB Output is correct
42 Correct 1 ms 6748 KB Output is correct
43 Correct 1 ms 6748 KB Output is correct
44 Correct 1 ms 6748 KB Output is correct
45 Correct 2 ms 6744 KB Output is correct
46 Correct 2 ms 6748 KB Output is correct
47 Correct 62 ms 23380 KB Output is correct
48 Correct 61 ms 24528 KB Output is correct
49 Correct 74 ms 25264 KB Output is correct
50 Correct 64 ms 25428 KB Output is correct
51 Correct 59 ms 25584 KB Output is correct
52 Correct 69 ms 25424 KB Output is correct
53 Correct 58 ms 25172 KB Output is correct
54 Correct 188 ms 24148 KB Output is correct
55 Correct 186 ms 24148 KB Output is correct
56 Correct 191 ms 23892 KB Output is correct
57 Correct 182 ms 24628 KB Output is correct
58 Correct 141 ms 24444 KB Output is correct
59 Correct 155 ms 24144 KB Output is correct
60 Correct 156 ms 24084 KB Output is correct
61 Correct 146 ms 24400 KB Output is correct
62 Correct 63 ms 23376 KB Output is correct
63 Correct 196 ms 24128 KB Output is correct
64 Correct 193 ms 23636 KB Output is correct
65 Correct 193 ms 23840 KB Output is correct
66 Correct 1 ms 6492 KB Output is correct
67 Correct 193 ms 27200 KB Output is correct
68 Correct 190 ms 27032 KB Output is correct
69 Correct 193 ms 27224 KB Output is correct
70 Correct 194 ms 27296 KB Output is correct
71 Correct 189 ms 27312 KB Output is correct
72 Correct 199 ms 27152 KB Output is correct
73 Correct 198 ms 27544 KB Output is correct
74 Correct 184 ms 27728 KB Output is correct
75 Correct 146 ms 27468 KB Output is correct
76 Correct 161 ms 27644 KB Output is correct
77 Correct 174 ms 26860 KB Output is correct
78 Correct 1 ms 6492 KB Output is correct
79 Correct 1 ms 6748 KB Output is correct
80 Correct 1 ms 6620 KB Output is correct
81 Correct 1 ms 6744 KB Output is correct
82 Correct 1 ms 6748 KB Output is correct
83 Correct 2 ms 6748 KB Output is correct
84 Correct 2 ms 6748 KB Output is correct
85 Correct 1 ms 6748 KB Output is correct
86 Correct 136 ms 8764 KB Output is correct
87 Correct 137 ms 8784 KB Output is correct
88 Correct 81 ms 9192 KB Output is correct
89 Correct 60 ms 9176 KB Output is correct
90 Correct 40 ms 9300 KB Output is correct
91 Correct 19 ms 9052 KB Output is correct
92 Correct 131 ms 8536 KB Output is correct
93 Correct 61 ms 25428 KB Output is correct
94 Correct 59 ms 24656 KB Output is correct
95 Correct 77 ms 25372 KB Output is correct
96 Correct 63 ms 25428 KB Output is correct
97 Correct 63 ms 25424 KB Output is correct
98 Correct 71 ms 25400 KB Output is correct
99 Correct 53 ms 24656 KB Output is correct
100 Correct 150 ms 27376 KB Output is correct
101 Correct 150 ms 27216 KB Output is correct
102 Correct 158 ms 27472 KB Output is correct
103 Correct 199 ms 27264 KB Output is correct
104 Correct 193 ms 26828 KB Output is correct
105 Correct 194 ms 26964 KB Output is correct
106 Correct 126 ms 26644 KB Output is correct
107 Correct 147 ms 26452 KB Output is correct
108 Correct 138 ms 27240 KB Output is correct
109 Correct 136 ms 27216 KB Output is correct
110 Correct 136 ms 27292 KB Output is correct
111 Correct 132 ms 27212 KB Output is correct