Submission #644784

#TimeUsernameProblemLanguageResultExecution timeMemory
644784FedShatEvent Hopping (BOI22_events)C++17
0 / 100
1594 ms2416 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr int INF = numeric_limits<int>::max() / 2; constexpr ll INFLL = numeric_limits<ll>::max() / 2; template<class T> istream &operator>>(istream &is, vector<T> &a) { for (auto &i : a) { is >> i; } return is; } int main() { cin.tie(0)->sync_with_stdio(0); #ifdef __APPLE__ freopen("input.txt", "r", stdin); #endif int n, q; cin >> n >> q; vector<array<int, 3>> lr(n); for (int i = 0; i < n; ++i) { cin >> lr[i][0] >> lr[i][1]; lr[i][2] = i; } sort(lr.begin(), lr.end()); vector<int> p(n); for (int i = 0; i < n; ++i) { p[lr[i][2]] = i; } auto can = [&](int i, int j) {// i -> j auto [li, ri, ii] = lr[i]; auto [lj, rj, jj] = lr[j]; return lj <= ri && ri <= rj; }; vector<int> prv(n); for (int i = 0; i < n; ++i) { // int ind = -1; // for (int j = 0; j < i; ++j) { // if (can(j, i)) { // ind = j; // break; // } // } // prv[i] = ind; int l = -1, r = i - 1; while (l + 1 < r) { int m = (l + r) / 2; if (can(m, i)) { r = m; } else { l = m; } } if (r != -1 && can(r, i)) { prv[i] = r; } else { prv[i] = -1; } } while (q--) { int a, b; cin >> a >> b; --a; --b; a = p[a]; b = p[b]; int ans = 0; while (!can(a, b)) { if (prv[b] == -1) { ans = -1; break; } b = prv[b]; ++ans; } if (a != b && ans != -1) { ++ans; } if (ans == -1) { cout << "impossible\n"; } else { cout << ans << "\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...