Machine Gun
Time limit2sMemory limit512 MB
For each query, find all enemies inside a right-facing angular wedge and evaluate a polynomial hash of their sorted indices; queries are forced online and the total number of hits is bounded.
- Level
Hard8 of 10
- Topics
- Geometry, Sorting, Binary search, Implementation
- Solved
- No attempts yet
Problem
Fulanito went to play an old arcade game. In the game, he can place a machine gun anywhere in his base, which consists of all points with integer coordinates and . There are enemies on the battlefield. The -th enemy () is at position with . All positions are given in advance.
A machine gun placed at covers a viewing angle facing right, centered on the line , whose boundaries are given by the lines . Once placed, it hits every enemy in the region bounded by this angle, including those located on the boundary lines.

Figure 1: Pictorial representation of the sample input
The scoring system used by this game is unnecessarily complicated; many believe that such a system was a big mistake by the developers (who, in response, state with conviction that “it's not a bug, it's a feature!”). Specifically, the score obtained by a given placement of the machine gun is computed by performing the following steps:
- List the indices ( between 1 and ) of all enemies the machine gun hits.
- Sort the indices in increasing order, and call the sorted values .
- Compute the score using the formula , where denotes the remainder of dividing by .
- Note: A machine gun that hits no enemies receives a score exactly equal to 0.
To get better at this game, Fulanito asks you questions: each query asks for the score that would be obtained if we placed the machine gun at a certain position . To make the problem more challenging, the values of are not given directly. Instead, values and are given, which can be used to compute and through the formulas and , where is the answer to the previous query ( when processing the first query).
NOTE: It is guaranteed that the sum of the number of enemies hit across all queries is at most .
Input
The input consists of several lines. The first line of the input contains two integers (), the number of enemies and the number of queries.
The next lines of the input contain two integers each: and (), the coordinates of the position of the -th enemy.
The next lines contain two integers each: the values and () that specify each query, as explained in the statement.
Output
For each query, print a single integer containing the answer to the query.