F - Box in Box Editorial by en_translator
We rotate each box to make \(h_i \leq w_i \leq d_i\) and determine if any \((i,j)\) satisfies \(h_i \lt h_j, w_i \lt w_j\) and \(d_i \lt d_j\). The answer remains the same because a box has greater width, height, and width than another, swapping them like mentioned above does not change any of the orderings. Since we are only interested in the ordering, compress the coordinates to make all values between \(1\) and \(N\).
First, we assume that \(h_i\) are pairwise distinct. Consider sorting the boxes in ascending order of \(h_i\) and process them in ascending order of \(i\); then, when processing the \(j\)-th box, \(h_j \gt h_i\) for all boxes \(j\) processed so far. This property can be used to determine the existence of sought \((i,j)\) as follows:
- Prepare an length-\(N\) segment tree that supports element-wise update and minimum value retrieval for a segment.
- For each \(i\) in ascending order,
- if the minimum value within the segment with indices less than \(w_i\) on the segment tree is less than \(d_j\), let the answer be
Yes
. - if the element on the segment tree indexed by \(w_i\) is greater than \(d_i\), replace it with \(d_i\).
- if the minimum value within the segment with indices less than \(w_i\) on the segment tree is less than \(d_j\), let the answer be
In this problem, \(h_i\) may not be distinct, so it may make a mistake when \(h_i=h_j, w_i\lt w_j, d_i \lt d_j\). Instead, sort the tuples \((h_i,w_i,d_i)\) in descending order of \(h_i\), with ties broken in descending order of \(w_i\) to avoid the mistake. This way, it can be solved in a same way as when \(h_i\) are virtually distinct.
posted:
last update: