N ladies attend the ball in the King's palace. Every lady can be described with three…
- User
N ladies attend the ball in the King's palace. Every lady can be described with three values: beauty, intellect and richness. King's Master of Ceremonies knows that ladies are very special creatures. If some lady understands that there is other lady at the ball which is more beautiful, smarter and more rich, she can jump out of the window. He knows values of all ladies and wants to find out how many probable self-murderers will be on the ball. Lets denote beauty of the i-th lady by Bi, her intellect by Ii and her richness by Ri. Then i-th lady is a probable self-murderer if there is some j-th lady that Bi < Bj, Ii < Ij, Ri < Rj. Find the number of probable self-murderers. Input
The first line contains one integer N (1 ≤ N ≤ 500000). The second line contains N integer numbers Bi, separated by single spaces. The third and the fourth lines contain sequences Ii and Ri in the same format. It is guaranteed that 0 ≤ Bi, Ii, Ri ≤ 109.
Output
Output the answer to the problem.
Examples
Input
3 1 4 2 4 3 2 2 5 3
Output
1
Assistant
import bisect class SegmentTree: def __init__(self, size): self.n = size self.size = 1 while self.size < self.n: self.size <<= 1 self.data = [0] * (2 * self.size) def update(self, pos, value): pos += self.size # convert to leaf node index if self.data[pos] < value: self.data[pos] = value pos >>= 1 while pos >= 1: new_val = max(self.data[2*pos], self.data[2*pos+1]) if self.data[pos] == new_val: break self.data[pos] = new_val pos >>= 1 def query_max(self, l, r): res = -1 l += self.size r += self.size while l <= r: if l % 2 == 1: res = max(res, self.data[l]) l += 1 if r % 2 == 0: res = max(res, self.data[r]) r -= 1 l >>= 1 r >>= 1 return res def main(): import sys input = sys.stdin.read().split() ptr = 0 N = int(input[ptr]) ptr +=1 B = list(map(int, input[ptr:ptr+N])) ptr +=N I = list(map(int, input[ptr:ptr+N])) ptr +=N R = list(map(int, input[ptr:ptr+N])) ptr +=N ladies = list(zip(B, I, R)) ladies.sort(key=lambda x: (-x[0], x[1], x[2])) batches = [] current_B = None current_batch = [] for lady in ladies: B_i, I_i, R_i = lady if B_i != current_B: if current_batch: batches.append(current_batch) current_batch = [lady] current_B = B_i else: current_batch.append(lady) if current_batch: batches.append(current_batch) Is = [lady[1] for lady in ladies] sorted_unique_Is = sorted(list(set(Is))) m = len(sorted_unique_Is) if m ==0: print(0) return st = SegmentTree(m) count =0 for batch in batches: for lady in batch: B_i, I_i, R_i = lady compressed_i = bisect.bisect_left(sorted_unique_Is, I_i) if compressed_i +1 <= m-1: max_r = st.query_max(compressed_i+1, m-1) else: max_r = -1 if max_r > R_i: count +=1 for lady in batch: B_i, I_i, R_i = lady compressed_i = bisect.bisect_left(sorted_unique_Is, I_i) st.update(compressed_i, R_i) print(count) if __name__ == "__main__": main()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.