This page is still under construction.

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

Tram

Time limit1sMemory limit256 MB

Summary
Each boarding passenger takes the empty seat farthest from the nearest seated passenger in a 2-column tram, with ties broken by row then column.
Level

Medium7 of 10

Topics
Heap, Sorting, Math, Simulation
Solved
No attempts yet

Problem

To ease the traffic jams in Seoul, mayor Kim Sang-geun brought in a tram. The seats of the tram form a grid of NN rows and 2 columns. The rows are numbered 1 through NN, and the columns are numbered 1 and 2.

The distance between seats (RA,CA)(R_A, C_A) and (RB,CB)(R_B, C_B) is the distance between the centers of the two cells, (RA−RB)2+(CA−CB)2\sqrt{(R_A-R_B)^2+(C_A-C_B)^2}.

Most people sit as far away from the other passengers as they can. When a passenger steps into the tram, they measure for every empty seat the distance from that seat to the nearest seated passenger, and they take the seat where that distance is largest. When several seats tie, they take the one with the smaller row number, and when the row number ties as well, the one with the smaller column number. A passenger who has sat down stays in that seat until leaving the tram. A passenger who boards an empty tram sits in row 1, column 1.

You are given the log of passengers boarding and leaving. Write a program that reports the seat each passenger takes.

The log has MM lines, numbered 1 through MM in the order given. There are two kinds of lines. 'E' means a passenger boarded, and 'L' means a passenger left. A line that records a departure also tells on which line that passenger boarded.

A boarding line appears only when at least one seat is empty.

Input

The first line contains the number of rows NN and the number of log lines MM. (1≤N≤150,0001 \le N \le 150{,}000, 1≤M≤30,0001 \le M \le 30{,}000)

Each of the next MM lines records a boarding or a departure. When line KK is 'L', it comes with PKP_K (1≤PK≤K1 \le P_K \le K), meaning that the passenger who boarded on line PKP_K leaves. Line PKP_K is always 'E', and no passenger leaves twice.

Output

Every time an 'E' line is given, print on its own line the row number and the column number of the seat that passenger takes, separated by a single space.

Examples3

  1. Example 1

    Input
    3 7
    E
    E
    E
    L 2
    E
    L 1
    E
    
    Expected output
    1 1
    3 2
    1 2
    3 1
    1 1
    
  2. Example 2

    Input
    13 9
    E
    E
    E
    E
    E
    E
    E
    E
    E
    
    Expected output
    1 1
    13 2
    7 1
    4 2
    10 1
    2 2
    3 1
    5 1
    6 2
    
  3. Example 3

    Input
    10 9
    E
    E
    E
    E
    L 3
    E
    E
    L 6
    E
    
    Expected output
    1 1
    10 2
    5 2
    7 1
    4 2
    2 2
    4 1