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# (.NET Core 3.1.201) (judge verdict: Runtime Error)

using System;
using System.Collections.Generic;
using System.Linq;

public class PriorityQueue<TKey, TValue>
{
    SortedDictionary<TKey, HashSet<TValue>> dict = new SortedDictionary<TKey, HashSet<TValue>>();
    int count = 0;

    public int Count
    {
        get
        {
            return count;
        }
    }

    public void Enqueue(TKey key, TValue value)
    {
        HashSet<TValue> set;
        if (!dict.TryGetValue(key, out set))
        {
            set = new HashSet<TValue>();
            dict[key] = set;
        }

        if (set.Add(value))
        {
            count++;
        }
    }

    public KeyValuePair<TKey, TValue> Dequeue()
    {
        var queue = dict.First();
        if (queue.Value.Count <= 1)
        {
            dict.Remove(queue.Key);
        }

        var val = queue.Value.First();
        queue.Value.Remove(val);
        count--;
        return new KeyValuePair<TKey, TValue>(queue.Key, val);
    }

    public KeyValuePair<TKey, TValue> Peek()
    {
        var queue = dict.First();
        var val = queue.Value.First();
        return new KeyValuePair<TKey, TValue>(queue.Key, val);
    }

    public void Remove(TKey key, TValue value)
    {
        dict[key].Remove(value);
        count--;
        if (dict[key].Count == 0)
        {
            dict.Remove(key);
        }
    }
}

public static class Solution
{
    static int BinSearchIndex(int v, List<(int l, int r)> list)
    {
        int minRange = 0;
        int maxRange = list.Count();
        while (minRange != maxRange)
        {
            var center = (minRange + maxRange) / 2;
            if (list[center].r < v)
            {
                minRange = center + 1;
            }
            else
            {
                maxRange = center;
            }
        }

        return minRange;
    }

    public static void Main()
    {
        var vals = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        var H = vals[0];
        var W = vals[1];
        var A = new int[H];
        var B = new int[H];
        for (int i = 0; i < H; i++)
        {
            vals = Console.ReadLine().Split(' ').Select(x => int.Parse(x) - 1).ToArray();
            A[i] = vals[0];
            B[i] = vals[1];
        }

        var answer = new List<long>();
        var pairs = new List<(int l, int r)>();
        var queue = new PriorityQueue<int, (int l, int r)>();
        for (int i = 0; i < W; i++)
        {
            var pair = (i, i);
            pairs.Add(pair);
            queue.Enqueue(0, pair);
        }

        for (int i = 0; i < H; i++)
        {
            var list = new List<(int l, int r)>();
            var li = BinSearchIndex(A[i], pairs);
            var ri = BinSearchIndex(B[i] + 1, pairs);

            if (li < W)
            {
                {
                    var j = ri - 1;
                    var pair = pairs[j];
                    var d = pair.r - pair.l;
                    queue.Remove(d, pair);

                    if (B[i] == W - 1)
                    {
                        pairs.RemoveAt(j);
                    }
                    else
                    {
                        pairs[j] = (pair.l, B[i] + 1);
                        pair = pairs[j];
                        d = pair.r - pair.l;
                        queue.Enqueue(d, pair);
                    }
                }
                for (int j = ri - 2; j >= li; j--)
                {
                    var pair = pairs[j];
                    pairs.RemoveAt(j);
                    var d = pair.r - pair.l;
                    queue.Remove(d, pair);
                }
            }
            var min = queue.Count == 0 ? -1 : queue.Peek().Key;
            if (min >= 0)
            {
                min += 1 + i;
            }
            answer.Add(min);
        }

        Console.WriteLine(string.Join(Environment.NewLine, answer));
    }
}