← 返回 optiver 的题目列表Single-Symbol Order Book Price Levels
类型:online_judge
Problem: Maintain Price Levels in a Single-Symbol Order Book
Implement a simplified NASDAQ market data order book for one symbol, such as AAPL.
The system receives the following callbacks:
def OnOrderInsert(id, side, price, qty)
def OnOrderModify(id, price, qty)
def OnOrderCancel(id)
It must also support this internal query API:
def GetPriceLevel(side, level_index) -> (price, total_qty)
Rules
side is either BUY or SELL.
Each order has a unique id, a side, a price, and a qty.
OnOrderInsert(id, side, price, qty): inserts a new order.
OnOrderModify(id, price, qty): modifies the price and quantity of an existing order. The side does not change.
OnOrderCancel(id): removes an existing order.
GetPriceLevel(side, level_index): returns the price and total quantity at the given price level on that side.
Price Level Definition
A price level is the aggregate quantity of all orders on the same side and at the same price.
level_index is 0-based.
For BUY, the best price is the highest price, so level 0 is the highest bid.
For SELL, the best price is the lowest price, so level 0 is the lowest ask.
If the requested level does not exist, return (-1, 0).
Input / Output Format
For testing, callbacks are represented as commands.
The first line contains an integer Q, the number of operations.
Each of the next Q lines is one of:
INSERT id side price qty
MODIFY id price qty
CANCEL id
GET side level_index
For each GET, output one line:
price total_qty
Constraints
1 <= Q <= 2 * 10^5
1 <= price <= 10^9
1 <= qty <= 10^9
id is a non-empty string.
INSERT ids are unique.
MODIFY and CANCEL ids always exist.
Example
Input:
7
INSERT o1 BUY 100 10
INSERT o2 BUY 101 5
INSERT o3 SELL 105 7
GET BUY 0
GET BUY 1
GET SELL 0
GET SELL 1
Output:
101 5
100 10
105 7
-1 0
Example
Input
7
INSERT o1 BUY 100 10
INSERT o2 BUY 101 5
INSERT o3 SELL 105 7
GET BUY 0
GET BUY 1
GET SELL 0
GET SELL 1
Output
101 5
100 10
105 7
-1 0