Monitoring~35 min2s · 256 MBtarget O((n + q) log n) time, O(n) spacesegment-tree
You work on a monitoring dashboard. A service runs on up to 100,000 replicas, listed in a fixed order. Each replica has its latest response time in milliseconds, and a replica can be measured again at any time.
A router sends traffic evenly to the fastest replicas of a group. Your team lead asks: "In a group of neighbouring replicas, how many share the lowest response time right now?" The dashboard receives up to 100,000 measurements and questions.
Write fastest_ties(latencies, ops).
latencies[i] is the response time of replica i, in milliseconds.[kind, a, b].[0, a, b] is a new measurement: replica a now has the response time b.[1, a, b] is a question about the replicas a to b, both included. It asks how many of them have the lowest response time in that group.Input: latencies = [5, 3, 8, 3], ops = [[1, 0, 3], [0, 1, 6], [1, 0, 3], [1, 2, 3]]
Output: [2, 1, 1]| Operation | Response times in the group | Lowest | How many |
|---|---|---|---|
[1, 0, 3] |
5, 3, 8, 3 | 3 | 2 |
[0, 1, 6] |
replica 1 becomes 6 | ||
[1, 0, 3] |
5, 6, 8, 3 | 3 | 1 |
[1, 2, 3] |
8, 3 | 3 | 1 |
Input: latencies = [7], ops = [[1, 0, 0]]
Output: [1]A single replica is always the fastest of its group.
Input: latencies = [4, 4, 4, 4], ops = [[1, 0, 3], [1, 1, 2]]
Output: [4, 2]Every replica has the same response time, so every replica in the group is tied for the lowest.
1 ≤ len(latencies) ≤ 100,0001 ≤ latencies[i] ≤ 100,0001 ≤ len(ops) ≤ 100,000kind is 0 or 1. For a measurement, 0 ≤ a < len(latencies) and 1 ≤ b ≤ 100,000. For a question, 0 ≤ a ≤ b < len(latencies).Reading a whole group for each question costs up to 100,000 steps, and 100,000 questions cost 10 billion steps. That is far too slow for two seconds. You need about log n steps for a question and for a measurement, which is about 17. A minimum cannot be undone like a sum, so each block must keep what it needs to answer on its own.