Given a series of pairs of integers (A_1, B_1), (A_2, B_2), ..., (A_N, B_N)
and an initially empty set S
, add these pairs to a set S
one at a time, and for each pair (A_i, B_i)
, do the following two operations:
1.
- Check if there exists any pair
(A_j, B_j)
already in the set such thatA_j >= A_i
andB_j >= A_i
, if true, don't add(A_i, B_i)
toS
- Otherwise, add
(A_i, B_i)
to the set, then delete any previously existing pairs(A_j, B_j)
such thatA_j <= A_i and B_j <= B_j
- Report the number of pairs
S
contains
The above is describing a brute-force implementation of the problem by iterating through the entire set and doing the comparison every time we are about to add a new pair to the set S
.
Maintaining the same property of the set S
(i.e., S
does not contain pairs such that A_i <= B_i
and A_j <= B_j
), I want to design a data structure that can do type 1 operation roughly in O(logN)
time (or O(logN * logN)
, I'm not sure) and type 2 in constant time. I tried using a balanced binary search tree with some augmentation but got stuck on the case when a bunch of existing pairs needs to be deleted when a much "large" pair gets added to the set, because that might take rebuilding the entire tree. I looked up for range tree and dynamic segment tree which seem promising but I couldn't figure it out myself.