Submission #828943

#TimeUsernameProblemLanguageResultExecution timeMemory
828943QwertyPiEvent Hopping (BOI22_events)C++14
0 / 100
1572 ms8268 KiB
#include <bits/stdc++.h> #pragma GCC optimize("unroll-loops") #pragma GCC optimize("Ofast") #define int long long using namespace std; const int N = 1e5 + 11; struct event{ int s, e, i; }; event a[N]; int p[N], R[N]; int32_t main(){ int n, q; cin >> n >> q; for(int i = 1; i <= n; i++){ int s, e; cin >> s >> e; a[i] = {s, e, i}; } sort(a + 1, a + n + 1, [](event u, event v){ return u.e <= v.e; }); R[n] = n; for(int i = n - 1; i >= 1; i--) R[i] = a[i].e == a[i + 1].e ? R[i + 1] : i; for(int i = 1; i <= n; i++) p[a[i].i] = i; for(int i = 0; i < q; i++){ int u, v; cin >> u >> v; u = p[u], v = p[v]; if(a[u].e > a[v].e){ cout << "impossible" << endl; continue; } if(u == v){ cout << 0 << endl; continue; } int ans = 0; while(a[u].e < a[v].s){ int far = u; for(int j = 1; j <= R[v]; j++){ if(a[far].e <= a[j].e && a[j].s <= a[u].e){ far = j; } } if(a[far].e == a[u].e){ ans = -1; break; }else{ u = far; ans++; } } cout << (ans == -1 ? "impossible" : to_string(ans + 1)) << endl; } }
#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...