This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout (T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
#define lpos pos*2
#define rpos pos*2+1
const int SIZE = 2e5 + 5;
const int H = __lg(SIZE);
int n, m, q;
vector<int> lis;
int hl[SIZE], hr[SIZE];
int to[SIZE][H + 1];
struct Node {
    int mx, lazy;
    Node operator + (const Node &rhs) const {
        Node re;
        re.mx = max(mx, rhs.mx);
        re.lazy = 0;
        return re;
    }
} node[SIZE << 3];
void push(int pos, int l, int r) {
    node[pos].mx += node[pos].lazy;
    if (l < r) {
        node[lpos].lazy += node[pos].lazy;
        node[rpos].lazy += node[pos].lazy;
    }
    node[pos].lazy = 0;
}
void pull(int pos, int l, int r) {
    int mid = (l + r) / 2;
    push(lpos, l, mid);
    push(rpos, mid + 1, r);
    node[pos] = node[lpos] + node[rpos];
}
void upd(int pos, int l, int r, int L, int R, int x) {
    if (l == L && r == R) {
        node[pos].lazy += x;
        push(pos, L, R);
        return;
    }
    push(pos, L, R);
    int mid = (L + R) / 2;
    if (r <= mid) upd(lpos, l, r, L, mid, x);
    else if (l > mid) upd(rpos, l, r, mid + 1, R, x);
    else {
        upd(lpos, l, mid, L, mid, x);
        upd(rpos, mid + 1, r, mid + 1, R, x);
    }
    pull(pos, L, R);
}
void solve() {
    cin >> n;
    FOR (i, 1, n) {
        cin >> hl[i] >> hr[i];
        lis.pb(hl[i]), lis.pb(hr[i]);
    }
    {
        sort(lis.begin(), lis.end());
        lis.erase(unique(lis.begin(), lis.end()), lis.end());
        m = lis.size();
        FOR (i, 1, n) {
            hl[i] = lower_bound(lis.begin(), lis.end(), hl[i]) - lis.begin() + 1;
            hr[i] = lower_bound(lis.begin(), lis.end(), hr[i]) - lis.begin() + 1;
        }
    }
    to[n + 1][0] = n + 1;
    for (int i = 1, p = 1; i <= n; i++) {
        while (p <= n && node[1].mx <= 1) {
            upd(1, hl[p], hr[p], 1, m, 1);
            p++;
        }
        to[i][0] = (node[1].mx > 1 ? p - 1 : p);
        upd(1, hl[i], hr[i], 1, m, -1);
    }
    FOR (j, 1, H) FOR (i, 1, n + 1) to[i][j] = to[to[i][j - 1]][j - 1];
    cin >> q;
    while (q--) {
        int l, r, ans = 0;
        cin >> l >> r;
        for (int i = H, pos = l; i >= 0; i--) if (to[pos][i] <= r) {
            ans += 1 << i;
            pos = to[pos][i];
        }
        ans++;
        cout << ans << '\n';
    }
}
int main() {
    Waimai;
    solve();
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |