#P14806. [CCPC 2024 哈尔滨站] 一维星系

[CCPC 2024 哈尔滨站] 一维星系

Description

In a magical one-dimensional space, there are nn planets numbered from 11 to nn. Initially (t=0t=0), the planet numbered ii is located at position xix_i with weight wiw_i (which can be negative). In the real world, planets move under the influence of gravity, and similarly, in this one-dimensional galaxy, planets move due to attraction. However, the movement in this galaxy does not follow conventional physical laws. Specifically, for any planet in this one-dimensional galaxy, if the total weight of planets to its left is greater than the total weight of planets to its right, it moves one unit to the left at the next time step. Conversely, if the total weight on the right is greater than that on the left, it moves one unit to the right. If both sides are equal, it remains in the same position. You can assume that the planets do not physically collide, meaning they can pass through each other.

Formally, let the position of the planet numbered ii at time tt (t=0,1,2,t = 0, 1, 2, \ldots) be xi,tx_{i,t}. The total weight of the planets to its left at this time is wi,tl=j : xj,t<xi,twjw_{i,t}^l = \sum_{j\ :\ x_{j,t} < x_{i,t}} w_j, and the total weight of the planets to its right is wi,tr=j : xj,t>xi,twjw_{i,t}^r = \sum_{j\ :\ x_{j,t} > x_{i,t}} w_j. The position of the planet at the next time step, xi,t+1x_{i, t+1}, is given by:

$$x_{i, t+1} = \begin{cases} x_{i,t} - 1, & w_{i,t}^l > w_{i,t}^r \\ x_{i,t} + 1, & w_{i,t}^l < w_{i,t}^r \\ x_{i,t}, & w_{i,t}^l = w_{i,t}^r \end{cases}$$

There are qq queries, each asking for the position of the planet numbered ii at a specific time tt. Please answer these queries.

Input Format

The first line contains two integers nn and qq (1n,q1051 \le n, q \le 10^5), representing the number of planets and the number of queries, respectively.

The ii-th of the next nn lines contains two integers xi,wix_i, w_i (109xi,wi109-10^9 \le x_i, w_i \le 10^9), representing the initial position and weight of the planet numbered ii.

The following qq lines each contain two integers tt and ii (0t1090 \le t \le 10^9, 1in1 \le i \le n), representing a query.

Output Format

Output qq lines, each representing the answer to the corresponding query.

4 12
0 1
1 2
-1 3
2 2
0 1
0 2
0 3
0 4
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
0
1
-1
2
1
0
0
1
0
1
1
0