Under construction

Still being built. Things may change

Range Closest Pair of Points Query

Time limit9sMemory limit1024 MB

Summary
Given n points and q index ranges [l, r], find the squared distance of the closest pair of points inside each range.
Level

Hard9 of 10

Topics
Divide and conquer, Geometry, Sorting
Solved
No attempts yet

Problem

The closest pair of points problem is a well-known problem of computational geometry. There are nn points p1,p2,…,pnp_1,p_2,\ldots,p_n in the Euclidean plane. You are given qq queries. The ii-th query gives two integers ℓi\ell_i and rir_i (1≤ℓi<ri≤n1\leq \ell_i< r_i\leq n). For each query, find a pair of points (u,v)(u,v) with ℓi≤u<v≤ri\ell_i\leq u<v\leq r_i such that the Euclidean distance (xu−xv)2+(yu−yv)2\sqrt{(x_u-x_v)^2+(y_u-y_v)^2} between pup_u and pvp_v is minimized.

Input

The first line contains two integers nn and qq (2≤n≤250 0002 \leq n\leq 250\,000, 1≤q≤250 0001\leq q\leq 250\,000), the number of points and the number of queries.

Each of the next nn lines contains two integers xix_i and yiy_i (1≤xi,yi≤1081\leq x_i,y_i\leq 10^8), the coordinates of pip_i.

Each of the next qq lines contains two integers ℓi\ell_i and rir_i (1≤ℓi<ri≤n1\leq \ell_i< r_i\leq n), describing a query.

Output

For each query, print one line containing the value (xu−xv)2+(yu−yv)2(x_u-x_v)^2+(y_u-y_v)^2 for the closest pair in the range.

Examples2

  1. Example 1

    Input
    5 5
    2 4
    1 1
    3 3
    5 1
    4 2
    1 5
    2 3
    2 4
    3 5
    1 3
    
    Expected output
    2
    8
    8
    2
    2
    
  2. Example 2

    Input
    2 1
    1 1
    1 1
    1 2
    
    Expected output
    0