Falling Blocks
Contest: Kotlin Heroes: Episode 3
Tags: *special, data structures

Recently, Polycarp has invented a new mobile game with falling blocks.

In the game, $$$n$$$ blocks are falling down, one at a time, towards a flat surface with length $$$d$$$ units. Each block can be represented as a rectangle with coordinates from $$$l_i$$$ to $$$r_i$$$ and unit height, dropped downwards from very high up. A block falls until it comes in contact with the flat surface or any other block. Let's define that block $$$a$$$ covers block $$$b$$$ if $$$l_a \le l_b \le r_b \le r_a$$$.

Consider what happens when a new block $$$i$$$ falls. If the new (upper) block $$$i$$$ comes in contact with any block $$$j$$$ such that block $$$i$$$ does not cover block $$$j$$$, block $$$i$$$ will stick to block $$$j$$$, and no blocks will disappear. Otherwise, all blocks that block $$$i$$$ covers and is in contact with will be vaporized, and block $$$i$$$ will continue falling with the ability to vaporize lower blocks.

For example, consider what happens when three blocks $$$(1,2)$$$, $$$(2,3)$$$ and $$$(1,3)$$$ fall, in that order. The first block will stick to the flat surface. Then, the second block will stick to the first block. Finally, the third block will vaporize the second block, keep falling, vaporize the first block, and stick to the flat surface.

Here is a graphic for the first example.

After each block falls, help Polycarp determine how many blocks will remain!

Input format:
The first line contains two integers $$$n$$$ and $$$d$$$ ($$$1 \le n, d \le 10^5$$$)  — the number of falling blocks and the length of the flat surface.

The $$$i$$$-th of the following $$$n$$$ lines contains integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le d$$$)  — the coordinates of the $$$i$$$-th block.

Output format:
Output $$$n$$$ integers. The $$$i$$$-th integer should be the number of blocks that will be left after the $$$i$$$-th block falls.

Constraints:
Time limit: 3.0 s
Memory limit: 512.0 MB

Note:
The first example is explained above.

In the second example, this is what happens after each block falls:

- Block $$$1$$$ will stick to the flat surface.
- Block $$$2$$$ will stick to the flat surface.
- Block $$$3$$$ will stick to blocks $$$1$$$ and $$$2$$$. Note that block $$$3$$$ will not vaporize block $$$2$$$ because it does not cover block $$$1$$$ and is in contact with it.
- Block $$$4$$$ will vaporize all the blocks and stick to the flat surface.
- Block $$$5$$$ will stick to block $$$4$$$
- Block $$$6$$$ will stick to block $$$5$$$.
- Block $$$7$$$ will stick to block $$$6$$$. Note that no blocks are vaporized because although block $$$7$$$ covers block $$$4$$$ and block $$$5$$$, they never come in contact.
- Block $$$8$$$ vaporizes block $$$7$$$ and sticks to block $$$6$$$.

Example input:
3 3
1 2
2 3
1 3

Example output:
1
2
1

Example input:
8 6
1 2
3 3
2 3
1 3
2 4
3 6
1 5
1 5

Example output:
1
2
3
1
2
3
4
4

Editorial:
If we store, for each height, an ordered set of all blocks currently there then if we know, the height $$$maxH$$$ to start from, we can efficiently enough vaporize blocks or decide that the current block has stopped falling.
We can query the set for that level to check if the fall is stopped (i.e. if the dropped block comes in contact with a block that it does not cover). If so, stop the vaporizing and remember the final height. If not, use the set to determine which blocks to vaporize before repeating the drop.
The question is: how to determine starting height $$$maxH$$$? If blocks wouldn't be vaporized then we could use a standard Segment Tree with "set value on the segment" and "get the maximum on the segment" to set a block on some height and to find the maximum height of blocks on some segment.
In fact, we can use it! It's because of the property that the block vaporizes only blocks it covers, so "setting new block" operation in ST will replace all vaporized blocks with the current one and we don't need to think about methods to erase blocks from ST manually.
The resulting complexity is $$$O(n \log(n))$$$.