- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 58 lines of C++ from the credited upstream file abc298_c.cpp.
- The implementation visibly relies on ordered lookup.
- 4 loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1#include <iostream>2#include <map>3 4using namespace std;5using ui = unsigned int;6 7int main() {8 ui n, q;9 cin >> n >> q;10 11 map<ui, map<ui, ui>> boxes;12 map<ui, map<ui, bool>> cards;13 14 for (ui k = 0; k < q; k++) {15 ui c = 0, i = 0, j = 0;16 cin >> c;17 18 if (c == 1) {19 cin >> i >> j;20 boxes[j][i]++;21 cards[i][j] = true;22 } else if (c == 2) {23 cin >> i;24 auto it = boxes[i].begin();25 bool f_done = false;26 while (it != boxes[i].end()) {27 for (ui t = 0; t < it->second; t++) {28 if (f_done) {29 cout << " ";30 } else {31 f_done = true;32 }33 cout << it->first;34 }35 it++;36 }37 cout << endl;38 } else if (c == 3) {39 cin >> i;40 auto it = cards[i].begin();41 bool f_done = false;42 while (it != cards[i].end()) {43 if (f_done) {44 cout << " ";45 } else {46 f_done = true;47 }48 cout << it->first;49 it++;50 }51 cout << endl;52 } else {53 54 }55 }56 57 return 0;58}