#P3508. Illuminated Windows

    ID: 16762 Type: Default 1000ms 256MiB

Illuminated Windows

Illuminated Windows

Byteasar is tormented by a lamp that, although not directly shining on his windows, reflects off windows of the opposite building and eventually illuminates some of his own windows. The lamp is mounted on the wall of Byteasar's building at a fixed vertical coordinate, while the opposite building is exactly 10 meters away. Using the laws of geometrical (ray) optics and the law of reflection (angle of incidence equals angle of reflection), a ray of light that originates at the lamp and reflects off a window on the opposite building will hit Byteasar's building at a predictable vertical coordinate.

In this problem, the lamp is located on Byteasar's building (referred to as building A) at the point (0, L) (with L given). The two buildings are parallel; building A and building B’s walls are assigned Cartesian coordinate systems with the horizontal axis along the wall and the vertical axis upward. All windows are axis‐aligned open rectangles, meaning that a ray is reflected only if it hits strictly inside a window (touching the boundary causes absorption).

The light from the lamp does not reach Byteasar’s windows directly, but only after reflecting exactly once on a window on the opposite building (building B). By the law of reflection on a vertical wall the vertical component remains the same. Therefore, if a ray emanates from (0, L) and strikes an interior point (10, y) of a window on building B, then after reflection the ray will hit building A at the point (0, 2y - L). In other words, each window on building B with vertical interval \( (y_1, y_2) \) creates a target illumination interval on building A given by \( (2y_1 - L,\; 2y_2 - L) \) (note that the endpoints are not included).

You are given the positions of windows on both buildings. The windows on building A (Byteasar's building) have open vertical intervals (their horizontal extent is irrelevant, since all points lie on the wall at x=0) and similarly, the windows on building B (the opposite building at x=10) have open vertical intervals. Byteasar wonders how many windows on his own building (building A) are illuminated by some ray via exactly one reflection off a window on building B. A window on building A is illuminated if its open vertical interval has a nonempty intersection with the target illumination interval induced by at least one window on building B.

The input format is as follows:

  • The first line contains an integer L, the vertical coordinate of the lamp on building A.
  • The next line contains an integer n, the number of windows on building A.
  • Each of the next n lines contains two space-separated integers, representing the lower and upper bounds of a window's vertical interval on building A.
  • The following line contains an integer m, the number of windows on building B.
  • Each of the next m lines contains two space-separated integers, representing the lower and upper bounds of a window's vertical interval on building B.

The output is a single integer: the number of windows on building A that are illuminated by at least one reflection.

Note: A ray is reflected only if it hits strictly inside a window. Hence, the boundaries of intervals are not considered. Two intervals \((a, b)\) and \((c, d)\) have a nonempty intersection if and only if \(\max(a, c) < \min(b, d)\).

inputFormat

The input consists of:

  1. An integer L representing the vertical coordinate of the lamp on building A.
  2. An integer n, the number of windows on building A.
  3. n lines, each containing two integers separated by a space: the lower and upper vertical coordinates (\(a\) and \(b\)) of a window on building A. It is guaranteed that the lamp (located at y = L) is not inside or on the boundary of any window.
  4. An integer m, the number of windows on building B.
  5. m lines, each containing two integers separated by a space: the lower and upper vertical coordinates (\(y_1\) and \(y_2\)) of a window on building B.

outputFormat

Output a single integer: the number of windows on building A that are illuminated by at least one ray after exactly one reflection off a window on building B. A window is considered illuminated if its interior has a nonempty intersection with at least one target illumination interval \((2y_1 - L,\; 2y_2 - L)\) derived from a window on building B.

sample

5
2
6 8
0 3
2
3 4
4 6
2