#P7564. [JOISC 2021] ボディーガード (Day3)

[JOISC 2021] ボディーガード (Day3)

Description

On a number line, there are NN people, and they are all customers of the Bookworm Bodyguard Company. Customer ii will move from position AiA_i to position BiB_i at time TiT_i. Their speed is one unit of length per unit of time.

If a bodyguard and a customer are at the same position at the same time, we say that the bodyguard is protecting that customer. Suppose the bodyguard starts protecting customer ii at time aa and stops protecting at time bb. Then the interval [a,b][a,b] is called the protection time of this customer, time aa is called the start time of protection, and time bb is called the stop time of protection. Here, aa and bb do not have to be integers. In particular, if a bodyguard is at the same position at the same time with two customers, the bodyguard can protect only one customer.

A bodyguard can move freely on the number line with speed at most one unit of length per unit of time and at least staying still. When a bodyguard stops protecting one customer, they can go to another position to protect another customer. If the path length that the bodyguard walks together with customer ii while protecting them is LL, then customer ii will pay the bodyguard L×CiL \times C_i Zimbabwe dollars, at a rate of CiC_i Zimbabwe dollars per unit length.

As the owner of the Bookworm Bodyguard Company, Bookworm holds QQ planned protection proposals. In proposal jj, a bodyguard starts working at time PjP_j from position XjX_j.

For each proposal, find the maximum total wage (in Zimbabwe dollars) that the bodyguard can earn.

Input Format

The first line contains two integers N,QN,Q, representing the number of customers and the number of planned protection proposals.

The next NN lines each contain four integers Ti,Ai,Bi,CiT_i,A_i,B_i,C_i, describing one customer.

The next QQ lines each contain two integers Pj,XjP_j,X_j, describing one planned protection proposal.

Output Format

Output QQ lines. Each line contains one integer, representing the maximum total wage (in Zimbabwe dollars) that the bodyguard can earn for the corresponding proposal.

2 2
1 2 1 4
3 1 3 2
1 2
3 3
8
2
3 2
3 1 5 2
1 4 1 4
4 2 4 4
2 2
6 3
15
0
5 5
8 1 4 10
8 3 7 6
1 4 6 2
3 9 5 4
6 1 9 6
7 6
6 8
1 3
9 4
2 4
30
27
48
30
48

Hint

Explanation for Sample 1

  • In proposal 11, the bodyguard can earn 4+4=84+4=8 Zimbabwe dollars in the following way.
    • Start moving from position 22 at time 11.
    • Protect customer 11 from time 11 to time 22. The path length traveled together is 11, so the wage is 4×1=44 \times 1=4 Zimbabwe dollars.
    • Stay at position 11 from time 22 to time 33.
    • Protect customer 22 from time 33 to time 55. The path length traveled together is 22, so the wage is 2×2=42 \times 2=4 Zimbabwe dollars.
  • In proposal 22, the bodyguard can earn 22 Zimbabwe dollars in the following way.
    • Start moving from position 33 at time 33.
    • Move from position 33 to position 22 from time 33 to time 44.
    • Protect customer 22 from time 44 to time 55. The path length traveled together is 11, so the wage is 2×1=22 \times 1=2 Zimbabwe dollars.

Explanation for Sample 2

  • In proposal 11, the bodyguard can earn 4+1+8+2=154+1+8+2=15 Zimbabwe dollars in the following way.
    • Start moving from position 22 at time 22.
    • Move from position 22 to position 2.52.5 from time 22 to time 2.52.5.
    • Protect customer 22 from time 2.52.5 to time 3.53.5. The path length traveled together is 11, so the wage is 4×1=44 \times 1=4 Zimbabwe dollars.
    • Protect customer 11 from time 3.53.5 to time 44. The path length traveled together is 0.50.5, so the wage is 2×0.5=12 \times 0.5=1 Zimbabwe dollars.
    • Protect customer 33 from time 44 to time 66. The path length traveled together is 22, so the wage is 4×2=84 \times 2=8 Zimbabwe dollars.
    • Protect customer 11 from time 66 to time 77. The path length traveled together is 11, so the wage is 2×1=22 \times 1=2 Zimbabwe dollars.
  • In proposal 22, no matter how the bodyguard moves, they cannot earn any wage, and can only get 00 Zimbabwe dollars.

Constraints and Notes

This problem uses bundled subtasks.

  • Subtask 1 (6 pts): Ti,Ai,Bi,Pj,Xj3000T_i,A_i,B_i,P_j,X_j \le 3000.
  • Subtask 2 (7 pts): Q=1Q=1.
  • Subtask 3 (15 pts): Q3000Q \le 3000.
  • Subtask 4 (20 pts): Q4×104Q \le 4 \times 10^4.
  • Subtask 5 (52 pts): No special restrictions.

For all testdata, 1N28001 \le N \le 2800, 1Q3×1061 \le Q \le 3 \times 10^6, 1Ti,Ai,Bi,Ci1091 \le T_i,A_i,B_i,C_i \le 10^9, AiBiA_i \ne B_i, CiC_i is even, and 1Pj,Xj1091 \le P_j,X_j \le 10^9.

Note

Translated from The 20th Japanese Olympiad in Informatics Spring Training Camp, Day3 B ボディーガード (Bodyguard) English version.

Translated by ChatGPT 5