Submission #935664

# Submission time Handle Problem Language Result Execution time Memory
935664 2024-02-29T10:55:03 Z peterandvoi Event Hopping (BOI22_events) C++17
100 / 100
136 ms 13684 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef ngu
#include "debug.h"
#else
#define debug(...) 42
#endif

#define MASK(x) (1LL << (x))

const int N = (int) 1e5 + 5;
const int LOG = 17;

int n, m, q;
pair<int, int> event[N];
int up[LOG][N];

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    #ifdef ngu
    freopen("test.inp", "r", stdin);
    freopen("test.out", "w", stdout);
    #endif
    cin >> n >> q;
    vector<int> R;
    for (int i = 1; i <= n; ++i) {
        cin >> event[i].first >> event[i].second;
        R.emplace_back(event[i].second);
    }
    auto cmb = [&](int x, int y) {
        if (x == -1) {
            return y;
        }
        if (y == -1) {
            return x;
        }
        return event[x].first < event[y].first ? x : y;
    };
    sort(R.begin(), R.end());
    R.erase(unique(R.begin(), R.end()), R.end());
    m = (int) R.size();
    vector<int> tree(4 << __lg(m), -1);
    auto upd = [&](int i, int x) {
        int id = 1, l = 1, r = m;
        while (l < r) {
            int mid = l + r >> 1;
            id <<= 1;
            if (i <= mid) {
                r = mid;
            } else {
                id |= 1;
                l = mid + 1;
            }
        }
        tree[id] = cmb(tree[id], x);
        while (id > 1) {
            id >>= 1;
            tree[id] = cmb(tree[id << 1], tree[id << 1 | 1]);
        }
    };
    auto get = [&](auto get, int u, int v, int id = 1, int l = 1, int r = m) {
        if (u <= l && r <= v) {
            return tree[id];
        }
        int mid = l + r >> 1;
        if (u <= mid && mid < v) {
            return cmb(get(get, u, v, id << 1, l, mid), get(get, u, v, id << 1 | 1, mid + 1, r));
        }
        if (u <= mid) {
            return get(get, u, v, id << 1, l, mid);
        }
        return get(get, u, v, id << 1 | 1, mid + 1, r);
    };
    for (int i = 1; i <= n; ++i) {
        int tr = upper_bound(R.begin(), R.end(), event[i].second) - R.begin();
        upd(tr, i);
    }
    for (int i = 1; i <= n; ++i) {
        int L = lower_bound(R.begin(), R.end(), event[i].first) - R.begin() + 1;
        int tr = upper_bound(R.begin(), R.end(), event[i].second) - R.begin();
        up[0][i] = get(get, L, tr);
    }
    for (int j = 1; j < LOG; ++j) {
        for (int i = 1; i <= n; ++i) {
            up[j][i] = up[j - 1][up[j - 1][i]];
        }
    }
    auto reach = [&](int u, int v) {
        if (v == -1) {
            return false;
        }
        return event[v].first <= event[u].second && event[u].second <= event[v].second;
    };
    auto qry = [&](int s, int t) {
        if (t == s) {
            cout << 0 << "\n";
            return;
        }
        if (event[s].second > event[t].second) {
            cout << "impossible\n";
            return;
        }
        if (event[t].first <= event[s].second) {
            cout << (reach(s, t) ? "1" : "impossible") << "\n";
            return;
        }
        int res = 0;
        for (int i = LOG - 1; i >= 0; --i) {
            int u = up[i][t];
            if (u != -1 && event[u].first > event[s].second) {
                t = u;
                res += MASK(i);
            }
        }
        t = up[0][t];
        if (reach(s, t)) {
            cout << res + 2 << "\n";
        } else {
            cout << "impossible\n";
        }
    };
    while (q--) {
        int s, t;
        cin >> s >> t;
        qry(s, t);
    }
}

Compilation message

events.cpp: In lambda function:
events.cpp:49:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   49 |             int mid = l + r >> 1;
      |                       ~~^~~
events.cpp: In instantiation of 'main()::<lambda(auto:23, int, int, int, int, int)> [with auto:23 = main()::<lambda(auto:23, int, int, int, int, int)>]':
events.cpp:84:34:   required from here
events.cpp:68:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   68 |         int mid = l + r >> 1;
      |                   ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 102 ms 13140 KB Output is correct
3 Correct 119 ms 13032 KB Output is correct
4 Correct 118 ms 12900 KB Output is correct
5 Correct 113 ms 13184 KB Output is correct
6 Correct 117 ms 13148 KB Output is correct
7 Correct 114 ms 13136 KB Output is correct
8 Correct 115 ms 13416 KB Output is correct
9 Correct 125 ms 13512 KB Output is correct
10 Correct 109 ms 13428 KB Output is correct
11 Correct 111 ms 13684 KB Output is correct
12 Correct 65 ms 12304 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 2 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 2 ms 6672 KB Output is correct
8 Correct 2 ms 6488 KB Output is correct
9 Correct 2 ms 6492 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 2 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 2 ms 6672 KB Output is correct
8 Correct 2 ms 6488 KB Output is correct
9 Correct 2 ms 6492 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 6492 KB Output is correct
13 Correct 2 ms 6488 KB Output is correct
14 Correct 2 ms 6492 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 2 ms 6620 KB Output is correct
17 Correct 2 ms 6492 KB Output is correct
18 Correct 2 ms 6492 KB Output is correct
19 Correct 26 ms 8284 KB Output is correct
20 Correct 33 ms 8172 KB Output is correct
21 Correct 27 ms 8528 KB Output is correct
22 Correct 22 ms 8540 KB Output is correct
23 Correct 20 ms 8352 KB Output is correct
24 Correct 18 ms 8284 KB Output is correct
25 Correct 23 ms 8016 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 2 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 2 ms 6672 KB Output is correct
8 Correct 2 ms 6488 KB Output is correct
9 Correct 2 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 2 ms 6488 KB Output is correct
13 Correct 2 ms 6488 KB Output is correct
14 Correct 2 ms 6624 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 2 ms 6748 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 81 ms 11324 KB Output is correct
20 Correct 79 ms 10516 KB Output is correct
21 Correct 83 ms 10696 KB Output is correct
22 Correct 82 ms 11220 KB Output is correct
23 Correct 77 ms 11144 KB Output is correct
24 Correct 90 ms 11216 KB Output is correct
25 Correct 17 ms 9428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 13008 KB Output is correct
2 Correct 110 ms 13136 KB Output is correct
3 Correct 121 ms 12988 KB Output is correct
4 Correct 112 ms 13584 KB Output is correct
5 Correct 110 ms 13424 KB Output is correct
6 Correct 115 ms 13240 KB Output is correct
7 Correct 115 ms 13176 KB Output is correct
8 Correct 112 ms 13404 KB Output is correct
9 Correct 80 ms 11128 KB Output is correct
10 Correct 112 ms 12888 KB Output is correct
11 Correct 113 ms 12508 KB Output is correct
12 Correct 118 ms 12728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 102 ms 13140 KB Output is correct
3 Correct 119 ms 13032 KB Output is correct
4 Correct 118 ms 12900 KB Output is correct
5 Correct 113 ms 13184 KB Output is correct
6 Correct 117 ms 13148 KB Output is correct
7 Correct 114 ms 13136 KB Output is correct
8 Correct 115 ms 13416 KB Output is correct
9 Correct 125 ms 13512 KB Output is correct
10 Correct 109 ms 13428 KB Output is correct
11 Correct 111 ms 13684 KB Output is correct
12 Correct 65 ms 12304 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 2 ms 6492 KB Output is correct
17 Correct 2 ms 6492 KB Output is correct
18 Correct 2 ms 6492 KB Output is correct
19 Correct 2 ms 6672 KB Output is correct
20 Correct 2 ms 6488 KB Output is correct
21 Correct 2 ms 6492 KB Output is correct
22 Correct 1 ms 6488 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 2 ms 6492 KB Output is correct
25 Correct 2 ms 6488 KB Output is correct
26 Correct 2 ms 6492 KB Output is correct
27 Correct 2 ms 6492 KB Output is correct
28 Correct 2 ms 6620 KB Output is correct
29 Correct 2 ms 6492 KB Output is correct
30 Correct 2 ms 6492 KB Output is correct
31 Correct 26 ms 8284 KB Output is correct
32 Correct 33 ms 8172 KB Output is correct
33 Correct 27 ms 8528 KB Output is correct
34 Correct 22 ms 8540 KB Output is correct
35 Correct 20 ms 8352 KB Output is correct
36 Correct 18 ms 8284 KB Output is correct
37 Correct 23 ms 8016 KB Output is correct
38 Correct 1 ms 6492 KB Output is correct
39 Correct 1 ms 6492 KB Output is correct
40 Correct 2 ms 6488 KB Output is correct
41 Correct 2 ms 6488 KB Output is correct
42 Correct 2 ms 6624 KB Output is correct
43 Correct 2 ms 6492 KB Output is correct
44 Correct 2 ms 6748 KB Output is correct
45 Correct 1 ms 6492 KB Output is correct
46 Correct 1 ms 6492 KB Output is correct
47 Correct 81 ms 11324 KB Output is correct
48 Correct 79 ms 10516 KB Output is correct
49 Correct 83 ms 10696 KB Output is correct
50 Correct 82 ms 11220 KB Output is correct
51 Correct 77 ms 11144 KB Output is correct
52 Correct 90 ms 11216 KB Output is correct
53 Correct 17 ms 9428 KB Output is correct
54 Correct 99 ms 13008 KB Output is correct
55 Correct 110 ms 13136 KB Output is correct
56 Correct 121 ms 12988 KB Output is correct
57 Correct 112 ms 13584 KB Output is correct
58 Correct 110 ms 13424 KB Output is correct
59 Correct 115 ms 13240 KB Output is correct
60 Correct 115 ms 13176 KB Output is correct
61 Correct 112 ms 13404 KB Output is correct
62 Correct 80 ms 11128 KB Output is correct
63 Correct 112 ms 12888 KB Output is correct
64 Correct 113 ms 12508 KB Output is correct
65 Correct 118 ms 12728 KB Output is correct
66 Correct 1 ms 6492 KB Output is correct
67 Correct 98 ms 12996 KB Output is correct
68 Correct 109 ms 13016 KB Output is correct
69 Correct 117 ms 13012 KB Output is correct
70 Correct 121 ms 13008 KB Output is correct
71 Correct 110 ms 13144 KB Output is correct
72 Correct 113 ms 13008 KB Output is correct
73 Correct 123 ms 13408 KB Output is correct
74 Correct 110 ms 13408 KB Output is correct
75 Correct 109 ms 13484 KB Output is correct
76 Correct 136 ms 13452 KB Output is correct
77 Correct 59 ms 12244 KB Output is correct
78 Correct 1 ms 6492 KB Output is correct
79 Correct 2 ms 6492 KB Output is correct
80 Correct 2 ms 6492 KB Output is correct
81 Correct 1 ms 6624 KB Output is correct
82 Correct 2 ms 6492 KB Output is correct
83 Correct 2 ms 6492 KB Output is correct
84 Correct 2 ms 6492 KB Output is correct
85 Correct 1 ms 6664 KB Output is correct
86 Correct 27 ms 8192 KB Output is correct
87 Correct 24 ms 8280 KB Output is correct
88 Correct 27 ms 8544 KB Output is correct
89 Correct 21 ms 8404 KB Output is correct
90 Correct 20 ms 8280 KB Output is correct
91 Correct 18 ms 8332 KB Output is correct
92 Correct 22 ms 7960 KB Output is correct
93 Correct 82 ms 11148 KB Output is correct
94 Correct 79 ms 10352 KB Output is correct
95 Correct 86 ms 10704 KB Output is correct
96 Correct 87 ms 11324 KB Output is correct
97 Correct 80 ms 11220 KB Output is correct
98 Correct 93 ms 11300 KB Output is correct
99 Correct 17 ms 9700 KB Output is correct
100 Correct 118 ms 13156 KB Output is correct
101 Correct 125 ms 13308 KB Output is correct
102 Correct 115 ms 13352 KB Output is correct
103 Correct 123 ms 13184 KB Output is correct
104 Correct 130 ms 12500 KB Output is correct
105 Correct 112 ms 12804 KB Output is correct
106 Correct 101 ms 12504 KB Output is correct
107 Correct 131 ms 12692 KB Output is correct
108 Correct 106 ms 12752 KB Output is correct
109 Correct 103 ms 12520 KB Output is correct
110 Correct 119 ms 13032 KB Output is correct
111 Correct 115 ms 13008 KB Output is correct