Under construction

Still being built. Things may change

Jenga Boom

Time limit2sMemory limit512 MB

Summary
Simulate removals from a Jenga-like tower and report whether it falls, and at which removal, when a level's center of mass leaves the convex hull of the blocks still supporting it.
Level

Hard8 of 10

Topics
Geometry, Simulation, Prefix sum, Implementation
Solved
No attempts yet

Problem

Jane is a game designer and she is working on the next version of Jenga Boom. A block in this version measures 1×w×nw1 \times w \times nw instead of the usual 1×2×61 \times 2 \times 6. The tower is built when the game starts. Each level holds nn blocks laid side by side along their long sides, and every level sits at a right angle to the level below it. Players take turns pulling out one block at a time, and the game ends when the tower falls.


The initial tower


The tower just before it falls

Jane wants a simulator that helps her choose nn and ww. The simulator computes when the tower falls if the blocks are pulled out in a given order.

Set up coordinates as follows. The ground is the xyxy plane and level ll occupies the slab l−1≤z≤ll-1 \le z \le l. The footprint of a level is the square [0,nw]×[0,nw][0, nw] \times [0, nw]. In an odd numbered level the long side of a block runs along the yy axis and block kk occupies [(k−1)w,kw]×[0,nw][(k-1)w, kw] \times [0, nw]. In an even numbered level the long side runs along the xx axis and block kk occupies [0,nw]×[(k−1)w,kw][0, nw] \times [(k-1)w, kw]. Every block has the same material and the same mass.

Consider the cross-section between level ii and level i+1i+1. If at least one block is left in levels i+1i+1 through hh, let CC be the center of mass of those blocks projected onto the xyxy plane, and let HH be the convex hull of the projection of the blocks left in level ii. The tower falls if CC lies outside HH or on the boundary of HH. If level ii is empty then HH is empty too, so the tower falls as long as a block is left above it. If any cross-section with 1≤i≤h−11 \le i \le h-1 meets this condition, the tower falls right after that block is pulled out.

Input

The first line contains two integers nn and ww that define the size of a block (1≤n,w≤100001 \le n, w \le 10000). The second line contains two integers hh and mm, the number of levels in the tower and the number of blocks that are pulled out (1≤h,m≤50001 \le h, m \le 5000).

Each of the next mm lines describes one removal, in order, with two integers lil_i and kik_i: the level counted from the bottom and the position of the block inside that level (1≤li≤h1 \le l_i \le h, 1≤ki≤n1 \le k_i \le n). No block is pulled out twice.

Output

Print yes on the first line if the tower falls, and no otherwise. If it falls, print on the second line which removal brought it down, counted from 1. Ignore the input that is left after the tower falls.

Examples3

  1. Example 1

    Input
    5 2
    6 6
    4 1
    4 2
    4 5
    5 3
    4 3
    1 1
    
    Expected output
    yes
    5
    
  2. Example 2

    Input
    3 3
    10 1
    10 3
    
    Expected output
    no
    
  3. Example 3

    Input
    2 2
    2 1
    1 1
    
    Expected output
    yes
    1