problems / 284

Best-Fit Memory Allocator

medium ordered-set simulation intervals design 1000 ms · 256 MB

Best-Fit Memory Allocator

An embedded runtime manages a single contiguous heap of M bytes with addresses 0 to M − 1. At any moment the heap is split into allocated blocks and free blocks. Free blocks are always kept maximal: two free blocks never touch each other. At the start the whole heap is one free block of M bytes at address 0.

The runtime uses a best-fit policy. It processes q requests in order:

  • ALLOC k: request k bytes. Among all free blocks of size at least k, take the one with the smallest size; if several have that size, take the one with the lowest start address. The allocation occupies the first k bytes of that block, starting at its start address, and the remaining bytes (if any) stay a free block right after it. If no free block has at least k bytes, the request fails and nothing changes.
  • FREE id: release the allocation made by the id-th ALLOC request. Its bytes become free and merge with a free block that ends exactly where it starts and with a free block that starts exactly where it ends, if such blocks exist.

ALLOC requests are numbered 1, 2, 3, … in the order they appear, counting failed ones too.

For every ALLOC request print the address it returned, or -1 if it failed. After all requests, print the number of free blocks and the size of the largest one.

Input

The first line contains two integers M and q (1 ≤ M ≤ 10^18, 1 ≤ q ≤ 2·10^5): the heap size in bytes and the number of requests.

Each of the next q lines is either ALLOC k (1 ≤ k ≤ 10^18) or FREE id. For every FREE id it is guaranteed that the id-th ALLOC request appears earlier, that it succeeded, and that its allocation has not been freed yet.

Output

For each ALLOC request, in order, print one line with its start address, or -1 if it failed.

Then print one line with two integers: the number of free blocks after all requests, and the size in bytes of the largest free block (0 0 if there are no free blocks).

Addresses and sizes can exceed 2^31; use 64-bit integers.

Notes

In the first example, after FREE 1 and FREE 3 the free blocks are [0, 30), [50, 60) and [85, 100) (sizes 30, 10 and 15). ALLOC 12 takes the 15-byte block at address 85 even though the block at 0 comes first, and ALLOC 8 takes the 10-byte block at 50. FREE 2 releases [30, 50), which merges with [0, 30); the block at 50 is allocated again, so the merge stops there. The largest free block then has 50 bytes, so ALLOC 60 fails.

In the second example, after FREE 1 and FREE 3 there are free blocks of 3 bytes at 0 and 4 bytes at 6 ([6, 9) merged with the leftover byte at 9). ALLOC 3 fits exactly at address 0. The 6th request fails, and the final FREE 2 joins [0, 3), [3, 6) and [6, 10) into one block of 10 bytes.

Example 1

Input

100 10
ALLOC 30
ALLOC 20
ALLOC 10
ALLOC 25
FREE 1
FREE 3
ALLOC 12
ALLOC 8
FREE 2
ALLOC 60

Output

0
30
50
60
85
50
-1
3 50

Example 2

Input

10 11
ALLOC 3
ALLOC 3
ALLOC 3
FREE 1
FREE 3
ALLOC 3
ALLOC 4
ALLOC 1
FREE 4
FREE 5
FREE 2

Output

0
3
6
0
6
-1
1 10

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

Hint 1

With M up to 10^18 you can't model individual bytes. What is the most free blocks that can exist at once, and which operations do you need on them?

Hint 2

Two different lookups are needed: "smallest block with size at least k, lowest address on ties" for ALLOC, and "the free blocks touching this address range on either side" for FREE. One ordering can't serve both, so keep every free block in two structures.

Hint 3

Keep a self-balancing binary search tree (an ordered set) of (size, address) pairs, where a lower-bound search gives the best fit directly, and a second ordered map keyed by address to find a released block's neighbours.

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

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