Keep counts, not flags
transcript
A window holds some terms. A term may appear twice. One copy leaves.
The terms are one, one, two. A flag clears when one copy leaves. But the other copy stays. A count says two terms, which is right.
When an element enters, its count goes up. When it leaves, its count goes down. The distinct total changes only at zero and one.
Each step moves one pointer by one. The total is 2,381,624 steps on our log. That is fast enough for the limits.
Keep the brute force as a reference. Try a thousand small random logs. A wrong window fails fast.
Edits add time as a third coordinate. Sort by two blocks, then by time. A third pointer applies or undoes the edits. The same counts still work, with one more step. Blocks grow to n to the power of two thirds.
A flag forgets the second copy. A count remembers it. Full breakdown in the lecture.