#P7167. [eJOI 2020] Fountain (Day1)

[eJOI 2020] Fountain (Day1)

Description

Everyone knows what a fountain is, right? Now there is a fountain made of NN disks, numbered from top to bottom as 1N1 \sim N. The diameter of the ii-th disk is DiD_i, and its capacity is CiC_i. If the amount of water in a disk exceeds its capacity, the water will overflow and flow downward, until it enters a disk whose radius is larger than the radius of this disk. If there is no disk below that satisfies this condition, the water will flow into the pool under the fountain.

Now you are given QQ queries. Each query is described as follows:

  • Pour ViV_i units of water into disk RiR_i, and determine which disk the water will finally flow into and stop at.

If it finally flows into the pool, output 00.

Note that each query does not affect the others.

Input Format

The first line contains two integers N,QN, Q, representing the number of disks and the number of queries.
The next NN lines each contain two integers Di,CiD_i, C_i, representing a disk.
The next QQ lines each contain two integers Ri,ViR_i, V_i, representing a query.

Output Format

Output QQ lines, each containing one integer representing the answer to the query.

6 5
4 10
6 8
3 5
4 14
10 9
4 20
1 25
6 30
5 8
3 13
2 8
5
0
5
4
2

Hint

Sample 1 Explanation

The first two queries are illustrated in the figure below:

Because each query does not affect the others, for the third query, the water in disk 55 will not overflow.

Constraints and Notes

This problem uses bundled testdata.

  • Subtask 1 (30 pts): N1000N \le 1000, Q2000Q \le 2000.
  • Subtask 2 (30 pts): DiD_i is a strictly increasing sequence.
  • Subtask 3 (40 pts): No special restrictions.

For 100%100\% of the testdata:

  • 2N1052 \le N \le 10^5.
  • 1Q2×1051 \le Q \le 2 \times 10^5.
  • 1Ci10001 \le C_i \le 1000.
  • 1Di,Vi1091 \le D_i, V_i \le 10^9.
  • 1RiN1 \le R_i \le N.

Statement

Translated from eJOI 2020 Day1 A Fountain.

Translated by ChatGPT 5