problems / 169

API Quota Manager

easy design hashing simulation 1000 ms · 256 MB

API Quota Manager

Your API service allocates k requests per user per calendar day. You need to process incoming API requests, track per-user daily quota consumption, and enforce rate limits.

Each request specifies a date and user ID. On each date, a user has exactly k requests available. If a request arrives for fewer requests than the user's remaining quota on that day, it is approved and the quota is consumed. Otherwise, it is rejected. When a new date arrives, the quota for all users resets to k.

For each request, output whether it was approved or rejected, along with the user's remaining quota on that date after the decision.

Input

The first line contains two integers n and k (1 ≤ n ≤ 10^5, 1 ≤ k ≤ 10^9).

The next n lines each contain an integer date, an integer user, and an integer count (0 ≤ date ≤ 10^6, 0 ≤ user ≤ 10^6, 1 ≤ count ≤ 10^9). Dates are given in non-decreasing order within the input.

Output

For each request, print a line with either APPROVED or REJECTED, followed by the user's remaining quota on that date (in requests). The remaining quota is the quota after accounting for this request, whether it was approved or not.

Notes

  • A request is approved if and only if the user's current remaining quota on that date is at least count.
  • Quotas are per calendar day: each date has an independent quota pool of k requests per user.
  • Users and dates may be sparse (not all IDs or dates are queried).
  • Handle the constraints efficiently: a straightforward hash map solution suffices.

Example 1

Input

5 5
0 0 2
0 0 3
0 0 1
1 0 2
1 0 4

Output

APPROVED 3
APPROVED 0
REJECTED 0
APPROVED 3
REJECTED 3

Example 2

Input

8 10
0 1 5
0 2 7
0 1 5
0 2 4
0 1 1
0 2 3
1 1 10
1 2 5

Output

APPROVED 5
APPROVED 3
APPROVED 0
REJECTED 3
REJECTED 0
APPROVED 0
APPROVED 0
APPROVED 5

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

Hint 1

Quota is tracked separately for each (date, user) pair, and a pair that has not been seen yet starts with the full quota k. Which lookup structure keeps the remaining quota for each pair?

Hint 2

Look up the remaining quota for the pair, approve and subtract only when it is at least count, and print the quota left after the decision either way. Use 64-bit integers.

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

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