Submission #1325102

#TimeUsernameProblemLanguageResultExecution timeMemory
1325102zwezdinvRailway Trip 2 (JOI22_ho_t4)C++20
100 / 100
257 ms46528 KiB
#include<bits/stdc++.h>

using ll = long long;

const int N = 1e5, LOG = 18;

int n;
struct segtree {
    int mx[2 * N], mn[2 * N];
    void build() {
        std::copy(mn, mn + n, mn + n);
        std::copy(mx, mx + n, mx + n);
        for (int i = n - 1; i > 0; --i) {
            mx[i] = std::max(mx[i << 1], mx[i << 1 | 1]);
            mn[i] = std::min(mn[i << 1], mn[i << 1 | 1]);
        }
    }
    std::pair<int, int> get(int l, int r) {
        int tl = n, tr = -1;
        for (l += n, r += n; l <= r; l >>= 1, r >>= 1) {
            if (l & 1) {
                tl = std::min(tl, mn[l]);
                tr = std::max(tr, mx[l]);
                ++l;
            }
            if (~r & 1) {
                tl = std::min(tl, mn[r]);
                tr = std::max(tr, mx[r]);
                --r;
            }
        }
        return {tl, tr};
    }
} tr[LOG];

signed main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    int k, m;
    std::cin >> n >> k >> m;
    std::vector<std::vector<std::pair<int, int>>> occl(n), occr(n);
    for (int i = 0; i < m; ++i) {
        int a, b;
        std::cin >> a >> b;
        --a, --b;
        if (a < b) {
            occl[a].emplace_back(b, 1);
            occl[std::min(b, a + k)].emplace_back(b, -1);
        } else {
            occr[a].emplace_back(b, 1);
            occr[std::max(b, a - k)].emplace_back(b, -1);
        }
    }
    std::vector<int> lft(n), rgt(n);
    std::multiset<int> st;
    for (int i = 0; i < n; ++i) {
        for (auto [x, t] : occl[i]) {
            if (t == 1) st.insert(x);
            else st.extract(x);
        }
        if (st.empty()) rgt[i] = i;
        else rgt[i] = *--st.end();
    }
    for (int i = n - 1; i >= 0; --i) {
        for (auto [x, t] : occr[i]) {
            if (t == 1) st.insert(x);
            else st.extract(x);
        }
        if (st.empty()) lft[i] = i;
        else lft[i] = *st.begin();
    }
    for (int i = 0; i < n; ++i) {
        std::tie(tr[0].mn[i], tr[0].mx[i]) = {lft[i], rgt[i]};
    }
    tr[0].build();
    for (int j = 0; j + 1 < LOG; ++j) {
        for (int i = 0; i < n; ++i) {
            auto [l, r] = tr[j].get(i, i);
            std::tie(tr[j + 1].mn[i], tr[j + 1].mx[i]) = tr[j].get(l, r);
        }
        tr[j + 1].build();
    }
    int q;
    std::cin >> q;
    while (q--) {
        int s, t;
        std::cin >> s >> t;
        --s, --t;
        if (s == t) {
            std::cout << "0\n";
            continue;
        }
        auto [fl, fr] = tr[LOG - 1].get(s, s);
        if (t < fl || fr < t) {
            std::cout << "-1\n";
            continue;
        }
        int l = s, r = s, ans = 0;
        for (int j = LOG - 1; j >= 0; --j) {
            auto [nl, nr] = tr[j].get(l, r);
            if (nr < t || t < nl) {
                ans += 1 << j;
                l = nl, r = nr;
            }
        }
        std::cout << ans + 1 << '\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...