Submission #949702

# Submission time Handle Problem Language Result Execution time Memory
949702 2024-03-19T15:20:23 Z sysia Event Hopping (BOI22_events) C++17
100 / 100
482 ms 51236 KB
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 20;
const int mod = 998244353;

struct Tree{
    vector<T>tab;
    int size = 1;

    Tree(int n){
        while (size < n) size*=2;
        tab.assign(2*size, {oo, oo});
    }

    void update(int x, T v){
        x += size;
        tab[x] = min(tab[x], v);
        while (x){
            x/=2;
            tab[x] = min(tab[2*x], tab[2*x+1]);
        }
    }

    T query(int l, int r){
        T ans = {oo, oo};
        for (l += size-1, r += size+1; r-l>1; l/=2, r/=2) {
            if (!(l&1)) ans = min(ans, tab[l+1]);
            if (r&1) ans = min(ans, tab[r-1]);
        }
        return ans;
    }
};

void solve(){
    int n, q; cin >> n >> q;
    vector<T>a(n);
    vector<int>s;
    for (auto &[l, r]: a){
        cin >> l >> r;
        s.emplace_back(l);
        s.emplace_back(r);
    }
    sort(s.begin(), s.end());
    s.erase(unique(s.begin(), s.end()), s.end());
    auto get = [&](int x){ return (int)(lower_bound(s.begin(), s.end(), x)-s.begin());};
    for (auto &[l, r]: a){
        l = get(l); r = get(r);
        debug(l, r);
    }
    int m = (int)s.size();
   
    vector<vector<int>>L(m), R(m);
    for (int i = 0; i<n; i++){
        auto [l, r] = a[i];
        L[l].emplace_back(i);
        R[r].emplace_back(i);
    }
    vector up(n+1, vector<int>(K));
    Tree t(m+1);
    for (int i = 0; i<n; i++) t.update(a[i].second, {a[i].first, i});
    //przedzial o minimalnym lewym koncu, z ktorego da sie doskoczyc na i-ty 
    for (int rep = 0; rep < m; rep++){
        for (auto i: L[rep]){
            auto [left, prev] = t.query(a[i].first, a[i].second);
            up[i][0] = (prev == oo ? i : prev);
            debug(i, up[i][0]);
        }
    }
    for (int j = 1; j<K; j++){
        for (int i = 0; i<n;i++){
            up[i][j] = up[up[i][j-1]][j-1];
        }
    }
    while (q--){
        int l, r; cin >> l >> r;
        --l;--r;
        debug(l, r);
        int ans = 0;
        int ll = 0, rr = n;
        while (rr >= ll){
            int mid = (ll+rr)/2;
            int curr = r;
            for (int i = K-1; i>=0; i--){
                // debug(i, l, up[l][i]);
                if (mid&(1<<i)){
                    curr = up[curr][i];
                }
            }
            //umiem doskoczyc do a[curr]
            //czy z l da sie przeskoczyc na curr kiedys(?) --> a[curr].second >= a[l].second >= a[curr].first;
            if (a[curr].first > a[l].second) {
                ll = mid+1;
            } else {
                ans = mid;
                rr = mid-1;
            }
        }
        for (int i = K-1; i>=0; i--){
            if (ans&(1<<i)){
                r = up[r][i];
            }
        }
        debug(l, r);
        //czy z l da sie przeskoczyc na r
        if (a[r].first <= a[l].second && a[l].second <= a[r].second){
            cout << ans + (l!=r) << "\n";
        } else {
            cout << "impossible\n";
        }
    }
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    //cin >> t;
    while (t--) solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 173 ms 38784 KB Output is correct
3 Correct 283 ms 38656 KB Output is correct
4 Correct 326 ms 38660 KB Output is correct
5 Correct 248 ms 40136 KB Output is correct
6 Correct 222 ms 43120 KB Output is correct
7 Correct 207 ms 42956 KB Output is correct
8 Correct 156 ms 51104 KB Output is correct
9 Correct 160 ms 51140 KB Output is correct
10 Correct 315 ms 42188 KB Output is correct
11 Correct 271 ms 48108 KB Output is correct
12 Correct 113 ms 38480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 2 ms 860 KB Output is correct
8 Correct 1 ms 856 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 2 ms 860 KB Output is correct
8 Correct 1 ms 856 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 856 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 57 ms 3948 KB Output is correct
20 Correct 59 ms 3924 KB Output is correct
21 Correct 74 ms 4092 KB Output is correct
22 Correct 39 ms 3924 KB Output is correct
23 Correct 39 ms 4436 KB Output is correct
24 Correct 44 ms 4368 KB Output is correct
25 Correct 44 ms 4188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 2 ms 860 KB Output is correct
8 Correct 1 ms 856 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 2 ms 784 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 856 KB Output is correct
17 Correct 2 ms 856 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 109 ms 39980 KB Output is correct
20 Correct 116 ms 39376 KB Output is correct
21 Correct 117 ms 37320 KB Output is correct
22 Correct 113 ms 46128 KB Output is correct
23 Correct 123 ms 48844 KB Output is correct
24 Correct 141 ms 48920 KB Output is correct
25 Correct 39 ms 25800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 178 ms 38732 KB Output is correct
2 Correct 260 ms 38604 KB Output is correct
3 Correct 291 ms 38856 KB Output is correct
4 Correct 158 ms 48844 KB Output is correct
5 Correct 330 ms 42188 KB Output is correct
6 Correct 319 ms 50636 KB Output is correct
7 Correct 275 ms 50672 KB Output is correct
8 Correct 214 ms 51144 KB Output is correct
9 Correct 120 ms 48920 KB Output is correct
10 Correct 194 ms 50376 KB Output is correct
11 Correct 192 ms 50164 KB Output is correct
12 Correct 214 ms 50432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 173 ms 38784 KB Output is correct
3 Correct 283 ms 38656 KB Output is correct
4 Correct 326 ms 38660 KB Output is correct
5 Correct 248 ms 40136 KB Output is correct
6 Correct 222 ms 43120 KB Output is correct
7 Correct 207 ms 42956 KB Output is correct
8 Correct 156 ms 51104 KB Output is correct
9 Correct 160 ms 51140 KB Output is correct
10 Correct 315 ms 42188 KB Output is correct
11 Correct 271 ms 48108 KB Output is correct
12 Correct 113 ms 38480 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 2 ms 860 KB Output is correct
20 Correct 1 ms 856 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 1 ms 856 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
28 Correct 1 ms 860 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 57 ms 3948 KB Output is correct
32 Correct 59 ms 3924 KB Output is correct
33 Correct 74 ms 4092 KB Output is correct
34 Correct 39 ms 3924 KB Output is correct
35 Correct 39 ms 4436 KB Output is correct
36 Correct 44 ms 4368 KB Output is correct
37 Correct 44 ms 4188 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 1 ms 604 KB Output is correct
41 Correct 2 ms 784 KB Output is correct
42 Correct 1 ms 600 KB Output is correct
43 Correct 1 ms 724 KB Output is correct
44 Correct 1 ms 856 KB Output is correct
45 Correct 2 ms 856 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 109 ms 39980 KB Output is correct
48 Correct 116 ms 39376 KB Output is correct
49 Correct 117 ms 37320 KB Output is correct
50 Correct 113 ms 46128 KB Output is correct
51 Correct 123 ms 48844 KB Output is correct
52 Correct 141 ms 48920 KB Output is correct
53 Correct 39 ms 25800 KB Output is correct
54 Correct 178 ms 38732 KB Output is correct
55 Correct 260 ms 38604 KB Output is correct
56 Correct 291 ms 38856 KB Output is correct
57 Correct 158 ms 48844 KB Output is correct
58 Correct 330 ms 42188 KB Output is correct
59 Correct 319 ms 50636 KB Output is correct
60 Correct 275 ms 50672 KB Output is correct
61 Correct 214 ms 51144 KB Output is correct
62 Correct 120 ms 48920 KB Output is correct
63 Correct 194 ms 50376 KB Output is correct
64 Correct 192 ms 50164 KB Output is correct
65 Correct 214 ms 50432 KB Output is correct
66 Correct 0 ms 344 KB Output is correct
67 Correct 172 ms 42024 KB Output is correct
68 Correct 278 ms 41924 KB Output is correct
69 Correct 299 ms 41884 KB Output is correct
70 Correct 267 ms 42392 KB Output is correct
71 Correct 229 ms 42832 KB Output is correct
72 Correct 203 ms 42996 KB Output is correct
73 Correct 155 ms 51188 KB Output is correct
74 Correct 159 ms 50988 KB Output is correct
75 Correct 394 ms 42128 KB Output is correct
76 Correct 285 ms 48304 KB Output is correct
77 Correct 104 ms 38540 KB Output is correct
78 Correct 0 ms 344 KB Output is correct
79 Correct 1 ms 604 KB Output is correct
80 Correct 1 ms 604 KB Output is correct
81 Correct 1 ms 604 KB Output is correct
82 Correct 1 ms 780 KB Output is correct
83 Correct 2 ms 860 KB Output is correct
84 Correct 1 ms 1112 KB Output is correct
85 Correct 1 ms 604 KB Output is correct
86 Correct 48 ms 3864 KB Output is correct
87 Correct 56 ms 3748 KB Output is correct
88 Correct 74 ms 4148 KB Output is correct
89 Correct 48 ms 3924 KB Output is correct
90 Correct 39 ms 4408 KB Output is correct
91 Correct 48 ms 4436 KB Output is correct
92 Correct 54 ms 4176 KB Output is correct
93 Correct 108 ms 40408 KB Output is correct
94 Correct 103 ms 39372 KB Output is correct
95 Correct 115 ms 37328 KB Output is correct
96 Correct 109 ms 46036 KB Output is correct
97 Correct 141 ms 48968 KB Output is correct
98 Correct 133 ms 48968 KB Output is correct
99 Correct 41 ms 26000 KB Output is correct
100 Correct 292 ms 51236 KB Output is correct
101 Correct 274 ms 50636 KB Output is correct
102 Correct 217 ms 50892 KB Output is correct
103 Correct 236 ms 50496 KB Output is correct
104 Correct 165 ms 50236 KB Output is correct
105 Correct 210 ms 50380 KB Output is correct
106 Correct 308 ms 38652 KB Output is correct
107 Correct 482 ms 41240 KB Output is correct
108 Correct 169 ms 39368 KB Output is correct
109 Correct 170 ms 39368 KB Output is correct
110 Correct 231 ms 50888 KB Output is correct
111 Correct 194 ms 50632 KB Output is correct