Problem:
Score : 600 points

Problem Statement

There is a grid of squares with H+1 horizontal rows and W vertical columns.

You will start at one of the squares in the top row and repeat moving one square right or down. However, for each integer i from 1 through H, you cannot move down from the A_i-th, (A_i + 1)-th, \ldots, B_i-th squares from the left in the i-th row from the top.

For each integer k from 1 through H, find the minimum number of moves needed to reach one of the squares in the (k+1)-th row from the top. (The starting square can be chosen individually for each case.) If, starting from any square in the top row, none of the squares in the (k+1)-th row can be reached, print -1 instead.

Constraints

1 \leq H,W \leq 2\times 10^5

1 \leq A_i \leq B_i \leq W

Input

Input is given from Standard Input in the following format:

H W
A_1 B_1
A_2 B_2
:
A_H B_H

Output

Print H lines. The i-th line should contain the answer for the case k=i.

Sample Input 1

4 4
2 4
1 1
2 3
2 4

Sample Output 1

1
3
6
-1

Let (i,j) denote the square at the i-th row from the top and j-th column from the left.

For k=1, we need one move such as (1,1) → (2,1).

For k=2, we need three moves such as (1,1) → (2,1) → (2,2) → (3,2).

For k=3, we need six moves such as (1,1) → (2,1) → (2,2) → (3,2) → (3,3) → (3,4) → (4,4).

For k=4, it is impossible to reach any square in the fifth row from the top.

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using pdd = pair<double, double>;
using uint = unsigned int;
template <typename T>
using Graph = vector<vector<T>>;
const int MOD = 1e9 + 7;
const ld PI = acosl(-1);

struct LazySegTree {
    int siz;
    vector<pii> node;
    vector<int> lazy;
    vector<int> ridx;

    LazySegTree(int N) : siz(1) {
        while (siz < N)
            siz *= 2;

        ridx.resize(2 * siz - 1, -1);
        node.resize(2 * siz - 1);
        for (int i = 0; i < siz; ++i) {
            ridx[i + siz - 1] = i;
            node[i + siz - 1] = {i, i};
        }
        for (int i = siz - 2; i >= 0; --i) {
            ridx[i] = max(ridx[2 * i + 1], ridx[2 * i + 2]);
            node[i] = node[2 * i + 1];
        }

        lazy.resize(2 * siz - 1, -1);
    }

    int calc(pii &nd) {
        return nd.first - nd.second;
    }

    void eval(int k) {
        if (lazy[k] < 0)
            return;
        if (k < siz - 1) {
            lazy[2 * k + 1] = lazy[k];
            lazy[2 * k + 2] = lazy[k];
        }

        node[k] = {lazy[k], ridx[k]};
        lazy[k] = -1;
    }

    void update(int a, int b, int x, int k = 0, int l = 0, int r = -1) {
        if (r < 0)
            r = siz;

        eval(k);
        if (r <= a || b <= l)
            return;
        if (a <= l && r <= b) {
            lazy[k] = x;
            eval(k);
            return;
        }
        update(a, b, x, 2 * k + 1, l, (l + r) / 2);
        update(a, b, x, 2 * k + 2, (l + r) / 2, r);
        if (calc(node[2 * k + 1]) <= calc(node[2 * k + 2]))
            node[k] = node[2 * k + 1];
        else
            node[k] = node[2 * k + 2];
    }

    pii query(int a, int b, int k = 0, int l = 0, int r = -1) {
        if (r < 0)
            r = siz;

        eval(k);
        if (r <= a || b <= l)
            return {INT_MAX, 0};
        if (a <= l && r <= b)
            return node[k];
        pii vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
        pii vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
        if (calc(vl) <= calc(vr))
            return vl;
        else
            return vr;
    }

    int lb(int a, int b, pii x, int k = 0, int l = 0, int r = -1) {
        if (r < 0)
            r = siz;

        eval(k);
        if (node[k] >= x || r <= a || b <= l)
            return a - 1;
        if (k >= siz - 1)
            return k - siz + 1;
        int vr = lb(a, b, x, 2 * k + 2, (l + r) / 2, r);
        if (vr != a - 1)
            return vr;
        return lb(a, b, x, 2 * k + 1, l, (l + r) / 2);
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int H, W;
    cin >> H >> W;
    LazySegTree segt(W);
    bool isng = false;
    for (int i = 0; i < H; ++i) {
        int A, B;
        cin >> A >> B;
        A--, B--;

        if (isng) {
            cout << -1 << '\n';
            continue;
        }

        int l = segt.lb(0, W, {A, 0}) + 1;
        int r = segt.lb(0, W, {B, INT_MAX}) + 1;

        if (B + 1 < W)
            segt.update(l, r, B + 1);
        else
            segt.update(l, r, INT_MAX);

        pii p = segt.query(0, W);
        int res = p.first - p.second;
        if (res >= 1e9) {
            cout << -1 << '\n';
            isng = true;
        } else
            cout << res + i + 1 << '\n';
    }

    return 0;
}