Submission #791979

# Submission time Handle Problem Language Result Execution time Memory
791979 2023-07-24T13:19:15 Z math_rabbit_1028 Event Hopping (BOI22_events) C++14
100 / 100
162 ms 18156 KB
#include <bits/stdc++.h>
using namespace std;
#define st first
#define ed second

int n, q;
pair<int, int> event[101010];

vector<int> com;

struct node {
    int mn, idx;
} tree[808080];

node merger(node lt_val, node rt_val) {
    node ret;
    ret.mn = min(lt_val.mn, rt_val.mn);
    if (lt_val.mn == rt_val.mn) {
        ret.idx = (event[lt_val.idx].ed > event[rt_val.idx].ed ? lt_val.idx : rt_val.idx);
    }
    if (lt_val.mn < rt_val.mn) {
        ret.idx = lt_val.idx;
    }
    if (lt_val.mn > rt_val.mn) {
        ret.idx = rt_val.idx;
    }
    return ret;
}

void init(int v, int st, int ed) {
    if (st == ed) {
        tree[v].mn = 1e9;
        tree[v].idx = 0;
        return;
    }
    int mid = (st + ed) / 2;
    init(2 * v, st, mid);
    init(2 * v + 1, mid + 1, ed);
    tree[v] = merger(tree[2 * v], tree[2 * v + 1]);
}

void update(int v, int st, int ed, int idx, int val, int i) {
    if (st == idx && ed == idx) {
        if (tree[v].mn > val) {
            tree[v].mn = val;
            tree[v].idx = i;
        }
        if (tree[v].mn == val && event[i].ed > event[tree[v].idx].ed) {
            tree[v].idx = i;
        }
        return;
    }
    if (st > idx || ed < idx) return;
    int mid = (st + ed) / 2;
    update(2 * v, st, mid, idx, val, i);
    update(2 * v + 1, mid + 1, ed, idx, val, i);
    tree[v] = merger(tree[2 * v], tree[2 * v + 1]);
}

node get(int v, int st, int ed, int lt, int rt) {
    if (lt > ed || rt < st) return {(int)1e9, 0};
    if (lt <= st && ed <= rt) return tree[v];
    int mid = (st + ed) / 2;
    return merger(
        get(2 * v, st, mid, lt, rt),
        get(2 * v + 1, mid + 1, ed, lt, rt)
    );
}

