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>

const int maxn = 2e5 + 3;
using ll = long long;

int h, w;
struct rg {
    int l, r;
    ll val;
    bool operator<(const rg &u) const {
        return l < u.l;
    }
};
std::set<rg> s;
std::set<std::pair<ll, rg>> vs;

void delS(const rg &u);
void addS(const rg &u) {
    s.insert(u);
    vs.emplace(u.val, u);
}

void delS(const rg &u) {
    s.erase(u);
    vs.erase({u.val, u});
}

ll query(ll ch) {
    if(vs.empty())
        return -1;
    return vs.begin()->second.val + ch;
}

int main() {
    scanf("%d%d", &h, &w);
    addS({1, w, 0});
    for(int i = 1; i <= h; ++i) {
        int u, v;
        scanf("%d%d", &u, &v);
        auto it = s.lower_bound({u, u});
        auto jt = s.upper_bound({v, v});
        if(it != s.begin() && (it->l > u || it == s.end()))
            --it;
        std::vector<rg> toproc(it, jt);
        for(auto &&j : toproc)
            delS(j);
        if(!toproc.empty()) {
            ll rminval = LLONG_MAX;
            for(auto &&j : toproc) {
                rminval = std::min(rminval, j.r <= v ? j.val + v + 1 - j.r : j.val);
            }
            int rb = toproc.back().r;
            if(v + 1 <= w) {
                addS({v + 1, std::max(rb, v + 1), rminval});
            }
            int lb = toproc.front().l;
            if(lb < u) {
                addS({lb, u - 1, toproc.front().val});
            }
        }
        printf("%lld\n", query(i));
    }
    return 0;
}