Submission #954442

#TimeUsernameProblemLanguageResultExecution timeMemory
954442TAhmed33Event Hopping (BOI22_events)C++98
0 / 100
1586 ms6628 KiB
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 25; const int B = 18; int dp[B][MAXN]; int n, q; pair <int, int> a[MAXN]; int jump (int a, int b) { for (int i = B - 1; i >= 0; i--) { if (b & (1 << i)) { a = dp[i][a]; } } return a; } int main () { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> q; for (int i = 1; i <= n; i++) { cin >> a[i].first >> a[i].second; } for (int i = 1; i <= n; i++) { dp[0][i] = i; for (int j = 1; j <= n; j++) { if (a[j].second >= a[i].first && a[j].second <= a[i].second) { if (a[j].first < a[dp[0][i]].first) dp[0][i] = j; } } } for (int i = 1; i < B; i++) { for (int j = 1; j <= n; j++) { dp[i][j] = dp[i - 1][dp[i - 1][j]]; } } while (q--) { int x, y; cin >> x >> y; if (x == y) { cout << "0\n"; continue; } int ans = 0; if (a[y].first <= a[x].second) { ans = -1; } else { for (int i = B - 1; i >= 0; i--) { auto g = dp[i][y]; if (!g) continue; if (a[g].first > a[x].second) { ans ^= 1 << i; y = g; } } } y = jump(y, 1); if (a[x].second <= a[y].second && a[x].second >= a[y].first) { cout << ans + 2 << '\n'; } else { cout << "impossible\n"; } } }
#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...