#include #include #include #include using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; // Sr, Sn は (r, s) の pair を保持する set。 // ※ set は pair の first, second の順で自動的にソートされる set> Sr, Sn; // 各文字列 s に対して整数 r を保持するマップ unordered_map D; for (int i = 0; i < q; i++){ string type; cin >> type; if (type == "1") { // クエリ1: "1 s r" string s; int r; cin >> s >> r; D[s] = r; Sn.insert({r, s}); } else if (type == "2") { // クエリ2: "2 x" で n を x 減少 int x; cin >> x; n -= x; } else { // クエリ "3" // クエリ3: "3 s x" で n を x 増加し、s に対応する (r, s) を Sr に移動 string s; int x; cin >> s >> x; n += x; int r = D[s]; Sr.insert({r, s}); Sn.erase({r, s}); } // Sn と Sr の合計サイズが n を超える間、先頭の要素を出力・削除 while (Sn.size() + Sr.size() > static_cast(n)) { if (!Sn.empty()) { auto it = Sn.begin(); cout << it->second << "\n"; Sn.erase(it); } else { auto it = Sr.begin(); cout << it->second << "\n"; Sr.erase(it); } } } return 0; }