Submission #716583

# Submission time Handle Problem Language Result Execution time Memory
716583 2023-03-30T13:01:55 Z ismayil Event Hopping (BOI22_events) C++17
0 / 100
1500 ms 44176 KB
#include<bits/stdc++.h>
using namespace std;
const int MAX = 3e5 + 5;
int n, q;
int a[20][MAX], S[MAX], E[MAX], v[MAX], id[MAX], _prev[20][MAX];
int lg(int i){
    return i ? __builtin_clz(1) - __builtin_clz(i) : -1;
}
void init(){
    for(int i = 1; i < 20; i++){
        for(int j = 1; j + (1 << i) - 1 < MAX; j++){
            if(v[a[i - 1][j]] < v[a[i - 1][j + (1 << (i - 1))]]){
                a[i][j] = a[i - 1][j];
            }else{
                a[i][j] = a[i - 1][j + (1 << (i - 1))];
            }
        }
    }
}
int query(int l, int r){
    int k = lg(r - l + 1);
    if(v[a[k][l]] <= v[a[k][r - (1 << k) + 1]]) return id[a[k][l]];
    return id[a[k][r - (1 << k) + 1]];
}
void solve(){
    
    cin >> n >> q;
    map<int, int> m;
    for(int i = 1; i <= n; i++) cin >> S[i] >> E[i], m[S[i]], m[E[i]];
    int idx = 0;
    for(auto& i : m) i.second = ++idx;
    
    for(int i = 1; i <= n; i++) S[i] = m[S[i]], E[i] = m[E[i]];
    fill(v + 1, v + 1 + m.size(), MAX);
    for (int i = 1; i <= n; i++)
    {
        if(v[E[i]] > S[i]){
            v[E[i]] = S[i];
            id[E[i]] = i;
        }
    }
    for (int i = 1; i < MAX; i++) a[0][i] = i;
    init();
    for (int i = 1; i <= n; i++){
        _prev[0][i] = query(S[i], E[i]);
        if(_prev[0][i] == i) _prev[0][i] = 0;
    }
    for (int i = 1; i < 20; i++)
    {
        for(int j = 1; j <= n; j++){
            _prev[i][j] = _prev[i - 1][_prev[i - 1][j]];
        }
    }
    
    while (q--)
    {
        int s, e;
        cin >> s >> e;
        if(s == e){
            cout << 0 << endl;
            continue;
        }
        if(E[s] > E[e]){
            cout << "impossible" << endl;
            continue;
        }
        if(S[e] <= E[s] && E[s] <= E[e]){
            cout << 1 << endl;
            continue;
        }
        int ans = 0;
        for(int i = 19; i >= 0; i--){
            if(S[_prev[i][e]] > E[s]){
                e = _prev[i][e];
                ans += (1 << i);
                cout << e << " " << (1 << i) << endl;
            }
            
        }
        e = _prev[0][e];
        if(e == 0) cout << "impossible" << endl;
        else cout << ans + 2 << endl;
    }
    
}
int main(){
    int t = 1;
    //cin>>t;
    for(int i = 1; i <= t; i++){
        solve();
    }
}
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 20692 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 20692 KB Output is correct
2 Incorrect 14 ms 20640 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 20692 KB Output is correct
2 Incorrect 14 ms 20640 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 20692 KB Output is correct
2 Incorrect 14 ms 20640 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1507 ms 44176 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 20692 KB Output isn't correct
2 Halted 0 ms 0 KB -