To solve this problem, we need to determine the number of ladies at the ball who 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
-
Data Collection and Sorting:
- Collect all the beauty (B), intellect (I), and richness (R) values for each lady.
- Sort all the intellect values (I) to handle coordinate compression efficiently.
-
Coordinate Compression:
- Create a sorted list of unique intellect values (I) to map each I value to a rank for efficient range queries.
-
Segment Tree Initialization:
- Use a segment tree to maintain the maximum R value for each I rank, allowing efficient maximum range queries.
-
Processing Ladies in Order:
- Sort the ladies by their beauty (B) in descending order to ensure that when evaluating each lady, all previously processed ladies have higher or equal beauty.
- For each lady, determine if there exists a lady with higher I and R than her using the segment tree. If such a lady exists, increment the count of probable self-murderers.
- Update the segment tree with the current lady's (I, R) values to maintain current maximum R values for subsequent queries.
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
# Collect all I values for coordinate compression
all_Is = I.copy()
# Generate sorted and unique list of I values
sorted_Is = sorted(all_Is)
unique_Is = []
last = None
for i in sorted_Is:
if i != last:
unique_Is.append(i)
last = i
m = len(unique_Is)
class SegmentTree:
def __init__(self, size):
self.n = 1
while self.n < size:
self.n *= 2
self.size = self.n
self.data = [0] * (2 * self.n)
def update(self, pos, val):
# pos is 1-based in 1..m
v = (pos - 1) + self.n
if self.data[v] < val:
self.data[v] = val
v //= 2
while v >= 1:
new_val = max(self.data[2*v], self.data[2*v +1])
if self.data[v] == new_val:
break
self.data[v] = new_val
v //=2
def query(self, l, r):
# l and r are 1-based
res = 0
a = l + self.n -1
b = r + self.n -1
while a <= b:
if a % 2 ==1:
res = max(res, self.data[a])
a +=1
if b %2 ==0:
res = max(res, self.data[b])
b -=1
a //=2
b //=2
return res
st = SegmentTree(m)
# Prepare ladies as tuples ( -B, index ) for sorting
ladies = []
for i in range(N):
ladies.append( (-B[i], i) )
# Sort ladies by decreasing B ( sorted in ascending of -B )
ladies.sort()
cnt =0
for lady in ladies:
_, idx_lady = lady
I_i = I[idx_lady]
R_i = R[idx_lady]
# Find the rank of I_i in unique_Is
i_pos = bisect.bisect_left(unique_Is, I_i)
if i_pos < len(unique_Is) and unique_Is[i_pos] == I_i:
rank = i_pos +1 #1-based
else:
rank = i_pos +1
# Query for I > I_i which starts from rank+1 up to m
l = rank +1
r = m
if l > r:
current_max =0
else:
current_max = st.query(l, r)
if current_max > R_i:
cnt +=1
# Update the segment tree with current R_i at position rank
st.update( rank, R_i )
print(cnt)
if __name__ == '__main__':
main()
Explanation
- Coordinate Compression: We first gather all the intellect values (I), sort them, and create a list of unique values. This allows us to map each intellect value to a compressed rank, reducing the range of values we need to handle.
- Segment Tree: The segment tree is used to efficiently manage and query the maximum richness (R) value for ranges of intellect ranks. This allows us to quickly check if any previously processed lady has a higher intellect and richness than the current lady.
- Processing Order: By processing ladies in order of descending beauty, we ensure that for each lady, we only need to check previous entries in the segment tree for dominance. This allows efficient updates and queries on the segment tree to determine if a lady is a probable self-murderer.
This approach ensures an efficient solution with a time complexity of O(N log N), making it suitable for large input sizes up to 500,000.