답안 #597494

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
597494 2022-07-16T07:14:18 Z yanndev Event Hopping (BOI22_events) C++17
100 / 100
196 ms 27408 KB
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;

struct Inter {
    int rl;
    int rr;
    int id;
    bool operator<(const Inter& other) const {
        if (rr != other.rr)
            return rr < other.rr;
        if (rl != other.rl)
            return rl < other.rl;
        return id < other.id;
    }
};

const int MX_N = 1e5 + 42;
const int TREE_SIZE = (int)1 << 19;

int rl[MX_N];
int rr[MX_N];
int jump[20][MX_N];
int tree[2 * TREE_SIZE + 42];

void insert(int idx, int val, bool force = false) {
    idx += TREE_SIZE;
    if ((force) || (rl[val] < rl[tree[idx]]))
        tree[idx] = val;
    for (int i = idx / 2; i > 0; i /= 2) {
        if (rl[tree[2 * i]] < rl[tree[2 * i + 1]])
            tree[i] = tree[2 * i];
        else
            tree[i] = tree[2 * i + 1];
    }
}

int query(int left, int right) {
    left += TREE_SIZE;
    right += TREE_SIZE;

    int mnVal = 1e9;
    int id = -1;
    while (left <= right) {
        if (left % 2 == 1) {
            int newId = tree[left];
            if (rl[newId] < mnVal) {
                mnVal = rl[newId];
                id = newId;
            }
            left++;
        }

        if (right % 2 == 0) {
            int newId = tree[right];
            if (rl[newId] < mnVal) {
                mnVal = rl[newId];
                id = newId;
            }
            right--;
        }

        left /= 2;
        right /= 2;
    }

    return id;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n, q;
    cin >> n >> q;

    vector<Inter> ranges {};
    vector<int> vals {};

    for (int i = 0; i < n; i++) {
        cin >> rl[i] >> rr[i];
        vals.push_back(rl[i]);
        vals.push_back(rr[i]);
    }

    sort(vals.begin(), vals.end());
    vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
    
    for (int i = 0; i < n; i++) {
        rl[i] = lower_bound(vals.begin(), vals.end(), rl[i]) - vals.begin();
        rr[i] = lower_bound(vals.begin(), vals.end(), rr[i]) - vals.begin();
        ranges.push_back({rl[i], rr[i], i});
    }

    rl[n] = rr[n] = 1e9 + 42;
    for (int i = 0; i < (int)vals.size(); i++)
        insert(i, n, true);

    sort(ranges.begin(), ranges.end());
    for (int i = 0; i < n; i++) {
        //cout << "doing range " << ranges[i].id << ' ' << ranges[i].rl << ' ' << ranges[i].rr << '\n';
        jump[0][ranges[i].id] = query(ranges[i].rl, ranges[i].rr);
        //cout << "query is " << jump[0][ranges[i].id] << '\n';
        if (jump[0][ranges[i].id] == -1)
            jump[0][ranges[i].id] = ranges[i].id;

        //cout << "inserting at " << ranges[i].rr << '\n';
        insert(ranges[i].rr, ranges[i].id);
        //cout << "val at pos " << query(ranges[i].rr, ranges[i].rr) << '\n';
    }
    
    for (int i = 1; i <= 19; i++)
        for (int j = 0; j < n; j++)
            jump[i][j] = jump[i - 1][jump[i - 1][j]];

    for (int i = 0; i < q; i++) {
        int s, e;
        cin >> s >> e;
        s--, e--;

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

        if (rl[e] <= rr[s] && rr[s] <= rr[e]) {
            cout << "1\n";
            continue;
        }

        if (rr[s] > rr[e]) {
            cout << "impossible\n";
            continue;
        }

        int ans = 0;

        //cout << "start has " << rl[s] << ' ' << rr[s] << '\n';
        //cout << "bef jump left is " << rl[e] << " right is " << rr[e] << '\n';

        for (int bit = 19; bit >= 0; bit--) {
            int nxt = jump[bit][e];
            if (rl[nxt] > rr[s]) {
                //cout << "nxt has " << rl[nxt] << ' ' << rr[nxt] << '\n';
                //cout << "jumping of " << (1 << bit) << '\n';
                ans += (1 << bit);
                e = nxt;
            }
        }

        //cout << "preans " << ans << '\n';
        //cout << "bef end left is " << rl[e] << " right is " << rr[e] << '\n';

        ans++;
        e = jump[0][e];
        //cout << "last jump left is " << rl[e] << " right is " << rr[e] << '\n';
        if (rl[e] <= rr[s]) {
            cout << ans + 1 << '\n';
        } else {
            cout << "impossible\n";
        }
    }
}

