Submission #1246428

#TimeUsernameProblemLanguageResultExecution timeMemory
1246428adam17Event Hopping (BOI22_events)C++20
0 / 100
65 ms15944 KiB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct tint{
    int a;
    int b;
    int c;
};

int N, Q, H, hh;
vector<tint> A;
vector<int> R;
vector<int> B;
vector<vector<int>> S;
vector<int> HL;
vector<int> O;
vector<vector<int>> D;

bool comp1(tint i, tint j) {
    if (i.b == j.b) {
        return i.a < j.a;
    } else {
        return i.b < j.b;
    }
}

bool jde(int i, int j) {
    return ((A[j].a <= A[i].b) && (A[i].b <= A[j].b));
}

int main() {
    cin >> N >> Q;
    A.resize(N);
    B.resize(N);
    R.resize(N);
    D.resize(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i].a >> A[i].b;
        A[i].c = i;
        // B[i] = i;
        R[i] = -1;
        D[i].resize(0);
    }
    sort(A.begin(), A.end(), comp1);
    for (int i = 0; i < N; i++) {
        B[A[i].c] = i;
        // cout << A[i].a << A[i].b << A[i].c << ' ';
    }
    // cout << endl;
    // for (int x: B) {
    //     cout << x << endl;
    // }
    // cout << endl;
    for (int j = 0; j < N; j++) {
        int i = j - 1;
        while (jde(i, j)) {
            if (R[i] == -1) {
                R[i] = j;
            }
            i--;
            if (i < 0) {
                goto abcd;
            }
        }
        abcd:;
    }

    hh = 1;
    H = 3;
    while (hh < N) {
        H++;
        hh *= 2;
    }
    S.resize(H);
    for (int h = 0; h < H; h++) {
        S[h].resize(N);
        for (int i = 0; i < N; i++) {
            S[h][i] = ((h == 0) ? R[i] : ((S[h - 1][i] == -1) ? -1 : S[h - 1][S[h - 1][i]]));
        }
    }

    O.resize(0);
    for (int i = 0; i < N; i++) {
        if (R[i] == -1) {
            O.push_back(i);
        } else {
            D[R[i]].push_back(i);
        }
    }
    HL.resize(N);
    while (O.size() > 0) {
        int o = O.back(); O.pop_back();
        HL[o] = ((R[o] == -1) ? 0 : HL[R[o]] + 1);
        for (int d: D[o]) {
            O.push_back(d);
        }
    }

    for (int q = 0; q < Q; q++) {
        int u, v;
        cin >> u >> v; u--; v--; u = B[u]; v = B[v];
        
        // if (HL[u] < HL[v]) {
        //     u ^= v;
        //     v ^= u;
        //     u ^= v;
        // }
        if (HL[u] < HL[v]) {
            if (jde(u, v)) {
                cout << "1\n";
            } else {
                cout << "impossible\n";
            }
        } else {
            int result = HL[u] - HL[v];
            hh = 1;
            int h = 0;
            while (HL[u] != HL[v]) {
                if ((HL[u] - HL[v]) % (2 * hh) != 0) {
                    u = S[h][u];
                }
                H++;
                hh *= 2;
            }
            if (u == v) {
                cout << result << endl;
            } else {
                cout << "impossible\n";
            }
        }
    }

    return 0;
}
#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...