Make the state tell the truth
Maintain active frequencies, reset test cases, and debug the representation first.
Optional extension: keep only positive counts
When a frequency map represents an active bag, store a key exactly while its count is positive. Then size() equals the number of currently distinct values. Adding uses ++freq[x]; removing uses find so an absent removal does not create a record.
After decrementing a present count, erase its entry if the count becomes zero. A zero-valued record still contributes to map size. This is a representation rule for an active bag; a real record with a score or balance of zero may need to remain in other tasks.
// Complete C++23 program. Input: q commands.
// 1 x: add one. 2 x: remove one if present. 3: print distinct count.
#include <iostream>
#include <map>
int main() {
int q;
std::cin >> q;
std::map<int, int> frequency;
while (q--) {
int type;
std::cin >> type;
if (type == 3) {
std::cout << frequency.size() << '\n';
continue;
}
int x;
std::cin >> x;
if (type == 1) ++frequency[x];
else {
auto it = frequency.find(x);
if (it != frequency.end()) {
--it->second;
if (it->second == 0) frequency.erase(it);
}
}
}
}Each test case owns its state
Create local containers inside solve(), or explicitly reset every piece of state before the next case. Cases [1, 2] and [2, 3] each have two distinct keys; a shared uncleared set incorrectly reports three in the second case.
Add work across cases. With total input length N, an upper bound for set construction is O(T + N log(N + 1)); peak storage follows the largest live case. Passing a map by value to every query can add O(qd) copying independently of its fast lookup.
Use a boundary checklist with a reason
Test empty collections where allowed, one item, all equal, all distinct, absent keys below/between/above stored keys, and deleting the final item. For maps, include mapped zero and repeated assignments. For ranks, test negative, first, last, and exactly one-past-last requests.
If too many copies disappear, inspect the multiset erase overload. If size grows on queries, inspect subscript. If an output order changes, inspect hash traversal. If samples pass but large tests stall, count duplicate runs, iterator walks, container copies, and output volume. Explain the invariant before changing the container.
Word frequency map
Split on whitespace. Keys are case-sensitive; punctuation stays attached. Displayed in sorted key order like std::map<string, int>.
code3learn2solve16 words · 3 distinct keys
Guided simulation · Change the inputs to test your understanding.
Add 7, add 7, remove 7, remove 7 in an active frequency map. What should the distinct counts be?
Turn the idea into code
A command parser always reads type and x, but command 3 has no x. Explain the bug and how the complete program avoids it.
Give me a hint
Input is a stream; consuming an extra integer steals part of the next command.
Show the explained solution
Reading x after command 3 consumes the next command type and misaligns all subsequent input. Handle type 3 immediately and continue; only commands 1 and 2 read an x. The operation contract controls both the container update and input parsing.
Your notes
Mark complete when you can explain it yourself.