Submission #644784

# Submission time Handle Problem Language Result Execution time Memory
644784 2022-09-25T08:37:05 Z FedShat Event Hopping (BOI22_events) C++17
0 / 100
1500 ms 2416 KB
#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 time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Execution timed out 1594 ms 2400 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Incorrect 1 ms 340 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Incorrect 1 ms 340 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Incorrect 1 ms 340 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1577 ms 2416 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Execution timed out 1594 ms 2400 KB Time limit exceeded
3 Halted 0 ms 0 KB -