Bodyguard
Time limit25sMemory limit2048 MB
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, very important people (VIP) will come to JOI Street and walk along it. VIPs are numbered from 1 to . VIP () departs from coordinate at time and walks to coordinate at speed 1. If , VIP moves in the positive direction at a constant speed. If , VIP 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 , the VIP pays the bodyguard yen per unit distance, measured over the distance the bodyguard guards the VIP. It is guaranteed that is an even integer.
You work for a security company and plan projects to guard the VIPs. Projects are numbered from 1 to . In project (), a bodyguard starts working at coordinate at time . 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 lines to standard output. The -th line contains the maximum total reward for project , as an integer.
Constraints
- ()
- ()
- ()
- ()
- ()
- is an even integer ()
- ()
- ()