eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

Iceberg Orders

Iceberg Orders

Ліміт часу 1 секунда
Ліміт використання пам'яті 128 MiB

Вы работаете на бирже Метагония. Недавно торговцы в Метагонии услышали о заявках айсберга, торгуемых на Лондонской фондовой бирже, и попросили вашего работодателя добавить такую функциональность. Фондовая биржа - это финансовый институт, который получает заказы и генерирует сделки.

Заявка айсберга - это пятека целых чисел (ID, T, P, V, TV). Каждая заявка имеет идентификатор ID (уникальный для всех заявок), тип T (равный либо BUY = 1 или SELL = 2), цену P, общий оставшийся объем V и 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.

Вхідні дані

The first line contains the number of orders n (1n50000). Each of the following n lines represent an order. Each order is given by a space-separated quintuple ID T P V TV, where 1ID1000000, T = 1 for BUY and T = 2 for SELL, 1P100000 and 1TVV10^9.

Вихідні дані

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.

Приклад

Вхідні дані #1
7
42 1 100 200 20
239 1 100 50 50
1111 1 101 30 15
1234 1 100 300 15
4321 2 99 125 25
5678 1 101 30 30
8765 2 101 100 20
Вихідні дані #1
42 4321 100 30
239 4321 100 50
1111 4321 101 30
1234 4321 100 15
5678 8765 101 30

42 1 100 170 20 10
1234 1 100 285 15 15
8765 2 101 70 20 20

Примітка

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):

prb7569.gif

The fifth order (with IDa = 4321) has Ta = SELL, Pa = 99, and Va = 125 and is eligible for match withall of the above four orders given in the above table. First, it matches twice with the order 1111 withthe highest price of 101 for a total trade volume of 30, removes it from the order book, bumps GP to 6in the process, and decreases Va to 95. Then, there are three other orders at price 100. One matchingpass through them produces three trades for a total volume of 85 (volume 20 with order 42, volume 50with order 239, removes it from the book, volume 15 with order 1234), bumps GP to 8, and decreasesVa to 10. The remaining orders in the book are shown below:

prb7569_1.gif

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:

prb7569_2.gif

The sixth BUY order (with ID = 5678) is added to the order book (GP becomes 9):

prb7569_3.gif

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:

prb7569_4.gif
Джерело 2015 ACM NEERC, Semifinals, December 6, Problem I