GPU Reservations

medium segment-tree intervals arrays 2000 ms · 256 MB

GPU Reservations

Your team shares a training cluster of C identical GPUs. The planning horizon is n hours, numbered 1 to n. Engineers don't grab GPUs directly; they send requests to a reservation service, which processes them one at a time in the order received. Requests are numbered 1 to q in that order.

There are two kinds of requests:

  • book l r k: reserve k GPUs for every hour from l to r, inclusive. The service accepts the booking if, in every hour from l to r, the GPUs already reserved by active bookings plus k is at most C. An accepted booking becomes active. Otherwise the booking is rejected and changes nothing. A booking is never partially granted.
  • cancel j: request j is a book request that came earlier. If it was accepted and is still active, it stops being active and its GPUs are released for all of its hours. If it was rejected, or has already been cancelled, nothing happens.

For each book request, report whether the service accepts it.

Input

The first line contains three integers n, C and q (1 ≤ n ≤ 2·10^5, 1 ≤ C ≤ 10^9, 1 ≤ q ≤ 2·10^5): the number of hours, the number of GPUs and the number of requests.

Each of the next q lines describes one request, in order. The i-th of them is either book l r k (1 ≤ l ≤ r ≤ n, 1 ≤ k ≤ 10^9) or cancel j (1 ≤ j < i), where request j is guaranteed to be a book request.

Output

For each book request, in input order, print a line containing ACCEPT if it is accepted or REJECT if it is rejected. Print nothing for cancel requests.

Notes

In the first example, request 2 is rejected because hour 3 already has 2 of the 3 GPUs reserved. After request 1 is cancelled, request 5 is still rejected, because hours 4 and 5 are fully taken by request 3, but request 6 fits exactly.

In the second example, cancel 2 does nothing because request 2 was rejected, and the second cancel 1 does nothing because request 1 is no longer active.

Example 1

Input

5 3 6
book 1 3 2
book 3 5 2
book 4 5 3
cancel 1
book 3 5 1
book 1 3 3

Output

ACCEPT
REJECT
ACCEPT
REJECT
ACCEPT

Example 2

Input

4 2 7
book 1 4 2
book 2 2 1
cancel 2
cancel 1
cancel 1
book 1 4 2
book 4 4 1

Output

ACCEPT
REJECT
ACCEPT
REJECT

Hints · reveal one at a time if you're stuck

Hint 1

Think of each hour as having a load: the number of GPUs already reserved then. For a booking to fit, which single quantity over its hours must stay within capacity once k is added? Checking only the endpoint hours is not enough.

Hint 2

Accepting a booking adds k to every hour in its range, and cancelling an active one subtracts it. Rejected bookings and repeated cancels must change nothing, so remember each booking and whether it is still active. Scanning the hours for every request is too slow at 2·10^5 requests, so you need something faster than O(n) per operation.

Hint 3

You need range add and range maximum, each in O(log n): a segment tree with lazy propagation.

report an issue Prefer your own editor? sooks prep 8 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.