int sparse[101010][20];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> q;
    for (int i = 1; i <= n; i++) cin >> event[i].st >> event[i].ed;

    for (int i = 1; i <= n; i++) {
        com.push_back(event[i].st);
        com.push_back(event[i].ed);
    }
    sort(com.begin(), com.end());
    com.erase(unique(com.begin(), com.end()), com.end());
    for (int i = 1; i <= n; i++) {
        event[i].st = lower_bound(com.begin(), com.end(), event[i].st) - com.begin() + 1;
        event[i].ed = lower_bound(com.begin(), com.end(), event[i].ed) - com.begin() + 1;
        //cout << event[i].st << " " << event[i].ed << "\n";
    }

    init(1, 1, com.size());
    for (int i = 1; i <= n; i++) update(1, 1, com.size(), event[i].ed, event[i].st, i);
    for (int i = 1; i <= n; i++) {
        sparse[i][0] = get(1, 1, com.size(), event[i].st, event[i].ed).idx;
        //cout << sparse[i][0] << "\n";
    }
    for (int k = 1; k <= log2(n); k++) {
        for (int i = 1; i <= n; i++) {
            sparse[i][k] = sparse[sparse[i][k - 1]][k - 1];
        }
    }

    while (q--) {
        int s, e;
        cin >> s >> e;

        if (s == e) {
            cout << "0\n";
            continue;
        }

        int ans = 0;
        for (int k = log2(n); k >= 0; k--) {
            if (event[sparse[e][k]].st > event[s].st) {
                e = sparse[e][k];
                ans += (1 << k);
            }
        }
        if (event[e].st <= event[s].ed && event[s].ed <= event[e].ed) ans += 1;
        else {
            e = sparse[e][0]; ans += 1;
            if (event[e].st <= event[s].ed && event[s].ed <= event[e].ed) ans += 1;
            else {
                cout << "impossible\n";
                continue;
            }
        }
        cout << ans << "\n";
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 113 ms 12392 KB Output is correct
3 Correct 119 ms 12440 KB Output is correct
4 Correct 148 ms 12468 KB Output is correct
5 Correct 135 ms 13188 KB Output is correct
6 Correct 127 ms 13124 KB Output is correct
7 Correct 127 ms 13176 KB Output is correct
8 Correct 117 ms 18100 KB Output is correct
9 Correct 119 ms 18156 KB Output is correct
10 Correct 122 ms 15884 KB Output is correct
11 Correct 123 ms 18024 KB Output is correct
12 Correct 81 ms 15324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 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 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 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 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 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 340 KB Output is correct
19 Correct 30 ms 1508 KB Output is correct
20 Correct 27 ms 1520 KB Output is correct
21 Correct 31 ms 1788 KB Output is correct
22 Correct 25 ms 1792 KB Output is correct
23 Correct 25 ms 1784 KB Output is correct
24 Correct 30 ms 1748 KB Output is correct
25 Correct 25 ms 1444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 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 0 ms 284 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 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 340 KB Output is correct
19 Correct 103 ms 11896 KB Output is correct
20 Correct 88 ms 11900 KB Output is correct
21 Correct 105 ms 11828 KB Output is correct
22 Correct 100 ms 13944 KB Output is correct
23 Correct 114 ms 15872 KB Output is correct
24 Correct 115 ms 15812 KB Output is correct
25 Correct 26 ms 11128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 114 ms 12424 KB Output is correct
2 Correct 125 ms 12504 KB Output is correct
3 Correct 130 ms 12404 KB Output is correct
4 Correct 119 ms 15048 KB Output is correct
5 Correct 121 ms 12900 KB Output is correct
6 Correct 138 ms 14836 KB Output is correct
7 Correct 143 ms 15328 KB Output is correct
8 Correct 162 ms 15392 KB Output is correct
9 Correct 116 ms 14492 KB Output is correct
10 Correct 140 ms 14916 KB Output is correct
11 Correct 136 ms 14724 KB Output is correct
12 Correct 139 ms 14964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 113 ms 12392 KB Output is correct
3 Correct 119 ms 12440 KB Output is correct
4 Correct 148 ms 12468 KB Output is correct
5 Correct 135 ms 13188 KB Output is correct
6 Correct 127 ms 13124 KB Output is correct
7 Correct 127 ms 13176 KB Output is correct
8 Correct 117 ms 18100 KB Output is correct
9 Correct 119 ms 18156 KB Output is correct
10 Correct 122 ms 15884 KB Output is correct
11 Correct 123 ms 18024 KB Output is correct
12 Correct 81 ms 15324 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 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 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 30 ms 1508 KB Output is correct
32 Correct 27 ms 1520 KB Output is correct
33 Correct 31 ms 1788 KB Output is correct
34 Correct 25 ms 1792 KB Output is correct
35 Correct 25 ms 1784 KB Output is correct
36 Correct 30 ms 1748 KB Output is correct
37 Correct 25 ms 1444 KB Output is correct
38 Correct 0 ms 284 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 103 ms 11896 KB Output is correct
48 Correct 88 ms 11900 KB Output is correct
49 Correct 105 ms 11828 KB Output is correct
50 Correct 100 ms 13944 KB Output is correct
51 Correct 114 ms 15872 KB Output is correct
52 Correct 115 ms 15812 KB Output is correct
53 Correct 26 ms 11128 KB Output is correct
54 Correct 114 ms 12424 KB Output is correct
55 Correct 125 ms 12504 KB Output is correct
56 Correct 130 ms 12404 KB Output is correct
57 Correct 119 ms 15048 KB Output is correct
58 Correct 121 ms 12900 KB Output is correct
59 Correct 138 ms 14836 KB Output is correct
60 Correct 143 ms 15328 KB Output is correct
61 Correct 162 ms 15392 KB Output is correct
62 Correct 116 ms 14492 KB Output is correct
63 Correct 140 ms 14916 KB Output is correct
64 Correct 136 ms 14724 KB Output is correct
65 Correct 139 ms 14964 KB Output is correct
66 Correct 0 ms 340 KB Output is correct
67 Correct 119 ms 15560 KB Output is correct
68 Correct 119 ms 15596 KB Output is correct
69 Correct 131 ms 15584 KB Output is correct
70 Correct 148 ms 15588 KB Output is correct
71 Correct 126 ms 15596 KB Output is correct
72 Correct 123 ms 15728 KB Output is correct
73 Correct 124 ms 18144 KB Output is correct
74 Correct 140 ms 18100 KB Output is correct
75 Correct 127 ms 15960 KB Output is correct
76 Correct 120 ms 18072 KB Output is correct
77 Correct 79 ms 15300 KB Output is correct
78 Correct 1 ms 340 KB Output is correct
79 Correct 1 ms 480 KB Output is correct
80 Correct 1 ms 468 KB Output is correct
81 Correct 1 ms 468 KB Output is correct
82 Correct 1 ms 476 KB Output is correct
83 Correct 1 ms 468 KB Output is correct
84 Correct 2 ms 468 KB Output is correct
85 Correct 1 ms 468 KB Output is correct
86 Correct 30 ms 2436 KB Output is correct
87 Correct 27 ms 2516 KB Output is correct
88 Correct 30 ms 2672 KB Output is correct
89 Correct 28 ms 2896 KB Output is correct
90 Correct 25 ms 2752 KB Output is correct
91 Correct 25 ms 2768 KB Output is correct
92 Correct 26 ms 2432 KB Output is correct
93 Correct 96 ms 13844 KB Output is correct
94 Correct 88 ms 12940 KB Output is correct
95 Correct 115 ms 13872 KB Output is correct
96 Correct 114 ms 15892 KB Output is correct
97 Correct 117 ms 15832 KB Output is correct
98 Correct 111 ms 15880 KB Output is correct
99 Correct 22 ms 11040 KB Output is correct
100 Correct 139 ms 17800 KB Output is correct
101 Correct 138 ms 17672 KB Output is correct
102 Correct 137 ms 17848 KB Output is correct
103 Correct 152 ms 17432 KB Output is correct
104 Correct 141 ms 17184 KB Output is correct
105 Correct 135 ms 17368 KB Output is correct
106 Correct 155 ms 14952 KB Output is correct
107 Correct 134 ms 14980 KB Output is correct
108 Correct 138 ms 15736 KB Output is correct
109 Correct 132 ms 15648 KB Output is correct
110 Correct 148 ms 17632 KB Output is correct
111 Correct 149 ms 17632 KB Output is correct