Submission #838527

#TimeUsernameProblemLanguageResultExecution timeMemory
838527skittles1412Event Hopping (BOI22_events)C++17
10 / 100
1594 ms3148 KiB
#include "bits/extc++.h"

using namespace std;

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t;
    ((cerr << " | " << u), ...);
    cerr << endl;
}

#ifdef DEBUG
#define dbg(...)                                              \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
    dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr   \
    if (false) \
    cerr
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))

void solve() {
    int n, q;
    cin >> n >> q;

    vector<pair<int, int>> arr(n);
    for (auto& [l, r] : arr) {
        cin >> l >> r;
    }

    while (q--) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;

        if (u == v) {
            cout << 0 << endl;
            continue;
        }

        int pos = arr[u].second, ans = 0;

        while (pos < arr[v].first) {
            int n_pos = -1;

            for (auto& [l, r] : arr) {
                if (l <= pos && pos <= r && r <= arr[v].second) {
                    n_pos = max(n_pos, r);
                }
            }

            if (n_pos == -1 || pos == n_pos) {
                break;
            }
            pos = n_pos;
            ans++;
        }

        if (arr[v].first <= pos && pos <= arr[v].second) {
            ans++;
            cout << ans << endl;
        } else {
            cout << "impossible" << endl;
        }
    }
}

int main() {
    cin.tie(nullptr);
    cin.exceptions(ios::failbit);
    ios_base::sync_with_stdio(false);
    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...