Under construction

Still being built. Things may change

Greatest Square

Time limit8sMemory limit512 MB

Summary
Given a rectilinear simple polygon and query points, find the largest axis-parallel square inside it with each point as its lower left corner.
Level

Hard9 of 10

Topics
Geometry, Segment tree, Binary search
Solved
No attempts yet

Problem

Grete has a polygon with nn vertices. All sides of the polygon are parallel to the coordinate axes, and each pair of adjacent sides is perpendicular. The polygon is simple, so it has no self-intersections and no self-touches.

Grete has mm queries. Each query gives a point (ui,vi)(u_i, v_i) strictly inside the polygon. For each point, Grete wants the side length of the largest square that lies inside the polygon and has (ui,vi)(u_i, v_i) as its lower left corner.

Input

The input consists of several test cases and ends at end-of-file. Each test case has the following format.

The first line contains two integers nn and mm, the number of vertices and the number of queries.

Each of the next nn lines contains two integers xix_i and yiy_i, the coordinates of the vertices in counterclockwise order.

Each of the next mm lines contains two integers uiu_i and viv_i, the coordinates of the lower left corner of a query.

Output

For each query, print one integer on its own line: the side length of the largest square inside the polygon whose lower left corner is the query point.

Constraints

  • 4≤n≤2×1054 \leq n \leq 2 \times 10^5
  • 1≤m≤2×1051 \leq m \leq 2 \times 10^5
  • −108≤xi,yi,ui,vi≤108-10^8 \leq x_i, y_i, u_i, v_i \leq 10^8
  • The sum of nn and the sum of mm do not exceed 2×1062 \times 10^6.

Examples1

  1. Example 1

    Input
    4 3
    0 0
    4 0
    4 4
    0 4
    1 1
    2 2
    3 3
    12 12
    3050 2000
    2000 2000
    2000 3635
    -2000 3635
    -2000 2000
    -2590 2000
    -2590 -2000
    -2000 -2000
    -2000 -3481
    2000 -3481
    2000 -2000
    3050 -2000
    1415 -2882
    -1100 498
    -827 -3331
    -114 -542
    -1887 3485
    -1606 -1463
    -768 880
    -1261 1180
    330 2648
    -1017 -2886
    -1213 -585
    -2025 -1966
    
    Expected output
    3
    2
    1
    585
    3100
    2827
    2542
    150
    3606
    2755
    2455
    987
    3017
    3213
    3966