This page is still under construction.

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

Ants in a Corridor

Time limit3sMemory limit512 MB

Summary
Ants join a corridor over time and bounce off each other and both walls, and each query asks for the position of one numbered ant.
Level

Hard8 of 10

Topics
Math, Sorting
Solved
No attempts yet

Problem

A corridor is a segment of length LL. The corridor carries coordinates: its left endpoint is 00, its right endpoint is LL, and the point that divides the corridor internally in the ratio x:yx : y has coordinate xLx+y\frac{xL}{x+y}.

Whenever Gyeonggeun is bored, he puts one more ant on the corridor. Every ant on this corridor moves either left or right and covers distance 11 per second. The corridor is only wide enough for one ant. When two ants moving in opposite directions meet at a point, both reverse direction at once and keep the same speed. An ant that reaches an endpoint of the corridor also reverses direction at once and keeps the same speed. Treat an ant as a point with no size. Two ants collide only when their coordinates are exactly equal, and an ant turns around only when its coordinate is exactly 00 or exactly LL.

At time 00 seconds the corridor holds no ants. Write a program that processes QQ operations of the following two kinds.

  1. At second tt, put an ant moving right or left at coordinate xx. If this ant is the ii-th one placed, it gets number ii.
  2. Print the coordinate of ant number ii at second tt.

Input

The first line contains two positive integers LL and QQ (1≤L≤1091 \le L \le 10^9, 1≤Q≤2×1051 \le Q \le 2 \times 10^5).

Each of the next QQ lines holds one operation. Every line starts with an integer tt (0≤t≤10180 \le t \le 10^{18}), the second at which the operation happens, and a positive integer pp (1≤p≤21 \le p \le 2), the kind of the operation. The rest of the line is as follows.

  • If p=1p = 1, an integer xx (0<x<L0 < x < L) and an integer dd (d∈{−1,1}d \in \{-1, 1\}) follow. At second tt an ant is placed at coordinate xx, moving right when d=1d = 1 and left when d=−1d = -1. The input never places an ant on a coordinate that another ant occupies at that second.
  • If p=2p = 2, the number ii of the ant follows. It is at least 11 and at most the number of ants placed so far. Print the coordinate of ant number ii at second tt.

The operations come in increasing order of tt, and no two operations share the same tt.

Output

For each operation with p=2p = 2, print the coordinate of that ant on its own line. Under these constraints the coordinate at a query time is always an integer, so print it as an integer.

Examples3

  1. Example 1

    Input
    5 5
    0 1 2 1
    2 1 3 -1
    6 2 2
    7 2 1
    8 2 2
    
    Expected output
    1
    2
    0
    
  2. Example 2

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

    Input
    6 6
    0 1 2 1
    1 1 5 -1
    4 2 1
    5 2 2
    9 2 1
    10 2 2
    
    Expected output
    2
    5
    1
    4