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: Time Limit Exceeded)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pb push_back
#define pii pair<ll,ll>
#define tii tuple<ll,ll,ll>
#define all(v) (v).begin(),(v).end()
#define ll long long
#define deb(x) cout << #x << "=" << x << endl;
#define ull unsigned long long
#define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr);
#define ld long double
#define mod 1000000007
#define PI 3.141592653589793238
#define phi 1.638
#define eps 1e-9
#define endl "\n"
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;   


int main() {
    fastio
    int h, w;
    cin>>h>>w;
    vector<pii> vec(h+1);
    for(int i = 1; i<=h; ++i)
        cin>>vec[i].first>>vec[i].second;  

    queue<tii> q;
    for(int i = 1; i<=w; ++i) {
        q.push(make_tuple(0, i, 0));
    }

    ll ans[h+1];
    for(int i = 0; i<=h; ++i)
        ans[i] = 1e9;

    map<pii, int> rem;
    while(!q.empty()) {
        ll x, y, dist;
        tie(x, y, dist) = q.front();
        q.pop();

        if(rem[{x,y}]) continue;
        rem[{x,y}] = 1;

        ans[x] = min(ans[x], dist);

        int dx[] = {0, 1};
        int dy[] = {1, 0};
        for(int i = 0; i<2; ++i) {
            int nx = dx[i]+x;
            int ny = dy[i]+y;
            if(nx>h || ny>w) continue;
            if(nx > x && ny>=vec[nx].first && ny<=vec[nx].second) continue;
            q.push(make_tuple(nx, ny, dist+1));
        }
    }

    for(int i = 1; i<=h; ++i) {
        cout<<(ans[i]==1e9?-1:ans[i])<<endl;
    }
}