Submission #860838

# Submission time Handle Problem Language Result Execution time Memory
860838 2023-10-14T15:26:59 Z TahirAliyev Event Hopping (BOI22_events) C++17
100 / 100
328 ms 19792 KB
#include <bits/stdc++.h>

#define ll long long
#define pii pair<int, int>
#define oo 1e9

using namespace std;

struct DATA{
    int mn = oo, id;
};

const int MAX = 1e5 + 5, LOGMAX = 20;
int n, q;

pii arr[MAX];
DATA tree[8 * MAX];

DATA comb(DATA a, DATA b){
    if(a.mn < b.mn){
        return a;
    }
    else{
        return b;
    }
}

void update(int node, int l, int r, int pos, int val, int id){
    if(l == r){
        tree[node] = comb({val, id}, tree[node]);
        return;
    }
    int mid = (l + r) / 2;
    if(pos <= mid){
        update(2 * node, l, mid, pos, val, id);
    }
    else{
        update(2 * node + 1, mid + 1, r, pos, val, id);
    }
    tree[node] = comb(tree[2 * node], tree[2 * node + 1]);
}

DATA ask(int node, int l, int r, int ql, int qr){
    if(qr < l || r < ql) return DATA();
    if(ql <= l && r <= qr) return tree[node];
    int mid = (l + r) / 2;
    return comb(ask(2 * node, l, mid, ql, qr), ask(2 * node + 1, mid + 1, r, ql, qr));
}

void compress(){
    vector<pii> v;
    for(int i = 1; i <= n; i++){
        v.push_back({arr[i].first, i});
        v.push_back({arr[i].second, -i});
    }
    sort(v.begin(), v.end());
    int t = 0;
    for(int i = 0; i < 2 * n; i++){
        if(i == 0 || v[i].first != v[i - 1].first){
            t++;
        }
        if(v[i].second > 0){
            arr[v[i].second].first = t;
        }
        else{
            arr[-v[i].second].second = t;
        }
    }
}

int par[LOGMAX][MAX];

bool isReachable(int i, int j){
    return arr[j].first <= arr[i].second && arr[i].second <= arr[j].second;
}

int lift(int s, int d){
    int res = 2;
    if(isReachable(s, d)){
        return 1;
    }
    for(int i = LOGMAX - 1; i >= 0; i--){
        if(!isReachable(s, par[i][d]) && arr[par[i][d]].second >= arr[s].second){
            d = par[i][d];
            res += (1 << i);
        }
    }
    if(!isReachable(s, par[0][d])){
        return -1;
    }
    return res;
}

