#P4468. Origami Layer Counting

    ID: 17714 Type: Default 1000ms 256MiB

Origami Layer Counting

Origami Layer Counting

A square paper with edges parallel to the coordinate axes is placed on a table with its bottom‐left corner at (0,0) and top‐right corner at (100,100). Then, n folding commands are performed. Each command is given by two distinct points P1(x1, y1) and P2(x2, y2) and represents a fold along the line passing through these points. When folding, the paper on the right side of the directed line segment from P1 to P2 is folded over to the left side (the left part remains in place). The paper is assumed to have no thickness, so every fold is perfect.

After all folds, a hole will be punched at a candidate position and a rope will be passed through to hang the work on a wall. The location of the hole is crucial: if the rope passes through too many layers, punching becomes difficult; if it passes through too few layers, the work might tear under the weight. To help decide a proper location for punching, you are to compute the number of paper layers pierced by a hole at each candidate point. Note that if the hole exactly touches the boundary of a layer (within a tolerance of 10-6), that layer is not counted.

Model Details: In this problem the paper is assumed to be ideal (zero thickness) so that all folds execute perfectly.

Reverse Folding Idea: One way to solve the problem is to “unfold” the paper. Each candidate point in the final folded state may correspond to one or more points on the original square. For every fold (processed in reverse order) the candidate point will have one or two possible predecessors (by reflecting across the fold line when applicable). A candidate branch is discarded if at any point during unfolding the point falls exactly on a fold line (within a tolerance of 10-6) or falls on the boundary of the original paper. The final answer for a candidate is the count of valid unfolded points that lie strictly within the original square.

inputFormat

The input begins with an integer n (n ≥ 0) indicating the number of fold commands. The next n lines each contain four real numbers: x1, y1, x2, y2, representing a fold command with two distinct points P1 and P2.

The following line contains an integer m (m ≥ 1) indicating the number of candidate punch positions. Each of the next m lines contains two real numbers x and y, representing a candidate point. It is guaranteed that the candidate points are given in the final folded configuration (i.e. they lie on the "left side" of every fold line as produced by the folding process).

outputFormat

For each candidate point, output a single integer on a separate line indicating the number of paper layers that the hole would pierce. A layer is counted only if the unfolded point lies strictly inside the original paper (i.e. not within 10-6 of any boundary) and if during unfolding it never hit a fold line (within a tolerance of 10-6).

sample

1
50 0 50 100
3
75 50
60 50
50 50
2

2 0

</p>