Monitoring~45 min2s · 256 MBtarget O(n^(5/3)) time, O(n + m) spacewindow-counts
An operations dashboard has a row of slots, and each slot shows the label id of one service. Operators re-label slots during an incident, and analysts ask how many different services appear in a range of slots.
Edits and questions arrive in one list, and an answer must reflect the edits that came before the question. The whole list is known in advance. A dashboard can have 15,000 slots and 15,000 operations.
Write distinct_with_updates(labels, operations).
labels[i] is the label id shown in slot i at the start.operations[j] = [0, p, v] sets slot p to the label v. Later operations see the change.operations[j] = [1, l, r] asks how many different labels the slots l to r show now, ends included. Always 0 ≤ l ≤ r < len(labels).Input: labels = [1, 2, 1, 3, 2], operations = [[1, 0, 4], [0, 1, 3], [1, 0, 2], [0, 4, 1], [1, 2, 4], [0, 0, 2], [1, 0, 4]]
Output: [3, 2, 2, 3]There are 4 questions and 3 edits. A question remembers how many edits came before it. The questions are answered in block order, with a window and a clock that move from one question to the next:
| Question (range, edits before) | Moves | Window after | Different labels |
|---|---|---|---|
| slots 0 to 2, after 1 edit | apply the edit at slot 1 (outside the window), then add 1, 3, 1 | slots 0 to 2 show 1, 3, 1 | 2 |
| slots 0 to 4, after 0 edits | undo the edit at slot 1, then add 3, 2 | slots 0 to 4 show 1, 2, 1, 3, 2 | 3 |
| slots 2 to 4, after 2 edits | apply the edits at slots 1 and 4 (inside the window), then drop 1, 3 | slots 2 to 4 show 1, 3, 1 | 2 |
| slots 0 to 4, after 3 edits | apply the edit at slot 0, then add 3, 2 | slots 0 to 4 show 2, 3, 1, 3, 1 | 3 |
The answers in the order of the questions are 3, 2, 2, 3.
Input: labels = [5, 5], operations = [[1, 0, 1], [0, 1, 6], [1, 0, 1]]
Output: [1, 2]The edit changes slot 1 from 5 to 6, so the second question sees two labels.
Input: labels = [4], operations = [[1, 0, 0]]
Output: [1]One slot, one question, no edits.
1 ≤ len(labels) ≤ 15,000, and 0 ≤ labels[i] ≤ 1,000,0001 ≤ len(operations) ≤ 15,000, and at least one operation is a question[0, p, v] has 0 ≤ p < len(labels) and 0 ≤ v ≤ 1,000,000Counting each question from scratch costs up to 15,000 steps, so thousands of questions cost tens of millions of steps. That is slow, but the real cost is that the edits break the usual offline trick of one fixed array. The edits must be treated as a third direction to move in.