This page is still under construction.

Parts of this page are still being built. What you see may change.

Dynamic memory allocation

Time limit1sMemory limit1024 MB

Summary
Simulate a memory allocator over n bytes: allocate the leftmost run of l free bytes, or free a range and count how many bytes were actually freed.
Level

Hard8 of 10

Topics
Intervals, Segment tree, Binary search
Solved
No attempts yet

Problem

Kamila is designing a new programming language. She has already written a precise specification for the memory management constructs, but the implementation is still missing. Build the memory management system her specification describes.

The available memory is an array of nn bytes numbered 00 through n−1n-1. At the beginning every byte is free, meaning no byte is allocated. The system then processes a sequence of queries in order, allocating and freeing bytes.

An allocation query is given by one integer ℓ\ell. The system finds a block of ℓ\ell consecutive free bytes, allocates them, and returns the position of the first byte of that block. If several such blocks exist, the one starting at the smallest position is chosen. If no such block exists, the query is rejected and the system returns −1-1. A rejected query leaves the memory unchanged.

A freeing query is given by two integers xx and ℓ\ell. The system marks the ℓ\ell consecutive bytes starting at position xx as free and returns the number of bytes it actually freed. A byte counts as actually freed when it was not free immediately before the query.

Input

The first line contains the number of bytes in the memory nn and the number of queries qq (1≤n,q≤3×1051 \le n, q \le 3 \times 10^5).

Each of the next qq lines describes one query. The first integer on a line is the query type, where 11 means allocation and 22 means freeing. An allocation query continues with one integer ℓ\ell (1≤ℓ≤n1 \le \ell \le n). A freeing query continues with two integers xx and ℓ\ell (0≤x≤n−10 \le x \le n-1, 1≤ℓ≤n−x1 \le \ell \le n-x).

Output

For every query, print the value returned by the system on its own line, in the given order.

Examples3

  1. Example 1

    Input
    5 4
    1 3
    1 3
    2 1 3
    1 4
    
    Expected output
    0
    -1
    2
    1
    
  2. Example 2

    Input
    1 3
    1 1
    1 1
    2 0 1
    
    Expected output
    0
    -1
    1
    
  3. Example 3

    Input
    10 8
    1 4
    1 3
    1 2
    2 0 4
    2 7 2
    1 2
    1 3
    1 1
    
    Expected output
    0
    4
    7
    4
    2
    0
    7
    2