To solve this problem, we need to determine how many ladies at the ball are probable self-murderers. A lady is considered a probable self-murderer if there exists another lady who is more beautiful, smarter, and richer than her.
Approach
- Sort Ladies by Beauty: First, we sort all ladies in descending order of their beauty. This allows us to process each lady in order such that any previous lady in the list has equal or higher beauty.
- Coordinate Compression: Since the values for intellect (I) can be very large (up to (10^9)), we compress these values into a smaller range. This helps in efficiently querying using a segment tree.
- Segment Tree for Maximum Queries: Using a segment tree, we maintain the maximum richness (R) values as we process each lady. For each lady, we check if there exists a lady with higher intellect and higher richness by querying the segment tree for the maximum richness value in the range of intellect values greater than the current lady's intellect.
- Update and Query: For each lady processed in order of decreasing beauty, we update the segment tree with her intellect and richness values, and check if a more dominant lady exists by querying the tree.
Solution Code
import bisect
def main():
import sys
input = sys.stdin.read().split()
idx = 0
N = int(input[idx])
idx += 1
B = list(map(int, input[idx:idx+N]))
idx += N
I = list(map(int, input[idx:idx+N]))
idx += N
R = list(map(int, input[idx:idx+N]))
idx += N
# Create list of tuples (B, I, R)
ladies = []
for i in range(N):
ladies.append( (B[i], I[i], R[i]) )
# Sort in descending order of B
ladies.sort(key=lambda x: -x[0])
# Collect all I values for coordinate compression
all_Is = [lady[1] for lady in ladies]
# Generate unique_Is sorted list
sorted_Is = sorted(all_Is)
unique_Is = []
prev = None
for x in sorted_Is:
if x != prev:
unique_Is.append(x)
prev = x
m = len(unique_Is)
class SegmentTree:
def __init__(self, size):
self.n = 1
while self.n < size:
self.n <<= 1
self.size_st = self.n
self.tree = [ -float('inf') ] * (2 * self.n)
def update(self, idx, val):
# idx is 0-based
pos = idx + self.n
if self.tree[pos] < val:
self.tree[pos] = val
pos >>= 1
while pos >= 1:
new_val = max(self.tree[2*pos], self.tree[2*pos+1])
if self.tree[pos] == new_val:
break
self.tree[pos] = new_val
pos >>= 1
def query_range(self, l, r):
# l and r are 0-based, inclusive
res = -float('inf')
l += self.n
r += self.n
while l <= r:
if l % 2 == 1:
res = max(res, self.tree[l])
l += 1
if r % 2 == 0:
res = max(res, self.tree[r])
r -= 1
l >>= 1
r >>= 1
return res
if m == 0:
print(0)
return
st = SegmentTree(m)
count = 0
for lady in ladies:
current_B, current_I, current_R = lady
# Find the compressed index for current_I in unique_Is
comp = bisect.bisect_left(unique_Is, current_I)
start_query = comp + 1
end_query = m - 1
if start_query <= end_query:
max_r = st.query_range(start_query, end_query)
else:
max_r = -float('inf')
if max_r > current_R:
count += 1
# Update the segment tree with current_I's compressed index and current_R
st.update(comp, current_R)
print(count)
if __name__ == "__main__":
main()
Explanation
- Sorting by Beauty: The ladies are sorted in descending order of beauty to ensure that when we process each lady, all previously processed ladies have higher or equal beauty.
- Coordinate Compression: The intellect values are compressed into a smaller range to efficiently use a segment tree for queries.
- Segment Tree Operations: The segment tree is used to keep track of the maximum richness encountered for each intellect value. For each lady, we check if there is a higher intellect and richer lady encountered before her using a range maximum query.
- Efficient Updates and Queries: The segment tree allows both updates and range maximum queries in logarithmic time, making the solution efficient enough to handle the upper constraint of (N = 500,000).
This approach ensures that we efficiently determine the number of probable self-murderers using sorting and a segment tree to handle range queries and updates in logarithmic time.