Under construction

Still being built. Things may change

Sail Shreds - 1

Time limit2sMemory limit512 MB

Summary
Place N triangles by translation only, without rotation, so they exactly tile an X by Y rectangle, and output the new position of each triangle's vertex A.
Level

Hard8 of 10

Topics
Geometry, Greedy, Implementation
Solved
No attempts yet

Statement

A two-day storm wrecked a group of sailors on a desert island in the Mediterranean Sea. Their radio is broken, so they cannot call for help. Their only chance of survival is to escape on a raft, and the raft needs a sail. They recovered the main sail from the wrecked ship, but it is torn into several shreds. They must sew the shreds together to restore the original sail.

Each shred is a triangle. Stripes on the sail make the correct orientation of each triangle easy to see. The orientations must be kept, because otherwise the sail resists wind poorly and may tear again. The sailors spent several hours searching for the original arrangement without success. They ask you to help them with a computer.

Given the vertices of the triangular shreds and the size of the rectangular sail, move the shreds to rebuild the sail. You may translate each shred but must not rotate it. The arrangement must satisfy these conditions:

  • The whole rectangle is covered.
  • No two triangles overlap. No point lies in the interiors of two different triangles.
  • No part of any triangle lies outside the rectangle.

You may assume a solution exists for every input. The total area of the shreds equals the area of the sail. Several shreds may have the same shape.

Input

The first line contains three integers N, X, and Y separated by single spaces. X is the width and Y is the height of the sail, with 1≤X,Y≤1 000 0001 \le X, Y \le 1\,000\,000. N is the number of shreds, with 2≤N≤1 0002 \le N \le 1\,000.

The next N lines describe the triangles. Line i+1 contains six integers Ai,xA_{i,x}, Ai,yA_{i,y}, Bi,xB_{i,x}, Bi,yB_{i,y}, Ci,xC_{i,x}, and Ci,yC_{i,y} separated by single spaces. These give the vertices AiA_i, BiB_i, and CiC_i of the i-th triangle. Every coordinate satisfies 0≤0 \le coordinate ≤1 000 000\le 1\,000\,000.

The position of a triangle in the input does not matter. For example, if the third line of the example input were replaced with 6 6 14 5 16 6, the set of shreds would be the same.

Output

Output exactly N lines. The i-th line contains two integers Ai,x∗A^*_{i,x} and Ai,y∗A^*_{i,y} separated by a space. These are the coordinates of the point AiA_i after the i-th shred is moved into place. The lower left corner of the sail is at (0, 0), and the upper right corner is at (X, Y).

Examples1

  1. Example 1

    Input
    6 10 5
    4 0 12 4 4 5
    4 5 12 4 14 5
    0 3 10 3 4 5
    4 5 10 3 8 7
    7 7 7 10 5 11
    0 1 2 0 2 2
    
    Expected output
    0 0
    0 5
    0 0
    4 2
    10 0
    8 4