luogu#P9933. [NFLSPC #6] 9.pop_book();

[NFLSPC #6] 9.pop_book();

Background

Alek Sui is running laps on the playground. He feels upset when he sees someone overtake him, so he uses the following strategy.

Problem Description

On a circular playground of length mm, there are nn people. Person ii starts from position pip_i at time tit_i and moves at speed viv_i units per second. Now at time 00, Alek Sui is at position 00 with speed 00, and he will follow the fastest person who passes by him. There are qq queries asking for the distance Alek Sui has moved at time TiT_i. It can be proven that this is an integer.

Note: The position that is xx (0x<m0\leq x < m) units counterclockwise from position 00 is called position xx. Everyone moves counterclockwise.

Multiple test cases.

Input Format

The first line contains an integer TT indicating the number of test cases. For each test case:

  • The first line contains three integers n,m,qn, m, q, representing the number of people, the playground length, and the number of queries.
  • The next nn lines each contain three integers pi,vi,tip_i, v_i, t_i, representing the starting position of person ii, the moving speed (units per second), and the starting time.
  • The next qq lines each contain one integer TiT_i, representing a query.

Output Format

For each query, output one line with an integer representing the answer.

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

0
8
16
19
23
27
31
36

Hint

For all testdata, 1T1031\leq T\leq 10 ^ 3, 1n,n5×1051\leq n, \sum n\leq 5\times 10 ^ 5, 1m,q,q1061\leq m, q, \sum q \leq 10 ^ 6, 1vi,ti,Ti1091\leq v_i, t_i, T_i\leq 10 ^ 9, 0pi<m0\leq p_i < m. It is guaranteed that tit_i is non-decreasing and TiT_i is strictly increasing.

  • Subtask 1 (1010 points): n5n\leq 5.
  • Subtask 2 (1010 points): n50n\leq 50.
  • Subtask 3 (2020 points): n500n\leq 500.
  • Subtask 4 (2020 points): n5×103n\leq 5\times 10 ^ 3.
  • Subtask 5 (2020 points): n5×104n\leq 5\times 10 ^ 4.
  • Subtask 6 (2020 points): no special constraints.

Please note that the subtasks do not guarantee the order of magnitude of q\sum q.

The I/O size of this problem is large. It is recommended to use scanf/printf, or disable synchronization for cin/cout, or use fast input and fast output.

Source: NFLSPC #6 I by Alex_Wei

Translated by ChatGPT 5