QOJ.ac

QOJ

時間限制: 0.5 s 記憶體限制: 512 MB 總分: 100

#9991. Walking in Opposite Directions

统计

There are $m$ piles of blocks, where the $i$-th pile is located at coordinate $x_i$ and contains $c_i$ blocks.

Repeatedly perform the following operation until it is no longer possible:

  • If there are two blocks at the same coordinate, find the two blocks with the smallest coordinate among those that satisfy the condition, decrease the coordinate of one by $1$, and increase the coordinate of the other by $1$.

It can be proven that after a finite number of operations, the coordinates of all blocks will be distinct, at which point no further operations can be performed.

Given multiple queries, each with a positive integer $k$, find the position of the $k$-th block from the left. It is guaranteed that the queries $k$ are strictly increasing.

Input

The input is read from standard input.

The first line contains a positive integer $m$. It is guaranteed that $1 \le m \le 2 \times 10^6$.

The next $m$ lines each contain two positive integers $x_i$ and $c_i$. It is guaranteed that $1 \le x_i \le 10^9$ and $1 \le c_i \le 10^9$. It is guaranteed that $x_i$ are strictly increasing in the order of input, i.e., $x_i < x_{i+1}$.

The next line contains a positive integer $q$, representing the number of queries. It is guaranteed that $1 \le q \le 2 \times 10^6$.

The next $q$ lines each contain a positive integer $k$, representing a query. It is guaranteed that $1 \le k \le \sum_{i=1}^{m} c_i \le 10^9$. It is guaranteed that the queries $k$ are strictly increasing.

Output

Output to standard output.

Output $q$ lines, each containing an integer, where the $i$-th line represents the answer to the $i$-th query.

Examples

Input 1

2
3 3
4 2
2
2
4

Output 1

2
5

Note 1

We describe the positions of the five blocks from left to right using a non-decreasing sequence of length 5. The operation process is as follows:

$33344 \to 23444 \to 23345 \to 22445 \to 13445 \to 13355 \to 12455 \to 12446 \to 12356$

Finally, the coordinate of the second block is 2, and the coordinate of the fourth block is 5.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.