Under construction

Still being built. Things may change

Bodyguard

Time limit25sMemory limit2048 MB

Summary
For each query, find the maximum reward a bodyguard starting at coordinate X at time P can earn by escorting VIPs whose walks are given in space-time.
Level

Hard9 of 10

Topics
Dynamic programming, Sorting, Greedy
Solved
No attempts yet

Problem

JOI Street is a long street running from west to east. It is treated as the number line.

From now on, NN very important people (VIP) will come to JOI Street and walk along it. VIPs are numbered from 1 to NN. VIP ii (1≤i≤N1 \le i \le N) departs from coordinate AiA_i at time TiT_i and walks to coordinate BiB_i at speed 1. If Ai<BiA_i < B_i, VIP ii moves in the positive direction at a constant speed. If Ai>BiA_i > B_i, VIP ii moves in the negative direction at a constant speed.

A bodyguard's job is to walk along JOI Street and guard the VIPs. To guard a VIP, the bodyguard must walk with the VIP for some time. A bodyguard may start guarding a VIP in the middle of the VIP's walk, or quit guarding before the VIP finishes walking. The start and end times of guarding need not be integers. However, even if several VIPs are at the same coordinate, a bodyguard can guard at most one VIP at a time.

A bodyguard can walk along JOI Street freely at a speed of at most 1 per unit time. After finishing guarding a VIP, the bodyguard may move to another place and start guarding another VIP. If a bodyguard walks with VIP ii, the VIP pays the bodyguard CiC_i yen per unit distance, measured over the distance the bodyguard guards the VIP. It is guaranteed that CiC_i is an even integer.

You work for a security company and plan QQ projects to guard the VIPs. Projects are numbered from 1 to QQ. In project jj (1≤j≤Q1 \le j \le Q), a bodyguard starts working at coordinate XjX_j at time PjP_j. For each project, compute the maximum total reward. Under the constraints of this task, the maximum total reward of a project is always an integer.

Input

Read the following data from standard input. All given values are integers.

N Q
T1 A1 B1 C1
.
.
.
TN AN BN CN
P1 X1
.
.
.
PQ XQ

Output

Write QQ lines to standard output. The jj-th line contains the maximum total reward for project jj, as an integer.

Constraints

  • 1≤N≤28001 \le N \le 2800
  • 1≤Q≤30000001 \le Q \le 3000000
  • 1≤Ti≤1091 \le T_i \le 10^9 (1≤i≤N1 \le i \le N)
  • 1≤Ai≤1091 \le A_i \le 10^9 (1≤i≤N1 \le i \le N)
  • 1≤Bi≤1091 \le B_i \le 10^9 (1≤i≤N1 \le i \le N)
  • Ai≠BiA_i \ne B_i (1≤i≤N1 \le i \le N)
  • 1≤Ci≤1091 \le C_i \le 10^9 (1≤i≤N1 \le i \le N)
  • CiC_i is an even integer (1≤i≤N1 \le i \le N)
  • 1≤Pj≤1091 \le P_j \le 10^9 (1≤j≤Q1 \le j \le Q)
  • 1≤Xj≤1091 \le X_j \le 10^9 (1≤j≤Q1 \le j \le Q)

Examples2

  1. Example 1

    Input
    2 2
    1 2 1 4
    3 1 3 2
    1 2
    3 3
    
    Expected output
    8
    2
    
  2. Example 2

    Input
    3 2
    3 1 5 2
    1 4 1 4
    4 2 4 4
    2 2
    6 3
    
    Expected output
    15
    0