#include #include #include #include #include extern "C" int __lsan_is_turned_off() { return 1; } #define rep(i, n) for (int i = 0; i < (n); i++) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() using namespace std; using ll = long long; using vi = vector; struct order { string op, type; ll p, a; }; struct FT { vi a; FT(int n) : a(n) {} ll sum(ll r) { return r ? a[r - 1] + sum(r & r - 1) : 0; } void add(int i, ll x) { if (i < sz(a)) a[i] += x, add(i | i + 1, x); } }; int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); int n; cin >> n; vector orders(n); for (auto &[op, type, p, a] : orders) cin >> op >> type >> p >> a; vi q(n); transform(all(orders), begin(q), [](auto &o) { return o.p; }); sort(all(q)); q.erase(unique(all(q)), end(q)); for (auto &o : orders) o.p = lower_bound(all(q), o.p) - begin(q); ll P = sz(q); FT supply(P), demand(P); priority_queue> sellers, buyers; ll ans = 0; ostringstream ss; rep(t, n) { auto [op, type, p, a] = orders[t]; if (type == "fok") { if (op == "buy" && supply.sum(p+1) < a) continue; if (op == "sell" && demand.sum(P-p) < a) continue; } if (op == "buy") buyers.emplace(p, -t, a), demand.add(P-1-p, a); else if (op == "sell") sellers.emplace(-p, -t, a), supply.add(p, a); while (!sellers.empty() && !buyers.empty()) { auto [sp, st, sa] = sellers.top(); auto [bp, bt, ba] = buyers.top(); if (-sp > bp) break; sellers.pop(), buyers.pop(); ll c = min(sa, ba); supply.add(-sp, -c), demand.add(P-1-bp, -c); ++ans; ss << -st + 1 << " " << -bt + 1 << " " << c << "\n"; sa -= c, ba -= c; if (sa) sellers.emplace(sp, st, sa); if (ba) buyers.emplace(bp, bt, ba); } } cout << ans << "\n"; cout << ss.str(); return 0; }