int main(){
    cin >> n >> q;
    for(int i = 1; i <= n; i++){
        cin >> arr[i].first >> arr[i].second;
    }
    compress();
    for(int i = 1; i <= n; i++){
        update(1, 1, 2 * n, arr[i].second, arr[i].first, i);
    }
    for(int i = 1; i <= n; i++){
        par[0][i] = ask(1, 1, 2 * n, arr[i].first, arr[i].second).id;
    }
    for(int j = 1; j < LOGMAX; j++){
        for(int i = 1; i <= n; i++){
            par[j][i] = par[j - 1][par[j - 1][i]];
        }
    }

    while(q--){
        int s, d; cin >> s >> d;
        if(s == d){
            cout << "0\n";
            continue;
        }
        int ans = lift(s, d);
        if(ans == -1){
            cout << "impossible\n";
        }
        else{
            cout << ans << '\n';
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 264 ms 18920 KB Output is correct
3 Correct 276 ms 19092 KB Output is correct
4 Correct 277 ms 19092 KB Output is correct
5 Correct 282 ms 19172 KB Output is correct
6 Correct 291 ms 19180 KB Output is correct
7 Correct 277 ms 19088 KB Output is correct
8 Correct 305 ms 19792 KB Output is correct
9 Correct 282 ms 19516 KB Output is correct
10 Correct 311 ms 19328 KB Output is correct
11 Correct 328 ms 19440 KB Output is correct
12 Correct 227 ms 18616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 3 ms 14808 KB Output is correct
5 Correct 3 ms 14800 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 3 ms 14684 KB Output is correct
8 Correct 3 ms 14680 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 3 ms 14808 KB Output is correct
5 Correct 3 ms 14800 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 3 ms 14684 KB Output is correct
8 Correct 3 ms 14680 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 2 ms 14684 KB Output is correct
11 Correct 2 ms 14684 KB Output is correct
12 Correct 4 ms 14684 KB Output is correct
13 Correct 3 ms 14684 KB Output is correct
14 Correct 3 ms 14684 KB Output is correct
15 Correct 3 ms 14684 KB Output is correct
16 Correct 3 ms 14936 KB Output is correct
17 Correct 3 ms 14684 KB Output is correct
18 Correct 4 ms 14684 KB Output is correct
19 Correct 172 ms 16168 KB Output is correct
20 Correct 161 ms 16356 KB Output is correct
21 Correct 213 ms 16888 KB Output is correct
22 Correct 160 ms 16628 KB Output is correct
23 Correct 157 ms 16592 KB Output is correct
24 Correct 169 ms 16536 KB Output is correct
25 Correct 158 ms 16096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 3 ms 14808 KB Output is correct
5 Correct 3 ms 14800 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 3 ms 14684 KB Output is correct
8 Correct 3 ms 14680 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 2 ms 14684 KB Output is correct
11 Correct 2 ms 14684 KB Output is correct
12 Correct 3 ms 14684 KB Output is correct
13 Correct 4 ms 14684 KB Output is correct
14 Correct 3 ms 14680 KB Output is correct
15 Correct 3 ms 14684 KB Output is correct
16 Correct 3 ms 14684 KB Output is correct
17 Correct 3 ms 14684 KB Output is correct
18 Correct 3 ms 14684 KB Output is correct
19 Correct 117 ms 17264 KB Output is correct
20 Correct 91 ms 16528 KB Output is correct
21 Correct 117 ms 17232 KB Output is correct
22 Correct 133 ms 17256 KB Output is correct
23 Correct 124 ms 17304 KB Output is correct
24 Correct 116 ms 17248 KB Output is correct
25 Correct 74 ms 16548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 263 ms 15764 KB Output is correct
2 Correct 277 ms 15852 KB Output is correct
3 Correct 274 ms 16048 KB Output is correct
4 Correct 314 ms 16364 KB Output is correct
5 Correct 271 ms 16272 KB Output is correct
6 Correct 327 ms 19272 KB Output is correct
7 Correct 316 ms 19084 KB Output is correct
8 Correct 293 ms 19368 KB Output is correct
9 Correct 135 ms 17248 KB Output is correct
10 Correct 291 ms 18984 KB Output is correct
11 Correct 311 ms 18576 KB Output is correct
12 Correct 282 ms 18672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 264 ms 18920 KB Output is correct
3 Correct 276 ms 19092 KB Output is correct
4 Correct 277 ms 19092 KB Output is correct
5 Correct 282 ms 19172 KB Output is correct
6 Correct 291 ms 19180 KB Output is correct
7 Correct 277 ms 19088 KB Output is correct
8 Correct 305 ms 19792 KB Output is correct
9 Correct 282 ms 19516 KB Output is correct
10 Correct 311 ms 19328 KB Output is correct
11 Correct 328 ms 19440 KB Output is correct
12 Correct 227 ms 18616 KB Output is correct
13 Correct 2 ms 14680 KB Output is correct
14 Correct 2 ms 14684 KB Output is correct
15 Correct 3 ms 14684 KB Output is correct
16 Correct 3 ms 14808 KB Output is correct
17 Correct 3 ms 14800 KB Output is correct
18 Correct 3 ms 14680 KB Output is correct
19 Correct 3 ms 14684 KB Output is correct
20 Correct 3 ms 14680 KB Output is correct
21 Correct 3 ms 14940 KB Output is correct
22 Correct 2 ms 14684 KB Output is correct
23 Correct 2 ms 14684 KB Output is correct
24 Correct 4 ms 14684 KB Output is correct
25 Correct 3 ms 14684 KB Output is correct
26 Correct 3 ms 14684 KB Output is correct
27 Correct 3 ms 14684 KB Output is correct
28 Correct 3 ms 14936 KB Output is correct
29 Correct 3 ms 14684 KB Output is correct
30 Correct 4 ms 14684 KB Output is correct
31 Correct 172 ms 16168 KB Output is correct
32 Correct 161 ms 16356 KB Output is correct
33 Correct 213 ms 16888 KB Output is correct
34 Correct 160 ms 16628 KB Output is correct
35 Correct 157 ms 16592 KB Output is correct
36 Correct 169 ms 16536 KB Output is correct
37 Correct 158 ms 16096 KB Output is correct
38 Correct 2 ms 14684 KB Output is correct
39 Correct 2 ms 14684 KB Output is correct
40 Correct 3 ms 14684 KB Output is correct
41 Correct 4 ms 14684 KB Output is correct
42 Correct 3 ms 14680 KB Output is correct
43 Correct 3 ms 14684 KB Output is correct
44 Correct 3 ms 14684 KB Output is correct
45 Correct 3 ms 14684 KB Output is correct
46 Correct 3 ms 14684 KB Output is correct
47 Correct 117 ms 17264 KB Output is correct
48 Correct 91 ms 16528 KB Output is correct
49 Correct 117 ms 17232 KB Output is correct
50 Correct 133 ms 17256 KB Output is correct
51 Correct 124 ms 17304 KB Output is correct
52 Correct 116 ms 17248 KB Output is correct
53 Correct 74 ms 16548 KB Output is correct
54 Correct 263 ms 15764 KB Output is correct
55 Correct 277 ms 15852 KB Output is correct
56 Correct 274 ms 16048 KB Output is correct
57 Correct 314 ms 16364 KB Output is correct
58 Correct 271 ms 16272 KB Output is correct
59 Correct 327 ms 19272 KB Output is correct
60 Correct 316 ms 19084 KB Output is correct
61 Correct 293 ms 19368 KB Output is correct
62 Correct 135 ms 17248 KB Output is correct
63 Correct 291 ms 18984 KB Output is correct
64 Correct 311 ms 18576 KB Output is correct
65 Correct 282 ms 18672 KB Output is correct
66 Correct 2 ms 14684 KB Output is correct
67 Correct 275 ms 19024 KB Output is correct
68 Correct 270 ms 19088 KB Output is correct
69 Correct 280 ms 18976 KB Output is correct
70 Correct 273 ms 19088 KB Output is correct
71 Correct 281 ms 19344 KB Output is correct
72 Correct 279 ms 19092 KB Output is correct
73 Correct 286 ms 19544 KB Output is correct
74 Correct 302 ms 19364 KB Output is correct
75 Correct 280 ms 19352 KB Output is correct
76 Correct 269 ms 19344 KB Output is correct
77 Correct 267 ms 18768 KB Output is correct
78 Correct 2 ms 14684 KB Output is correct
79 Correct 3 ms 14680 KB Output is correct
80 Correct 3 ms 14816 KB Output is correct
81 Correct 3 ms 14684 KB Output is correct
82 Correct 3 ms 14684 KB Output is correct
83 Correct 3 ms 14680 KB Output is correct
84 Correct 4 ms 14776 KB Output is correct
85 Correct 3 ms 14684 KB Output is correct
86 Correct 162 ms 16228 KB Output is correct
87 Correct 167 ms 16384 KB Output is correct
88 Correct 162 ms 16628 KB Output is correct
89 Correct 160 ms 16628 KB Output is correct
90 Correct 176 ms 16560 KB Output is correct
91 Correct 159 ms 16372 KB Output is correct
92 Correct 159 ms 16100 KB Output is correct
93 Correct 120 ms 17176 KB Output is correct
94 Correct 92 ms 16500 KB Output is correct
95 Correct 116 ms 17040 KB Output is correct
96 Correct 110 ms 17196 KB Output is correct
97 Correct 130 ms 17264 KB Output is correct
98 Correct 116 ms 17340 KB Output is correct
99 Correct 74 ms 16548 KB Output is correct
100 Correct 300 ms 19164 KB Output is correct
101 Correct 283 ms 19176 KB Output is correct
102 Correct 309 ms 19168 KB Output is correct
103 Correct 275 ms 18664 KB Output is correct
104 Correct 300 ms 18524 KB Output is correct
105 Correct 286 ms 18880 KB Output is correct
106 Correct 290 ms 18324 KB Output is correct
107 Correct 261 ms 18432 KB Output is correct
108 Correct 276 ms 19088 KB Output is correct
109 Correct 294 ms 19088 KB Output is correct
110 Correct 286 ms 18916 KB Output is correct
111 Correct 286 ms 19072 KB Output is correct