Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Binary Search/Binary Search on the Answer

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.

Learn firstBinary Search on the AnswerOffline Query ProcessingUnion-Find (Disjoint Set)Segment Tree / Fenwick (BIT)
0%
mastery
0/1 solved
Practise nextNew Roads Queries
IdeaPracticePitfalls

Recognise it

The signal

for each query, the first time/update index at which its condition becomes true

  1. 1Brute force

    Binary search each query separately, replaying the updates every time: O(q T log T).

  2. 2The observation

    Every query's search replays the same update prefixes on its own.

  3. 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

Next

New Roads Queries

0/1 solved
  • 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

    New Roads Queries

Resources

  • Binary searchcp-algorithms · article
  • Binary SearchUSACO Guide · course
  • ITMO Binary Search courseCodeforces EDU · course
  • Binary Search explore cardLeetCode · course
  • Sorting and searching cheatsheetTech Interview Handbook · article
TodayReviewPatternsProblems