tiles — BOI contest
Task type: batch | Time limit: 1 s | Memory limit: 256 MB | Difficulty: 22
Algorithms: line sweep, data structures, tree map

# Tiles

![](_page_0_Picture_0.jpeg)

Soon after converting to Christianity, it is believed that the first and the only Lithuanian King Mindaugas ordered the construction of the Vilnius Cathedral. The construction is almost completed, except that the floor has to be covered with ceramic ornamented glazed tiles.

The floor of the Vilnius Cathedral is a polygon in a 2D plane with a Cartesian coordinate system. The polygon has *N* distinct vertices, numbered from 1 to *N*. For each *i* such that 1 ≤ *i* ≤ *N*, vertex *i* is located at point (*X<sub>i</sub>*, *Y<sub>i</sub>*), where *X<sub>i</sub>* and *Y<sub>i</sub>* are nonnegative integers. There is an edge connecting vertex *i* and vertex *i* + 1 (for each *i* such that 1 ≤ *i* ≤ *N* − 1), as well as an edge connecting vertex *N* and vertex 1. The vertices are listed in either clockwise or counterclockwise order.

The cathedral is an **axis-aligned** polygon, which means that each of the edges is parallel to either the *x*-axis or the *y*-axis. Moreover, the cathedral is a **simple** polygon, that is:

*   exactly two edges meet at each vertex;
*   any pair of edges can only meet at a vertex.

The builders of the cathedral have infinitely many pieces of tiles. Each piece is a square with side length equal to 2. The builders would like to cover a big part of the cathedral with these pieces. Specifically, the builders want to pick some vertical line and cover the part of the cathedral to the left of the line. For any integer *k*, let *L<sub>k</sub>* denote the vertical line consisting of points with *x*-coordinate equal to *k*. A covering of the part of the cathedral to the left of *L<sub>k</sub>* is a placement of some number of pieces in the plane such that:

*   each point which lies in the interior of the polygon and has *x*-coordinate less than *k* is covered by some piece;
*   no point which lies outside of the polygon or has *x*-coordinate greater than *k* is covered by some piece;
*   the interiors of the pieces do not overlap.

The minimum *x*-coordinate of any vertex in the cathedral is 0. Let *M* denote the maximum *x*-coordinate of any vertex in the cathedral.

## Task

Help the builders of the Vilnius Cathedral by determining the largest integer *k*, such that *k* ≤ *M*, and there exists a covering of the part of the cathedral to the left of *L<sub>k</sub>*. Note that by definition, there exists a covering of the part of the cathedral to the left of *L*<sub>0</sub> (which uses 0 pieces).

## Input

The first line of the input contains two integers *N* and *M* – the number of vertices and the maximum *x*-coordinate of any vertex.

Then, *N* lines follow. The *i*-th of them contains two integer numbers *x<sub>i</sub>* and *y<sub>i</sub>* – the coordinates of the *i*-th vertex. The vertices are listed in either clockwise or counterclockwise order.

## Output

Your program should output the maximum *k*, such that *k* ≤ *M* and there exists a covering of the part of the cathedral to the left of *L<sub>k</sub>*.

## Examples

| Input | Output | Explanation                                                                                                                                                                                                                                                                                                              |
| :---- | :----- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 14 6   <br> 0 1  <br> 0 3  <br> 2 3  <br> 2 4  <br> 0 4  <br> 0 6  <br> 3 6  <br> 3 7  <br> 4 7  <br> 6 7  <br> 6 5  <br> 3 5  <br> 3 2  <br> 3 1| 2     | The following picture shows the part of the cathedral to the left of line *L<sub>k</sub>* for *k* = 2:  <br> ![](_page_2_Figure_0.jpeg)  <br> There is a covering of the part of the cathedral to the left of *L<sub>2</sub>*. The covering uses two pieces. For any *k* > 2, there is no covering of the part of the cathedral to the left of *L<sub>k</sub>*. |
| 4 3   <br> 0 3  <br> 0 0  <br> 3 0  <br> 3 3 | 0     | There is no positive value of *k* such that the part of the cathedral to the left of *L<sub>k</sub>* could be covered with tiles.                                                                                                                                                                       |
| 18 9   <br> 0 2  <br> 2 2  <br> 2 1  <br> 4 1  <br> 4 0  <br> 9 0  <br> 9 2  <br> 4 2  <br> 4 4  <br> 7 4  <br> 7 3  <br> 9 3  <br> 9 6  <br> 4 6  <br> 4 5  <br> 2 5  <br> 2 4  <br> 0 4 | 6     | As illustrated below, it is possible to cover the part of the cathedral to the left of line *L<sub>6</sub>*: <br> ![](_page_2_Figure_1.jpeg)  <br> For each *k* > 6, there is no covering of the part of the cathedral to the left of *L<sub>k</sub>*.                                                                           |

## Constraints

*   4 ≤ *N* ≤ 2 ⋅ 10<sup>5</sup>
*   1 ≤ *M* ≤ 10<sup>9</sup>
*   0 ≤ *y<sub>i</sub>* ≤ 10<sup>9</sup> (for each 1 ≤ *i* ≤ *N*)
*   The cathedral forms an axis-aligned simple polygon.
*   The minimum of *x*<sub>1</sub>, *x*<sub>2</sub>, ..., *x<sub>N</sub>* is 0, and the maximum of *x*<sub>1</sub>, *x*<sub>2</sub>, ..., *x<sub>N</sub>* is *M*.

## Subtasks

| No. | Points | Additional constraints                                                                         |
| --- | ------ | ---------------------------------------------------------------------------------------------- |
| 1   | 4      | *N* = 4.                                                                                      |
| 2   | 9      | *N* ≤ 6.                                                                                     |
| 3   | 11     | *x<sub>N</sub>* = 0, *y<sub>N</sub>* = 0, *x<sub>i</sub>* ≤ *x<sub>i+1</sub>*, *y<sub>i</sub>* ≥ *y<sub>i+1</sub>* (for each *i* such that 1 ≤ *i* ≤ *N* − 2). |
| 4   | 19     | *M* ≤ 1000 and all *y<sub>i</sub>* ≤ 1000.                                                    |
| 5   | 22     | All values of *y<sub>i</sub>* are even.                                                        |
| 6   | 25     | All values of *x<sub>i</sub>* are even.                                                        |
| 7   | 10     | No additional constraints.                                                                     |

Starter code: