#P8755. [蓝桥杯 2021 省 AB2] 负载均衡

[蓝桥杯 2021 省 AB2] 负载均衡

Problem Description

There are nn computers. The computing capacity of the ii-th computer is viv_{i}.

A series of tasks are assigned to computers. The ii-th task is assigned at time aia_{i}, to the computer with index bib_{i}, with duration cic_{i}, and computing power consumption did_{i}. If the task is assigned successfully, it starts running immediately. During its execution, it continuously occupies did_{i} computing capacity of computer bib_{i} for cic_{i} seconds.

For each task assignment, if the remaining computing capacity of the computer is not enough, output 1-1 and cancel this assignment. Otherwise, output the remaining computing capacity of this computer after assigning this task.

Input Format

The first line contains two integers n,mn, m, representing the number of computers and the number of tasks to be assigned.

The second line contains nn integers v1,v2,vnv_{1}, v_{2}, \cdots v_{n}, representing the computing capacity of each computer.

The next mm lines each contain 44 integers ai,bi,ci,dia_{i}, b_{i}, c_{i}, d_{i}, with the meanings described above. The testdata guarantees that aia_{i} is strictly increasing, i.e., ai<ai+1a_{i}<a_{i+1}.

Output Format

Output mm lines. Each line contains one number, corresponding to the result of each task assignment.

2 6
5 5
1 1 5 3
2 2 2 6
3 1 2 3
4 1 6 1
5 1 3 3
6 1 3 4
2
-1
-1
1
-1
0

Hint

[Sample Explanation]

At time 11, task 11 is assigned to computer 11, with duration 55. This task will end at time 66, and it occupies computing capacity 33 of computer 11.

At time 22, task 22 does not have enough required computing capacity, so the assignment fails.

At time 33, computer 11 is still running task 11, and its remaining computing capacity is less than 33, so it fails.

At time 44, computer 11 is still running task 11, but its remaining computing capacity is enough. After assignment, the remaining computing capacity is 11.

At time 55, computer 11 is still running tasks 11 and 44, and its remaining computing capacity is less than 44, so it fails.

At time 66, computer 11 is still running task 44. Its remaining computing capacity is enough, and it is used up exactly.

[Constraints and Conventions for Test Cases]

For 20%20\% of the test cases, n,m200n, m \leq 200.

For 40%40\% of the test cases, n,m2000n, m \leq 2000.

For all test cases, $1 \leq n, m \leq 2\times 10^5, 1 \leq a_{i}, c_{i}, d_{i}, v_{i} \leq 10^{9}, 1 \leq b_{i} \leq n$.

Lanqiao Cup 2021, Round 2, Provincial Contest, Group A Problem H (Group B Problem I).

Translated by ChatGPT 5