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.
Hard8GeometrySimulationPrefix sumImplementationNo attempts yetTime limit2sMemory limit512 MBJane is a game designer and she is working on the next version of Jenga Boom. A block in this version measures 1×w×nw instead of the usual 1×2×6. The tower is built when the game starts. Each level holds n 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 n and w. 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 xy plane and level l occupies the slab l−1≤z≤l. The footprint of a level is the square [0,nw]×[0,nw]. In an odd numbered level the long side of a block runs along the y axis and block k occupies [(k−1)w,kw]×[0,nw]. In an even numbered level the long side runs along the x axis and block k occupies [0,nw]×[(k−1)w,kw]. Every block has the same material and the same mass.
Consider the cross-section between level i and level i+1. If at least one block is left in levels i+1 through h, let C be the center of mass of those blocks projected onto the xy plane, and let H be the convex hull of the projection of the blocks left in level i. The tower falls if C lies outside H or on the boundary of H. If level i is empty then H is empty too, so the tower falls as long as a block is left above it. If any cross-section with 1≤i≤h−1 meets this condition, the tower falls right after that block is pulled out.
The first line contains two integers n and w that define the size of a block (1≤n,w≤10000). The second line contains two integers h and m, the number of levels in the tower and the number of blocks that are pulled out (1≤h,m≤5000).
Each of the next m lines describes one removal, in order, with two integers li and ki: the level counted from the bottom and the position of the block inside that level (1≤li≤h, 1≤ki≤n). No block is pulled out twice.
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.