Conference Hotel Blocks

hard segment-tree intervals arrays 2800 ms · 256 MB

Conference Hotel Blocks

A conference hotel has a single long corridor of n rooms, numbered 1 to n from the elevator. Event organisers call the front desk to reserve a block of rooms for their group: a block must be k adjacent rooms, so that the whole group stays together. The booking system always hands out the block closest to the elevator, which keeps the far end of the corridor free for late arrivals.

At the start every room is free. The desk then processes q requests in order:

  • book k: find the smallest x such that rooms x, x+1, …, x+k−1 are all free. If such an x exists, those k rooms become occupied and the answer is x. Otherwise nothing changes and the answer is 0.
  • checkout l r: every room from l to r inclusive becomes free. Some of these rooms may already be free; they simply stay free. A checkout may cover parts of several blocks, and part of a block may be checked out while the rest stays occupied.

Report the answer to every book request.

Input

The first line contains two integers n and q (1 ≤ n ≤ 2·10^5, 1 ≤ q ≤ 2·10^5): the number of rooms and the number of requests.

Each of the next q lines contains one request, either book k (1 ≤ k ≤ n) or checkout l r (1 ≤ l ≤ r ≤ n). At least one request is a book request.

Output

For every book request, in input order, print one line with the number of the first room of the assigned block, or 0 if no k adjacent rooms are free.

Notes

In the first example the corridor has 10 rooms. The first three bookings take rooms 1–3, 4–5 and 6–9. A block of 2 no longer fits (only room 10 is free), so the fourth answer is 0. After rooms 2 to 4 are checked out, the free rooms are 2, 3, 4 and 10, so the block of 3 starts at room 2 and the block of 1 gets room 10.

In the second example, after the two checkouts the free rooms are 3–5 and 8–10. The block of 4 does not fit anywhere, and the block of 3 goes to the lower-numbered run, starting at room 3.

Example 1

Input

10 7
book 3
book 2
book 4
book 2
checkout 2 4
book 3
book 1

Output

1
4
6
0
2
10

Example 2

Input

10 5
book 10
checkout 3 5
checkout 8 10
book 4
book 3

Output

1
0
3

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

Hint 1

Treat the corridor as an array of rooms that are free or occupied. Both kinds of request assign a whole range, and each booking needs the leftmost run of k free rooms. What summary of a range would let you answer that question quickly?

Hint 2

Use a segment tree where each node stores the longest free prefix, the longest free suffix, and the longest free run inside its range. A free run can straddle the middle of a node, so the children's best values alone are not enough when you combine them.

Hint 3

To find the leftmost run of length k, descend from the root and check three places in left-to-right order: inside the left child, across the midpoint, then inside the right child. Lazy tags that set a whole range to free or occupied handle the checkouts and bookings.

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

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