Submission #716575

#TimeUsernameProblemLanguageResultExecution timeMemory
716575ismayilEvent Hopping (BOI22_events)C++17
0 / 100
428 ms38584 KiB
#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; } int ans = 0; for(int i = 19; i >= 0; i--){ if(S[_prev[i][e]] > E[s]){ e = _prev[i][e]; ans += (1 << i); } } e = _prev[0][e]; ans++; if(e == 0) cout << "impossible" << endl; else cout << ans + 1 << endl; } } int main(){ int t = 1; //cin>>t; for(int i = 1; i <= t; i++){ solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...