#P4125. [WC2012] 记忆中的水杉树
[WC2012] 记忆中的水杉树
Description
Changzhou Senior High School of Jiangsu is a century-old prestigious school, filled with countless unforgettable memories.
Will remembers that when he was little, before the school's reconstruction, there was a tall metasequoia grove. When the metasequoias shed their leaves, the needles would cover the ground like a blanket, making it romantic and leisurely to walk on. Back then, Will and his classmates liked to use these needles to play a “taking needles” game under the metasequoias.
At the start of the game, everyone lays needles flat on the ground. Then, in each round, one player may choose one needle and move it in the horizontal or vertical direction (that is, translate it to infinity) — provided that during the movement it is not blocked by any needle that has not yet been moved away. If a needle’s movement would be blocked in a round, then this move is illegal and not allowed.
After rounds, when all needles have been removed, the game ends. Not every needle can be moved at any time. When there are many needles, determining in each round whether a specific needle can be moved in a given direction is troublesome. Now we abstract the ground as a 2D Cartesian plane, the needles as pairwise disjoint line segments in the plane, labeled from to . Will also provides, for each round, the index of the needle he wants to move and the direction. Please help him:
- Find in which round the earliest illegal move occurs.
- Provide a legal move sequence that completes the game.
Note: When moving segments, merely touching at endpoints does not cause obstruction. See the sample for details.
Input Format
The first line contains a positive integer , the number of needles.
The next lines each contain integers describing the positions of the needles. On the -th of these lines, the integers , , , indicate that the endpoints of the line segment abstracting needle are and .
The next lines each contain integers describing the moves. On the -th of these lines, the integers , indicate that in round , the needle to move is , and the direction is . Here is an integer between and : means move left (the negative -axis direction), means move up (the positive -axis direction), means move right, and means move down.
The input is guaranteed that:
- All segments have positive length, no two segments share a point, and there are no vertical or horizontal segments.
- through form a permutation of through .
- There is at least one illegal move in Will’s given sequence.
- There always exists a sequence of legal moves that completes the game.
Output Format
The output contains lines.
The first line contains an integer between and , indicating the earliest round in which an illegal move occurs.
The next lines each contain two integers, as in the input format, describing a legal move sequence.
5
2 5 5 8
2 1 3 5
5 2 6 5
7 0 4 2
3 1 4 0
2 0
3 0
4 0
1 2
5 1
3
2 0
3 0
4 3
1 2
5 1
4
-1 1 2 3
13 5 9 8
10 10 15 14
10 17 0 20
3 1
2 1
1 1
4 1
2
4 1
3 1
2 1
1 1
Hint
[Sample explanation]
In round of Will’s given plan, moving needle to the left is blocked by needle .
[Constraints]
See the table below.

For a test point:
- If the illegal move detection is correct but the provided plan is wrong, you can get points. The message will be:
An invalid move in step. - If the illegal move detection is wrong but the provided plan is correct, you can get points. The message will be:
Negative error detection!. - If both the detection and the plan are correct, you get points; otherwise, points.
If the program’s output format is incorrect, it will be directly judged as points.
Translated by ChatGPT 5
京公网安备 11011102002149号