Under construction

Still being built. Things may change

Tiles

Time limit2sMemory limit1024 MB

Summary
Given a simple axis-aligned polygon, find the largest k at most M such that the part left of x = k can be covered by non-overlapping 2 by 2 tiles.
Level

Hard8 of 10

Topics
Dynamic programming, Geometry
Solved
No attempts yet

Statement

Soon after converting to Christianity, the first and only Lithuanian king, Mindaugas, is believed to have ordered the construction of the Vilnius Cathedral. The construction is nearly complete. Only the floor remains, and it must be covered with ornamented glazed ceramic tiles.

The cathedral floor is a polygon in a 2D plane with a Cartesian coordinate system. The polygon has NN distinct vertices, numbered from 1 to NN. For each 1≤i≤N1 \le i \le N, vertex ii is at point (Xi,Yi)(X_i, Y_i), where XiX_i and YiY_i are nonnegative integers. Vertex ii is joined to vertex i+1i+1 for each 1≤i≤N−11 \le i \le N-1, and vertex NN is joined to vertex 1. The vertices are listed in either clockwise or counterclockwise order.

The cathedral is an axis-aligned polygon: every edge is parallel to the xx-axis or the yy-axis. It is also simple. Exactly two edges meet at each vertex, and any two edges meet only at a shared vertex.

The builders have infinitely many square tiles, each with side length 2. They pick a vertical line and want to cover the part of the cathedral to the left of it. For any integer kk, let LkL_k be the vertical line with xx-coordinate kk. A covering of the part left of LkL_k is a placement of tiles in the plane such that:

  • every point inside the polygon with xx-coordinate less than kk is covered by some tile;
  • no point outside the polygon, or with xx-coordinate greater than kk, is covered by any tile;
  • the interiors of the tiles do not overlap.

The minimum xx-coordinate of any vertex is 0. Let MM be the maximum xx-coordinate of any vertex. Find the largest integer kk with k≤Mk \le M such that a covering of the part left of LkL_k exists. By definition, a covering of the part left of L0L_0 exists, using 0 tiles.

Input

The first line contains two integers NN and MM, the number of vertices and the maximum xx-coordinate of any vertex. Each of the next NN lines contains two integers xix_i and yiy_i, the coordinates of vertex ii.

Output

Print the maximum kk such that k≤Mk \le M and a covering of the part of the cathedral to the left of LkL_k exists.

Constraints

  • 4≤N≤2⋅1054 \le N \le 2 \cdot 10^5
  • 1≤M≤1091 \le M \le 10^9
  • 0≤yi≤1090 \le y_i \le 10^9 for each 1≤i≤N1 \le i \le N
  • The cathedral forms an axis-aligned simple polygon.
  • The minimum of x1,x2,…,xNx_1, x_2, \dots, x_N is 0, and the maximum is MM.

Examples3

  1. Example 1

    Input
    14 6
    0 1
    0 3
    2 3
    2 4
    0 4
    0 6
    3 6
    3 7
    4 7
    6 7
    6 5
    3 5
    3 2
    3 1
    
    Expected output
    2
    
  2. Example 2

    Input
    4 3
    0 0
    0 3
    3 3
    3 0
    
    Expected output
    0
    
  3. Example 3

    Input
    18 9
    0 2
    2 2
    2 1
    4 1
    4 0
    9 0
    9 2
    4 2
    4 4
    7 4
    7 3
    9 3
    9 6
    4 6
    4 5
    2 5
    2 4
    0 4
    
    Expected output
    6