#P1083. [NOIP 2012 提高组] 借教室

[NOIP 2012 提高组] 借教室

Description

During university, classrooms often need to be rented. From department events to small study groups, everyone needs to apply to the school to borrow classrooms. Classroom sizes and functions differ, and the borrower’s identity may differ, so the procedures vary as well.

Given a large number of rental requests, we want to solve this problem by programming.

We need to process borrowing requests for the next nn days. On day ii, the school has rir_i classrooms available for rent. There are mm orders. Each order is described by three positive integers dj,sj,tjd_j, s_j, t_j, meaning a borrower wants to rent djd_j classrooms per day from day sjs_j to day tjt_j inclusive.

We assume borrowers have no requirements regarding classroom size or location. That is, for each order we only need to provide djd_j classrooms each day; which specific classrooms they are, or whether they are the same across days, does not matter.

Borrowing follows a first-come, first-served principle. We process orders in the given order and allocate classrooms accordingly. If, during allocation, an order cannot be fully satisfied, we must stop allocation and notify the current applicant to modify the order. Here, “cannot be satisfied” means that on at least one day between sjs_j and tjt_j, the remaining number of classrooms is less than djd_j.

We need to determine whether any order cannot be fully satisfied. If so, which applicant should be notified to modify their order.

Input Format

  • The first line contains two positive integers n,mn, m, the number of days and the number of orders.
  • The second line contains nn positive integers; the ii-th number is rir_i, the number of classrooms available on day ii.
  • The next mm lines each contain three positive integers dj,sj,tjd_j, s_j, t_j, denoting the quantity requested per day and the start and end days of the rental.
  • Adjacent numbers on the same line are separated by a single space. Days and orders are both numbered starting from 11.

Output Format

  • If all orders can be satisfied, output a single line containing the integer 00.
  • Otherwise, output two lines: the first line contains the negative integer 1-1, and the second line contains the index of the applicant whose order needs to be modified.
4 3 
2 5 4 3 
2 1 3 
3 2 4 
4 2 4
-1 
2

Hint

Sample explanation:

After satisfying order 11, the remaining classrooms for 44 days are 0,3,2,30, 3, 2, 3. Order 22 asks for 33 classrooms per day from day 22 to day 44, but on day 33 the remaining classrooms are 22, so it cannot be satisfied. Allocation stops, and the applicant of order 22 is notified to modify the order.

Constraints:

  • For 10%10\% of the testdata, 1n,m101 \le n, m \le 10.
  • For 30%30\% of the testdata, 1n,m10001 \le n, m \le 1000.
  • For 70%70\% of the testdata, 1n,m1051 \le n, m \le 10^5.
  • For 100%100\% of the testdata, 1n,m1061 \le n, m \le 10^6, 0ri,dj1090 \le r_i, d_j \le 10^9, 1sjtjn1 \le s_j \le t_j \le n.

Additional notes:

NOIP 2012 Senior Day 2, Problem 2.

A new set of hack testdata was added on 2022.2.20.

Translated by ChatGPT 5