Submission #895386

#TimeUsernameProblemLanguageResultExecution timeMemory
895386juliany2Railway Trip 2 (JOI22_ho_t4)C++17
100 / 100
176 ms37716 KiB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()

template<class T> struct ST {
    static constexpr T ID = {{{(int) 1e9, 0}, {(int) -1e9, 0}}}; // or whatever ID
    inline T comb(T a, T b) { return {min(a[0], b[0]), max(a[1], b[1])}; } // or whatever function

    int sz;
    vector<T> t;

    void init(int _sz, T val = ID) {
        t.assign((sz = _sz) * 2, ID);
    }
    void init(vector<T> &v) {
        t.resize((sz = v.size()) * 2);
        for (int i = 0; i < sz; ++i)
            t[i + sz] = v[i];
        for (int i = sz - 1; i; --i)
            t[i] = comb(t[i * 2], t[(i * 2) | 1]);
    }
    void upd(int i, T x) {
        for (t[i += sz] = x; i > 1; i >>= 1)
            t[i >> 1] = comb(t[i], t[i ^ 1]);
    }
    T query(int l, int r) {
        T ql = ID, qr = ID;
        for (l += sz, r += sz + 1; l < r; l >>= 1, r >>= 1) {
            if (l & 1) ql = comb(ql, t[l++]);
            if (r & 1) qr = comb(t[--r], qr);
        }
        return comb(ql, qr);
    }
};

const int N = 1e5 + 7, L = 20;
int n, k, m;
int l[N], r[N];
int lift[N][L][2];
vector<int> x[N], y[N];

int bmin(int a, int b) {
    return l[a] < l[b] ? a : b;
}

int bmax(int a, int b) {
    return r[a] > r[b] ? a : b;

}

int main() {
    cin.tie(0)->sync_with_stdio(false);

    cin >> n >> k >> m;

    for (int i = 1; i <= m; i++) {
        int a, b;
        cin >> a >> b;

        if (a < b) {
            int s = min(b, a + k - 1);
            x[a].push_back(b);
            x[s + 1].push_back(-b);
        }
        else {
            int s = max(b, a - k + 1);
            y[s].push_back(b);
            y[a + 1].push_back(-b);
        }
    }

    iota(l + 1, l + n + 1, 1);
    iota(r + 1, r + n + 1, 1);

    ST<array<array<int, 2>, 2>> st;
    st.init(n + 1);

    multiset<int> X, Y;
    for (int i = 1; i <= n; i++) {
        for (int a : x[i]) {
            if (a > 0)
                X.insert(a);
            else
                X.erase(X.find(-a));
        }
        for (int a : y[i]) {
            if (a > 0)
                Y.insert(a);
            else
                Y.erase(Y.find(-a));
        }

        if (X.size())
            r[i] = *X.rbegin();
        if (Y.size())
            l[i] = *Y.begin();

        st.upd(i, {{{l[i], i}, {r[i], i}}});
    }

    for (int i = 1; i <= n; i++) {
        lift[i][0][0] = st.query(l[i], r[i])[0][1];
        lift[i][0][1] = st.query(l[i], r[i])[1][1];
    }

    for (int j = 1; j < L; j++) {
        for (int i = 1; i <= n; i++) {
            lift[i][j][0] = bmin(lift[lift[i][j - 1][0]][j - 1][0], lift[lift[i][j - 1][1]][j - 1][0]);
            lift[i][j][1] = bmax(lift[lift[i][j - 1][0]][j - 1][1], lift[lift[i][j - 1][1]][j - 1][1]);
        }
    }

    int q;
    cin >> q;

    while (q--) {
        int s, t;
        cin >> s >> t;

        if (t >= l[s] && t <= r[s]) {
            cout << 1 << '\n';
            continue;
        }

        int a = s, b = s, ans = 1;
        for (int i = L - 1; ~i; i--) {
            int na = bmin(lift[a][i][0], lift[b][i][0]), nb = bmax(lift[a][i][1], lift[b][i][1]);
            if (t < l[na] || t > r[nb])
                a = na, b = nb, ans += (1 << i);
        }

        int fa = bmin(lift[a][0][0], lift[b][0][0]), fb = bmax(lift[a][0][1], lift[b][0][1]);

        cout << (t >= l[fa] && t <= r[fb] ? ans + 1 : -1) << '\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...