/*

7 1
1 2
3 4
1 5
6 7
5 10
10 20
15 20
1 6

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 118 ms 25116 KB Output is correct
3 Correct 139 ms 25192 KB Output is correct
4 Correct 194 ms 25112 KB Output is correct
5 Correct 148 ms 25336 KB Output is correct
6 Correct 144 ms 25516 KB Output is correct
7 Correct 155 ms 25672 KB Output is correct
8 Correct 173 ms 27260 KB Output is correct
9 Correct 161 ms 27248 KB Output is correct
10 Correct 158 ms 25696 KB Output is correct
11 Correct 169 ms 26312 KB Output is correct
12 Correct 71 ms 25784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 2 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 724 KB Output is correct
17 Correct 2 ms 724 KB Output is correct
18 Correct 1 ms 724 KB Output is correct
19 Correct 31 ms 2408 KB Output is correct
20 Correct 30 ms 2416 KB Output is correct
21 Correct 36 ms 2700 KB Output is correct
22 Correct 26 ms 2764 KB Output is correct
23 Correct 23 ms 2612 KB Output is correct
24 Correct 23 ms 2700 KB Output is correct
25 Correct 29 ms 2340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 2 ms 760 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 2 ms 724 KB Output is correct
16 Correct 2 ms 724 KB Output is correct
17 Correct 2 ms 724 KB Output is correct
18 Correct 1 ms 724 KB Output is correct
19 Correct 90 ms 24748 KB Output is correct
20 Correct 93 ms 24700 KB Output is correct
21 Correct 97 ms 24684 KB Output is correct
22 Correct 92 ms 25272 KB Output is correct
23 Correct 103 ms 26296 KB Output is correct
24 Correct 104 ms 26224 KB Output is correct
25 Correct 47 ms 23160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 110 ms 25144 KB Output is correct
2 Correct 148 ms 25164 KB Output is correct
3 Correct 191 ms 25088 KB Output is correct
4 Correct 168 ms 27184 KB Output is correct
5 Correct 146 ms 25552 KB Output is correct
6 Correct 169 ms 26896 KB Output is correct
7 Correct 176 ms 26880 KB Output is correct
8 Correct 141 ms 27044 KB Output is correct
9 Correct 110 ms 26168 KB Output is correct
10 Correct 149 ms 26500 KB Output is correct
11 Correct 143 ms 26288 KB Output is correct
12 Correct 152 ms 26468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 118 ms 25116 KB Output is correct
3 Correct 139 ms 25192 KB Output is correct
4 Correct 194 ms 25112 KB Output is correct
5 Correct 148 ms 25336 KB Output is correct
6 Correct 144 ms 25516 KB Output is correct
7 Correct 155 ms 25672 KB Output is correct
8 Correct 173 ms 27260 KB Output is correct
9 Correct 161 ms 27248 KB Output is correct
10 Correct 158 ms 25696 KB Output is correct
11 Correct 169 ms 26312 KB Output is correct
12 Correct 71 ms 25784 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 724 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 1 ms 724 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 1 ms 724 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 2 ms 724 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
26 Correct 1 ms 724 KB Output is correct
27 Correct 1 ms 724 KB Output is correct
28 Correct 1 ms 724 KB Output is correct
29 Correct 2 ms 724 KB Output is correct
30 Correct 1 ms 724 KB Output is correct
31 Correct 31 ms 2408 KB Output is correct
32 Correct 30 ms 2416 KB Output is correct
33 Correct 36 ms 2700 KB Output is correct
34 Correct 26 ms 2764 KB Output is correct
35 Correct 23 ms 2612 KB Output is correct
36 Correct 23 ms 2700 KB Output is correct
37 Correct 29 ms 2340 KB Output is correct
38 Correct 0 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 2 ms 760 KB Output is correct
41 Correct 1 ms 724 KB Output is correct
42 Correct 1 ms 724 KB Output is correct
43 Correct 2 ms 724 KB Output is correct
44 Correct 2 ms 724 KB Output is correct
45 Correct 2 ms 724 KB Output is correct
46 Correct 1 ms 724 KB Output is correct
47 Correct 90 ms 24748 KB Output is correct
48 Correct 93 ms 24700 KB Output is correct
49 Correct 97 ms 24684 KB Output is correct
50 Correct 92 ms 25272 KB Output is correct
51 Correct 103 ms 26296 KB Output is correct
52 Correct 104 ms 26224 KB Output is correct
53 Correct 47 ms 23160 KB Output is correct
54 Correct 110 ms 25144 KB Output is correct
55 Correct 148 ms 25164 KB Output is correct
56 Correct 191 ms 25088 KB Output is correct
57 Correct 168 ms 27184 KB Output is correct
58 Correct 146 ms 25552 KB Output is correct
59 Correct 169 ms 26896 KB Output is correct
60 Correct 176 ms 26880 KB Output is correct
61 Correct 141 ms 27044 KB Output is correct
62 Correct 110 ms 26168 KB Output is correct
63 Correct 149 ms 26500 KB Output is correct
64 Correct 143 ms 26288 KB Output is correct
65 Correct 152 ms 26468 KB Output is correct
66 Correct 0 ms 468 KB Output is correct
67 Correct 112 ms 25332 KB Output is correct
68 Correct 134 ms 25352 KB Output is correct
69 Correct 196 ms 25264 KB Output is correct
70 Correct 140 ms 25528 KB Output is correct
71 Correct 149 ms 25692 KB Output is correct
72 Correct 152 ms 25728 KB Output is correct
73 Correct 161 ms 27408 KB Output is correct
74 Correct 166 ms 27340 KB Output is correct
75 Correct 151 ms 25648 KB Output is correct
76 Correct 156 ms 26376 KB Output is correct
77 Correct 71 ms 25812 KB Output is correct
78 Correct 0 ms 468 KB Output is correct
79 Correct 1 ms 724 KB Output is correct
80 Correct 1 ms 764 KB Output is correct
81 Correct 1 ms 756 KB Output is correct
82 Correct 1 ms 724 KB Output is correct
83 Correct 1 ms 760 KB Output is correct
84 Correct 1 ms 724 KB Output is correct
85 Correct 1 ms 724 KB Output is correct
86 Correct 30 ms 2404 KB Output is correct
87 Correct 31 ms 2508 KB Output is correct
88 Correct 32 ms 2764 KB Output is correct
89 Correct 27 ms 2704 KB Output is correct
90 Correct 23 ms 2680 KB Output is correct
91 Correct 21 ms 2608 KB Output is correct
92 Correct 28 ms 2396 KB Output is correct
93 Correct 87 ms 24636 KB Output is correct
94 Correct 88 ms 24840 KB Output is correct
95 Correct 111 ms 24704 KB Output is correct
96 Correct 90 ms 25212 KB Output is correct
97 Correct 106 ms 26260 KB Output is correct
98 Correct 103 ms 26340 KB Output is correct
99 Correct 51 ms 23148 KB Output is correct
100 Correct 185 ms 26976 KB Output is correct
101 Correct 161 ms 26944 KB Output is correct
102 Correct 141 ms 27176 KB Output is correct
103 Correct 157 ms 26692 KB Output is correct
104 Correct 149 ms 26416 KB Output is correct
105 Correct 146 ms 26624 KB Output is correct
106 Correct 142 ms 25336 KB Output is correct
107 Correct 176 ms 25592 KB Output is correct
108 Correct 146 ms 25572 KB Output is correct
109 Correct 144 ms 25568 KB Output is correct
110 Correct 149 ms 26884 KB Output is correct
111 Correct 156 ms 26940 KB Output is correct