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.
0/3 solved
Recognise it
The signal
count points dominated in three coordinates
1Brute force
O(n2) pair checks, or a 2D BIT of size n2.
2The observation
One more dimension than sorting plus a BIT can handle.
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
- 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