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/Backtracking & Recursion/Divide and Conquer (merge-sort counting)

Technique · CP 2400+

CDQ Divide and Conquer

Split operations by time (or by one coordinate) and recurse on both halves. At each split, count how the left half's updates affect the right half's queries with a sweep plus a BIT. This removes one dimension at the cost of an extra log factor.

Learn firstDivide and Conquer (merge-sort counting)Segment Tree / Fenwick (BIT)Offline Query Processing
0%
mastery
0/3 solved
Practise nextIntersection of Permutations
IdeaPracticePitfalls

Recognise it

The signal

count points dominated in three coordinates

  1. 1Brute force

    O(n2) pair checks, or a 2D BIT of size n2.

  2. 2The observation

    One more dimension than sorting plus a BIT can handle.

  3. 3Why it works

    Each (update, query) pair with the update earlier is counted exactly once, at the recursion node where the two are split apart.

Reach for it when

  • +updates interleaved with 2D queries, all offline
  • +merge-sort counting with one extra coordinate

Not when

  • ×online queries
  • ×only two dimensions (sort + BIT is enough)

Practice

Next

Intersection of Permutations

0/3 solved
  • Intersection of PermutationsCodeforces2400
  • Little Artem and Time MachineCodeforces2000
  • Distinct Values Queries IICSESHard

Pitfalls

Misconceptions

  • !clear the BIT by undoing the inserted updates, not by reallocating it
Go deeper1 combinations · resources

Combinations

  • CDQ Divide and Conquer+Segment Tree / Fenwick (BIT)

    2D point updates/queries (or swaps in permutations) all offline, n ~ 2e5

    CDQ Divide and Conquer:
    split operations by time; left updates feed right queries
    Segment Tree / Fenwick (BIT):
    a BIT over one coordinate during the merge sweep

    Intersection of PermutationsLittle Artem and Time Machine

Resources

  • Complete Search with RecursionUSACO Guide · course
  • Recursion I explore cardLeetCode · course
  • Recursion cheatsheetTech Interview Handbook · article
  • General approach to backtracking questionsLeetCode discuss · article
  • Competitive Programmer's HandbookAntti Laaksonen · book
TodayReviewPatternsProblems