Iceberg Orders
You are working for Metagonia stock exchange. Recently traders in Metagonia had heard about Iceberg orders traded on London stock exchange and asked your employer to add such functionality as well. A stock exchange is an engine that receives orders and generates trades.
An iceberg order is a quintuple of integers (ID, T, P, V, TV). Each order has an identifier ID (unique among all orders), type T (which is equal to either BUY = 1 or SELL = 2), price P, total remaining volume V and tip volume TV . For each order, exchange additionally keeps track of its current volume CV and priority PR. There is also a global priority counter of the exchange GP. An order book of the exchange is a set of orders.
Trades that are generated by the exchange are quadruples of integers (BUY-ID, SELL-ID, P, V). Each trade has BUY-ID and SELL-ID - identifiers of matching BUY and SELL orders, trade price P, and trade volume V.
When an order is received by the exchange it is matched against orders currently on the order book. This is done as follows. Suppose an order a is received with T[a]
= SELL. Among all orders currently on the order book we look for an order b such that T[b]
= BUY and P[b]
≥ P[a]
. We select such an order b with the largest price, and if there are several - one with the smallest priority. If there is such an order b, then a trade t is generated with BUY-ID[t]
= ID[b]
and SELL-ID[t]
= ID[a]
at trade price P[t]
= P[b]
with trade volume V[t]
= min(V[a]
,CV[b]
). V[a]
, V[b]
, and CV[b]
are all decreased by trade volume. If V[b]
= 0 after this, then the order b is removed from the order book. If CV[b]
= 0 (but V[b]
> 0) then we set current volume of order b to CV[b]
= min(V[b]
, TV[b]
), set PR[b]
= GP, and increment GP. We continue these operations of selecting b and generating trades until either V[a]
= 0 or there are no more orders b on the order book which satisfy the condition. In the latter case, we add order a to the order book with CV[a]
= min(V[a]
, TV[a]
) and PR[a]
= GP, and then increment GP. When the process of matching the order a is finished with several trades between the same pair of orders a and b (and there can be lots of them!), they are all united into a single trade with the volume equal to the sum of individual trade volumes.
If T[a]
= BUY we are looking for an order b with T[b]
= SELL and P[b]
≤ P[a]
and select such an order b with the smallest price and the smallest priority among those. The rest of the matching process is as described above, with trades having BUY-ID[t]
= ID[a]
, SELL-ID[t]
= ID[b]
, P[t]
= P[b]
, and V[t]
= min(V[a]
,CV[b]
).
Initially the order book is empty. You are presented with several orders that are received by the exchange one by one. You need to print generated trades and the order book state after all orders are processed.
Hint: The priority and GP are introduced in the problem statement only for the purpose of a formal description of the algorithm. The actual implementation does not have to keep track of priority. Typically, an exchange simply keeps a priority-ordered list of orders of each type at each price in its order book.
Input
The first line contains the number of orders n (1 ≤ n ≤ 50000). Each of the following n lines represent an order. Each order is given by a space-separated quintuple ID T P V TV, where 1 ≤ ID ≤ 1000000, T = 1 for BUY and T = 2 for SELL, 1 ≤ P ≤ 100000 and 1 ≤ TV ≤ V ≤ 10^9
.
Output
For each order print all trades generated by processing this order, in ascending order of pairs (BUY-ID, SELL-ID), each trade on its own line. Each trade shall be printed as a space-separated quadruple of integers BUY-ID SELL-ID P V. It is guaranteed that the total number of trades would not exceed 100000. Print a blank line after all trades, followed by the order book. Each order that is still on the book shall be printed as a space-separated sextuple ID T P V TV CV, sorted first by P and then by PR.
Examples
Note
In the sample input the first four orders have T = BUY. Assuming that at the beginning GP at the exchange was equal to 1, after receiving these orders the order book looks in the following way when ordered according to the matching rules from the problem statement for T[b]
= BUY orders (first by the largest price, then by the smallest priority):
The fifth order (with ID[a]
= 4321) has T[a]
= SELL, P[a]
= 99 and V[a]
= 125 and is eligible for match with all of the above four orders given in the above table. First, it matches twice with the order 1111 with the highest price of 101 for a total trade volume of 30, removes it from the order book, bumps GP to 6 in the process, and decreases V[a]
to 95. Then, there are three other orders at price 100. One matching pass through them produces three trades for a total volume of 85 (volume 20 with order 42, volume 50 with order 239, removes it from the book, volume 15 with order 1234), bumps GP to 8, and decreases V[a]
to 10. The remaining orders in the book are shown below:
One last match with the order 42 produces a trade with a volume of 10 (for a total volume of 30 for matches with order 42) and the order 4321 is done (V[a]
= 0). Four corresponding total trades for order 4321 are printed in the sample output. The remaining order book is:
The sixth BUY order (with ID = 5678) is added to the order book (GP becomes 9):
The last, seventh order (with ID = 8765), can be matched only with the order 5678 due to price condition, generates a trade with volume of 30, order 5678 is removed from the order book, while order 8765 is added. Now the order book has both BUY and SELL orders: