Technique · CP 2400+
Parallel Binary Search
When each of q queries needs its own binary search over the time of a sequence of updates, run all the searches together. In each of log T rounds, sort queries by their current mid, replay the updates once, and check every query at its mid.
0/1 solved
Recognise it
The signal
for each query, the first time/update index at which its condition becomes true
1Brute force
Binary search each query separately, replaying the updates every time: O(q T log T).
2The observation
Every query's search replays the same update prefixes on its own.
3Why it works
After every round each query's [lo, hi] still contains its answer. Within a round, mids are visited in increasing order while the updates are replayed once.
Reach for it when
- +the check needs a structure built from a prefix of the updates (DSU, BIT)
- +each query's answer is monotone in time
Not when
- ×queries are online
- ×the condition is not monotone in time
Practice
- New Roads QueriesCSESMedium
Pitfalls
Misconceptions
- !the structure must be reset between rounds
Go deeper1 combinations · resources
Combinations
Parallel Binary Search+Union-Find (Disjoint Set)
for each pair, the first time it becomes connected as edges are added
- Parallel Binary Search:
- every query binary searches the day on which its pair connects
- Union-Find (Disjoint Set):
- replay the edge additions once per round