This page is still under construction.

Parts of this page are still being built. What you see may change.

Folding

Time limit2sMemory limit1024 MB

Summary
Given two disjoint red intervals on a 1e9 long tape, fold at position x and report the total length of the resulting red region, for up to 1e6 queries.
Level

Medium6 of 10

Topics
Geometry, Math, Implementation, Intervals
Solved
No attempts yet

Problem

There is a transparent tape. Its length is exactly one meter (10^9 nanometers). In this problem, all numbers are integers, and we use a number to denote a position on the tape. The number p denotes the position of the point that is p nanometers from the head of the tape.

Bob is a master dyer, so he can color the tape precisely at the nanometer scale. He colors two sectors [p1, q1] and [p2, q2] red. The tape between p1 and q1 is red. The tape between p2 and q2 is also red. The rest of the tape stays transparent.

To verify Bob's skill, we ask Ben, the tape folding master, to help us. Ben can fold the tape perfectly at any position. If Ben folds the tape at x, the new position of a point p is one of the following.

  • If p = x, it becomes the new head of the tape, that is, 0.
  • If p > x, it becomes p - x.
  • If p < x, it becomes x - p.

After Ben folds the tape, we measure the total length of the red part of the new tape. If the red part has the expected length, we believe both Bob and Ben are masters of their skills. The color of a position of the new tape is determined by the colors of the corresponding positions of the old tape. A position of the new tape is red if one of the corresponding positions in the old tape is red.

Bob has already colored the tape, and Ben has proposed the positions to fold. Write a program to compute the expected lengths colored red.

Input

The first line contains four space-separated integers p1, q1, p2, and q2. Bob has colored the sectors [p1, q1] and [p2, q2]. The second line contains an integer q, which means Ben has made q proposals. Each of the remaining q lines contains an integer x, the position at which Ben folds. The q proposals are independent of one another. There is only one folding point in one proposal.

Output

For each position, output the expected total length of the new tape that is colored red.

Constraints

  • 0 ≤ p1 < q1 < p2 < q2 ≤ 10^9
  • 0 ≤ x ≤ 10^9
  • q ≤ 10^6

Examples1

  1. Example 1

    Input
    1 3 8 9
    10
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    Expected output
    3
    2
    3
    3
    2
    3
    3
    3
    3
    3