Under construction

Still being built. Things may change

Sliding Blocks

Time limit1sMemory limit512 MB

Summary
Given n rectangles that slide left and down one at a time, find the final lower-left corner of the last block.
Level

Medium7 of 10

Topics
Simulation, Geometry, Sorting
Solved
No attempts yet

Statement

Rectangular blocks are given one by one in the plane. Every block is parallel to the xy-axis. Each block starts at a given location and moves along a given direction, which is always left-downward. A block's location is the (x,y)(x, y) coordinate of its lower-left corner.

The movement of a block has two phases.

In the first phase, the block moves from its starting location toward (x−Δx,y−Δy)(x - \Delta x, y - \Delta y). While it moves, if its bottom side hits the x-axis or another block, its direction changes to leftward and it enters the second phase. If its left side hits the y-axis or another block, its direction changes to downward and it enters the second phase. If its lower-left corner touches the upper-right corner of another block, it may change to either downward or leftward. Downward has higher priority. If downward is blocked by other blocks, the direction changes to leftward. If the upper-left or lower-right corner of the block only touches another block's corner, the block keeps its direction, because a corner contact does not block it.

In the second phase, the block repeats move-left and move-down (or move-down and move-left) for as long as it can move. Once the block enters the second phase, it keeps moving in the same direction as far as possible. When the x-axis, the y-axis, or another block blocks it, it changes direction and keeps moving.

Figure J.1 shows an example of a block's path. The red line is the path, and the orange block is the final position.

Figure J.2 shows the corner cases described above.

For each block, the following information is given.

  1. The width and height ww and hh of the block.
  2. The starting location (x,y)(x, y).
  3. The direction (Δx,Δy)(\Delta x, \Delta y) of the first movement. See Figure J.3. The block begins at (x,y)(x, y) and moves toward (x−Δx,y−Δy)(x - \Delta x, y - \Delta y).

Given the information on nn blocks, find the location of the last block after every block has moved one by one according to the rules above. The iith block (for 2≤i≤n2 \le i \le n) does not overlap any earlier block at its starting location. Some blocks may not move at all from their starting location because other blocks block them.

Input

The first line contains an integer nn (1≤n≤1,0001 \le n \le 1{,}000), the number of blocks. Each of the next nn lines contains six integers ww, hh, xx, yy, Δx\Delta x, Δy\Delta y. The width and height satisfy 1≤w,h≤100,0001 \le w, h \le 100{,}000. The starting location satisfies 0≤x,y≤1080 \le x, y \le 10^8. The direction satisfies 1≤Δx,Δy≤1051 \le \Delta x, \Delta y \le 10^5.

Output

Print one line with two integers xx and yy, the location of the last block after all blocks have moved.

Examples2

  1. Example 1

    Input
    8
    12 2 100 100 1 1
    3 5 100 1 1 1
    5 8 100 1 1 1
    2 4 0 100 1 1
    6 3 0 100 1 1
    13 2 1 100 100 1
    15 2 1 100 100 1
    2 3 18 10 1 1
    
    Expected output
    2 2
    
  2. Example 2

    Input
    7
    4 4 1000 1 1 1
    3 3 1000 1 1 1
    2 10 1000 1 1 1
    3 4 1 100 1 1
    2 3 0 100 1 1
    4 3 0 100 1 1
    2 2 10 15 1 1
    
    Expected output
    3 4