#include using namespace std; struct Order { bool buy; int size, price; }; const int MAXN = 10010; int n, orderIndice[MAXN], canceled[MAXN]; vector orders; template struct OrderQueue { typedef set _IntSet; _IntSet eles; void erase(int x) { eles.erase(eles.find(x)); } bool empty() const { return eles.empty(); } int top() const { return *eles.begin(); } int pop() { int ans = *eles.begin(); eles.erase(eles.begin()); return ans; } void push(int oi) { eles.insert(oi); } int size() const { return eles.size(); } void clear() { eles.clear(); } int topPrice() { return orders[top()].price; } int topSize() { int tp = topPrice(), ans = 0; for(auto i : eles){ const Order& o = orders[i]; if(o.price == tp) ans += o.size; else break; } return ans; } }; struct BuyOrderCompare { bool operator() (int i, int j) { const Order &oi = orders[i], &oj = orders[j]; return oi.price > oj.price || (oi.price == oj.price && i < j); } }; struct SellOrderCompare { bool operator() (int i, int j) { const Order &oi = orders[i], &oj = orders[j]; return oi.price < oj.price || (oi.price == oj.price && i < j); } }; OrderQueue buyQueue; OrderQueue sellQueue; void cancel(int ci) { int oi = orderIndice[ci]; if(canceled[oi]) return; const Order& o = orders[oi]; if(o.buy) buyQueue.erase(oi); else sellQueue.erase(oi); canceled[oi] = 1; } void trade(int oi) { Order& o = orders[oi]; if(o.buy) { if(sellQueue.empty() || o.price < sellQueue.topPrice()) { buyQueue.push(oi); return; } int askPrice; while(!sellQueue.empty() && o.size > 0 && o.price >= (askPrice = sellQueue.topPrice())) { int toi = sellQueue.top(); Order& to = orders[toi]; int tradeSize = min(o.size, to.size); cout<<"TRADE "< 0) buyQueue.push(oi); else canceled[oi] = 1; return; } if(buyQueue.empty() || o.price > buyQueue.topPrice()) { sellQueue.push(oi); return; } int bidPrice; while(!buyQueue.empty() && o.size > 0 && o.price <= (bidPrice = buyQueue.topPrice())) { int toi = buyQueue.top(); Order& to = orders[toi]; int tradeSize = min(o.size, to.size); cout<<"TRADE "< 0) sellQueue.push(oi); else canceled[oi] = 1; } void quote() { int bidSize = 0, bidPrice = 0, askSize = 0, askPrice = 99999; if(!buyQueue.empty()) { bidSize = buyQueue.topSize(); bidPrice = buyQueue.topPrice(); } if(!sellQueue.empty()) { askSize = sellQueue.topSize(); askPrice = sellQueue.topPrice(); } cout<<"QUOTE "<>n) { if(first) first = false; else cout<>cmd; if(cmd == "CANCEL") { int x; cin>>x; cancel(x-1); quote(); continue; } Order o; cin>>o.size>>o.price; o.buy = (cmd == "BUY"); orderIndice[i] = orders.size(); orders.push_back(o); trade(orderIndice[i]); quote(); } } return 0; }