Origami

Time limit1sMemory limit128 MB

Summary
Given up to 8 folds of a square sheet, count how many layers of paper a query point pierces, ignoring points on edges.
Level

Medium7 of 10

Topics
Geometry, Simulation, Recursion, Implementation
Solved
No attempts yet

Problem

Origami is the ancient art of folding a single sheet of paper into shapes such as animals and flowers. A machine performs simple origami on a board with a coordinate system. A square sheet of paper is placed on the board so that its lower-left corner is at (0,0)(0, 0) and its upper-right corner is at (100,100)(100, 100).

The machine then runs a program made of several folding steps, in order. Each step is a single fold line given by two points. Facing from the first point toward the second point, the paper on the left side of the line stays in place, and the paper on the right side is folded flat on top of it (reflected across the line). After the last step, the origami consists of several layers of paper.

To hang the origami you must pierce a hole through it. Given a point, compute how many layers of paper the pierce passes through, so you can tell whether the spot is neither too thick nor too thin.

Input

The first line contains an integer nn (0≤n≤80 \le n \le 8), the number of folding steps. Each of the next nn lines contains four real numbers x1 y1 x2 y2x_1\ y_1\ x_2\ y_2, the two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) that define one fold line. The folds are applied in the given order.

The next line contains an integer mm, the number of query points. Each of the next mm lines contains two real numbers x yx\ y, the coordinates of a point to pierce.

Output

For each query point, output on its own line the number of paper layers pierced at that point.

The paper has zero thickness and folds ideally. Treat each pierce as a single point. A layer that is pierced exactly on the border of the paper or on a folded edge (within 10−610^{-6}) does not count as a pierced layer.

Examples1

  1. Example 1

    Input
    2
    -0.5 -0.5 1 1
    1 75 0 75
    6
    10 60
    80 60
    30 40
    10 10
    50 50
    20 50
    
    Expected output
    4
    2
    2
    0
    0
    2