We need to count number of ladies for which there exists another lady with strictly greater beauty, intellect, and richness. Input N up to 500k, coordinates up to 1e9. So we need an efficient algorithm. This is essentially a 3D dominance query: count points that are dominated (strictly less) by at least one other point.
We can count the number of points that are NOT self-murderers, i.e., points that are maximal in the sense that no other point strictly dominates them. Then answer = N - count of non-self-murderers (points that are not dominated by any other). Actually careful: "probable self-murderer" if exists j such that Bi < Bj, Ii < Ij, Ri < Rj. So these are points that are dominated by at least one other point. So we need count of dominated points.
Alternatively, we can compute for each point whether there exists a point with all three coordinates greater. This is classic 3D dominance query: given points, for each point i, check if any point has x > xi, y > yi, z > zi. Equivalent to transform coordinates: maybe sort by one dimension descending and then use a data structure for 2D dominance (max over y, z?). Let's think.
We have N up to 500k, so O(N log^2 N) might be okay but must be efficient. Possibly O(N log N) with clever use of BIT or segment tree and coordinate compression.
Standard approach: sort points decreasing by x (beauty). Then as we iterate, we maintain a data structure over y and z for points with larger x (already processed). For current point (x_i, y_i, z_i), we need to check if there exists any point with y > y_i and z > z_i among those seen. Since we sorted descending by x, all points processed have x >= current x (actually since we need strictly greater, we have to be careful about equal coordinates: if equal, they do not count as strictly greater. So we need to treat points with same x differently). Common technique: sort by x descending, and when processing, handle points with same x together: for points with same x, we first query for each to see if any previously processed point (with strictly larger x) dominates; then after queries, we insert all of them into the data structure. That ensures strictness in x. Good.
Now, for each point, after ensuring x strictness, we need to query if there exists a point with y > y_i and z > z_i in the data structure. This is a 2D dominance query: existence of point with y > y_i and z > z_i among points we have. How to answer quickly? We can use a segment tree over y coordinate (compressed) where each node stores the maximum z value among points with that y (or maybe better: store the maximum z for each y, and then for query y > y_i, we need to check if any point with y in (y_i, maxY] has z > z_i). That can be done with a BIT or segment tree that keeps maximum. However query requires y > y_i, so we need to query over range (y_i+1, maxY] for max z. If that max > z_i, then there exists a dominating point. But careful: we need to check existence of both y > y_i and z > z_i simultaneously. If we just take max z over that y range, and if max > z_i, does it guarantee there is a point with y > y_i and z > z_i? Yes, because max > z_i implies at least one point with z > z_i in that y range. However, could there be a point with y > y_i but its z might be <= z_i, but max > z_i indicates there is some point (maybe with different y) that has z > z_i. But is it possible that the point with high z has y exactly y_i? Not, because we query y > y_i, so exclude y_i. But what if there is a point with y > y_i and z <= z_i but the max comes from a point with y <= y_i? No, we restrict y range, so the max comes from points with y in that range. So if max > z_i, there exists at least one point in that range with z > z_i. So good.
But we need to handle y coordinate compression. Since coordinates up to 1e9 and N up to 500k, we compress y values. However note that we need to query for y > y_i, so we need to find the compressed index for y_i and then query from index+1 to end.
Data structure: BIT (Fenwick tree) for prefix maxima? BIT usually supports prefix queries, but we need suffix (greater than). If we compress and reverse order, we can use prefix queries. For simplicity, we can use segment tree for range max queries, or BIT for suffix by doing complement: we could store max over indexes and query suffix as total max - prefix? Not straightforward. Alternatively we can compress y in decreasing order or use BIT with reversed indexing: define new index = N_y - original_index + 1, then query prefix. But simpler to use segment tree.
But we also need to insert points: update at y compressed index with max(z, current stored). Because we only care about maximum z for each y? Actually we need to store the maximum z value among points with that y. But what about multiple points with same y but different z? Storing max z is sufficient because if we need to know existence of any point with y > y_i and z > z_i, then for each y we only need the highest z among points with that y. But there is nuance: if we have a point with y > y_i but its z is lower than z_i, it doesn't help; but we only care about max. So yes, storing the maximum for each y is enough. However, what about points with y > y_i but we might need to consider that the point with highest z might have y not the same as the point we want? It doesn't matter; if the maximum across the range > z_i, we are good.
But could there be a scenario where the maximum across the range is > z_i but the point that yields that maximum actually has y > y_i? Yes, it's in the range. So fine.
Now, we need to handle the fact that for the query we need both y > y_i and z > z_i. Using range max works.
But careful: if we store just the maximum per y, it's fine, but when we update multiple points with same y, we might need to keep the maximum anyway. So we can update by taking max.
So algorithm:
-
Read arrays B, I, R. Create list of points (b, i, r). We'll call dimensions x=beauty, y=intellect, z=richness.
-
Compress y coordinates: collect all distinct I values. Sort them, assign indices. We'll need to query y > y_i, so we need to know the range (rank(y_i)+1, M) where M is number of distinct y's.
-
Sort points by x descending. If x ties, we may process in batches.
-
Initialize a segment tree or BIT (for max) over y indices (1..M) with initial values -inf (or 0 but since z >=0, we can use -1). We'll query for range (idx_y+1, M) maximum z.
-
Iterate over points in sorted order. Use pointer i = 0 while i < N:
- Let j = i, while j < N and points[j].x == points[i].x: j++.
- For each index k from i to j-1: query for point k: find its compressed y index idx_y, then query maxZ = query_max(idx_y+1, M). If maxZ > point.z, then this point is dominated (self-murderer). Else not.
- After all queries for this x-value batch, then for each k from i to j-1: update at idx_y with max(current value, point.z). Since we need to update to reflect points with this x (they are now available for future points with smaller x). But note: if we update after queries, we guarantee that only points with strictly larger x are considered for domination. Good.
- Set i = j.
-
Count number of points where we found domination = answer.
Complexities: O(N log N) after sorting, assuming segment tree or BIT for max (BIT can be adapted for range max query? BIT typically does prefix sum; for max, BIT can support prefix max updates and queries if we do point updates and prefix queries. But we need range query for suffix. We can do BIT on reversed order: compress y, but also store values for max over indices i...M. BIT can support prefix max if we process indices from low to high, but to get suffix max, we can compress y in descending order: i.e., assign higher compressed index for smaller y? Actually we need to query y > y_i, so if we reverse the ordering: sort y ascending and assign indices 1..M for increasing y. Then y > y_i corresponds to indices > idx_y. To query suffix max, we could store BIT that can query suffix max by maintaining a BIT that stores max for prefix and then we can query from idx_y+1 to M using something like segment tree. Or we can use BIT with reversed index: define new index = M - idx_y + 1, then query prefix for new index? Let's explore: If we map each y to rank such that larger y gets larger rank? Actually originally we compress ascending: rank 1 for smallest y, rank M for largest y. Then condition y > y_i corresponds to ranks > rank_i. If we define rev_rank = M - rank + 1, then ranks > rank_i becomes rev_ranks < rev_rank_i? Let's compute: rank = r, rev = M - r + 1. If we want ranks > r, i.e., r' > r, then rev' = M - r' + 1 < M - r + 1 = rev. So indeed suffix in original becomes prefix in reversed. So we can build a BIT over rev indices that maintains max z for each rev. For point with original rank r, we update at rev = M - r + 1 with max. For query for y > y_i, we need to check if there exists any point with rev < rev_i (since rev_i = M - r_i + 1). So we query prefix max over rev indices 1 to rev_i - 1. If that max > z_i, then there is a dominating point. This works! BIT can easily support point update (set to max) and prefix max query. So we can use Fenwick tree for max.
Fenwick tree for max: initialize tree array of size M+2 with -inf (or -1). For update at index idx (rev), we set arr[idx] = max(arr[idx], val) and then propagate: while idx <= M, tree[idx] = max(tree[idx], val); idx += idx & -idx. For query prefix max up to idx: we take result = -inf; while idx > 0: result = max(result, tree[idx]); idx -= idx & -idx. That's standard.
So we can use BIT for max.
Now we need to handle compress y. Also note that we may have duplicate y values; that's fine because they map to same rank.
Now, we must also consider if there are multiple points with same (x, y, z) etc. Our algorithm will treat them correctly: If two points have same x, they will not dominate each other because x is not strictly greater. However they could have same x but one has higher y and z? Strict dominance requires all three strictly greater, so not. So for same x, they don't dominate. Our batch processing ensures that points with same x are not used for queries of each other because we query before inserting them. So good.
But what about ties in y? When we query, we require y > y_i, so if a point has same y but higher x and higher z, it would not count because y is not strictly greater. That's correct. So our query condition of y > y_i is correct.
Now, we need to be careful about potential large z values; we store as is.
Now, complexity: sorting O(N log N), compression O(N log N), BIT operations O(log N) per point => O(N log N). With N=500k, that's fine.
Now, confirm with sample:
N=3
B: 1 4 2
I: 4 3 2
R: 2 5 3
Points:
1: (1,4,2)
2: (4,3,5)
3: (2,2,3)
Sorted by x desc: point2 (4,3,5), point3 (2,2,3), point1 (1,4,2).
Compress y: distinct I = {4,3,2} => sorted asc: [2,3,4] ranks: y=2->1, y=3->2, y=4->3. M=3. Rev mapping: rev = M - rank +1. So for y=2 rank1 rev=3; y=3 rank2 rev=2; y=4 rank3 rev=1.
Initialize BIT size 3 with -inf.
Process batch x=4: only point2. Query for point2: y=3, rank2 rev=2. Query prefix max for rev indices 1 to rev-1 = 1. BIT initially -inf -> max = -inf < z=5 -> not dominated. Then update at rev=2 with z=5. BIT at rev2 set to 5.
Next batch x=2: point3 (2,2,3). y=2 rank1 rev=3. Query prefix max rev 1 to 2 (since rev_i-1 =2). BIT currently: rev2=5, rev1=-inf => max=5 > z=3? 5 > 3, yes -> dominated. So point3 is self-murderer. Then update at rev=3 with z=3. BIT: update rev3 max=3.
Next batch x=1: point1 (1,4,2). y=4 rank3 rev=1. Query prefix max rev 1 to 0? rev_i-1 =0 => no range, so result -inf, not dominated. Then update rev1 with z=2 (but currently rev1 maybe -inf, set to 2). So only one dominated => answer 1. Matches sample.
Great.
Now we need to ensure correctness for edge cases.
Potential issues:
-
Points with same x but multiple points: must process them as batch: query all then insert all. Insert all after queries.
-
Points with same y: Query uses y > y_i, so if same y, not considered.
-
Points with same z: query checks max > z_i, so if max == z_i, not >, so not dominated.
-
If there are points with x larger but y and z not both greater, no domination.
Now, we have to consider large N up to 500k and coordinates up to 1e9, but our BIT size equals number of distinct y values up to N, so BIT of size up to 500k, okay.
Now, we need to implement in C++ likely. Let's produce a solution.
But also consider: The problem statement: "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." So we need to count the number of ladies for which there exists another lady that strictly dominates in all three attributes.
So the algorithm as described works.
However, one nuance: Could there be multiple points with same (x,y,z)? They are identical. No domination because not strictly greater. So they are not self-murderers unless there is a point with all three greater (which would be > each). But identical points don't dominate each other. Our algorithm will treat them as separate. For a point with identical attributes as others, when processing batch with same x, we query before inserting any of that x batch, so no other identical point is considered; if there is an identical point but with higher x? Actually identical attributes would have same x, so if there is a point with strictly greater x, then x is greater, but y and z equal? Since they are identical, y and z equal, not strictly greater. So no domination. So they would be non-dominated.
Now, what about coordinate compression for y: we need to be careful that the BIT is built over rev indices, and we need to compute rev_i = M - rank_i + 1.
We must also consider that if there are many points, the BIT's prefix max query for rev_i-1 must handle rev_i = 1 (i.e., highest y because rev=1 corresponds to original rank M, which is largest y). Then rev_i-1 = 0, so query returns -inf, meaning no y > y_i? Wait, check: rev_i = 1 for original rank M (largest y). Then query prefix max for indices 1..0 yields none. That corresponds to y > y_i? Since y_i is the largest y, there are no y > y_i, correct. So we handle that.
Similarly, for y_i smallest, rev_i = M, query prefix max for indices 1..M-1 includes all other y's, which is correct.
Now we need to implement BIT for max. Initialize tree array of size M+2 with a very small number. Since z can be 0, we can use -1 as sentinel (since z >=0). But if all z are 0, then max > -1 works. But we need to distinguish no points: we set initial to -1 (or -inf). However, if z can be 0, and we have a point with z=0, then max > 0 would be false if all stored are 0, but if there is a point with z>0 then >0 will be true. That's fine. But careful: if we have a point with z=0 and there is another point with z=0 but larger x and y > y_i, then 0 > 0 is false, so not dominated. So it's fine.
But we need to consider that z can be 0, and the BIT initially -1 ensures that query returns -1 if no points, which is less than any z (>=0). So safe.
Now, update: we need to set BIT at index rev to max(current, z). Since BIT stores prefix max, we need to ensure that after multiple updates, the BIT correctly reflects maximum among all points at indices <= some index. For point update, we do: while idx <= M: tree[idx] = max(tree[idx], val); idx += idx & -idx; That's standard for max-BIT (also called Fenwick tree for maximum). However, note that this does not support decreasing values; but we only ever increase or keep same, because we take max. That's fine.
Now, we must be careful about handling duplicate y with multiple z updates: we always take max, so BIT stores the maximum z for that y and also for prefixes. That's fine.
Now, implementation steps:
- Read N.
- Read arrays B, I, R of size N.
- Create vector of points: struct {int x, y, z;} (or long long).
- Create vector of y values for compression: all I values.
- Sort and unique to get compressed mapping.
- For each point, compute rank_y (0-based or 1-based) using lower_bound. Then compute rev = M - rank (if rank 0-based? Let's define rank 0..M-1 sorted ascending. We'll map to 1-indexed for BIT. Let's do: compress sorted unique y. For a given y, get index = lower_bound(comp.begin(), comp.end(), y) - comp.begin() + 1 (1-indexed). So rank 1..M, with 1 smallest y, M largest y. Then rev = M - idx + 1. Because if idx = rank (1-based ascending), then rev = M - idx + 1 gives rev 1 for largest y, M for smallest. Then BIT over indices 1..M.
Alternatively, we could compress y in descending order directly: sort y descending, assign rev indices directly. But above is fine.
-
Sort points by x descending. If same x, we don't need to care about order within batch.
-
Initialize BIT of size M with initial values -1 (or -INF). We'll use a vector bit(M+2, -1). Since z can be up to 1e9, we might use int (2e9 fits in signed 32-bit? 1e9 fits, but max could be 1e9, so int is okay. Use long long for safety? Not necessary.
-
Iterate: i=0; while i < N:
- j = i; while j < N && points[j].x == points[i].x: j++.
- For k from i to j-1: compute rev, query BIT prefix max up to rev-1. If max > points[k].z, then set dominated flag for that point. We'll have a boolean array dominated[N] or count directly.
- Then for k from i to j-1: update BIT at rev with points[k].z.
- i = j.
-
Print count of dominated.
That's O(N log N). Memory O(N).
Now, let's test on some edge cases.
Case 1: N=1. Then no other lady, answer 0. Our algorithm: no batch queries, nothing inserted before first batch? The only point: we process batch x = that value. Query: rev-1 query returns -1 (since BIT empty) => max = -1 < z, not dominated. Then update. Count=0. Good.
Case 2: All points identical: (1,1,1) repeated N. Since x same, batch processing: for each point, query before any inserted, returns -1, not dominated. So answer 0. Good.
Case 3: Points where some dominate others: typical.
Now, there might be nuance: if there are multiple points with same x but different y, and one of them might be dominated by a point with same x but larger y and z? But strict domination requires all coordinates strictly greater, so if x equal, it cannot dominate because x not greater. So it's correct not to consider them.
Now, what about points where there is a point with larger x, and larger y but equal z? Not domination. Our query checks max > z_i, so if max == z_i, it's false. So correct.
Now, we need to be careful about coordinate compression: y values might be up to 1e9, but that's fine.
Now, we must implement BIT for max. However, note that BIT for max with point updates only supports setting to max (increase only). But consider the following scenario: Suppose we have two points with same y but different z, we update first with larger z, then later with smaller z. The BIT stored max, so it remains at larger z, which is correct because the larger z point still exists. However, what if we update with larger z later? We take max, so it works. So fine.
Now, verify with another test: Suppose points: A: (10,10,10), B: (9,9,9). Process A first (x=10): query returns -1, not dominated. Update BIT at y=10 (largest y) rev? Let's compress y: only 10 and 9. Sorted ascending: [9,10], M=2. For y=10, rank=2, rev=1. For y=9, rank=1, rev=2. Process A: rev=1, query prefix up to 0 => -1, not dominated. Update rev1 with z=10. Process B: y=9, rev=2, query prefix up to rev-1=1: max at rev1=10 > 9 => dominated. So B is dominated. Correct.
Now, what if points: A: (10,9,10), B: (9,10,9). Can A dominate B? For B to be dominated, need some point with x>9, y>10? Actually B: (9,10,9). A: (10,9,10). For B, need x>9 (A has 10 okay), y>10 (A has 9 not >10), z>9 (A has 10 okay). So not all three greater, so B not dominated. Our algorithm: compress y: 9 and 10, M=2, rev for y=10 rank2 rev1, y=9 rank1 rev2. Process A first (x=10): y=9 rev2, query prefix up to rev2-1=1: initially -1, not dominated. Update rev2 with z=10. Process B (x=9): y=10 rev1, query prefix up to rev1-1=0 => -1, not dominated. So B not dominated. Good.
Now, what about points with same x but one has larger y and z, but we process them in batch: they will not consider each other. So correct.
Now, complexity: BIT operations O(log M) per point, plus sorting O(N log N). N=500k, log N ~19, fine.
Now, we should also consider memory for points: we can store as arrays of ints. Use vector of struct. Input reading: 3 lines of N integers each. We'll need to store all points.
Now, need to be careful with large input, use fast IO.
Now, is there any nuance with the BIT max query for prefix up to rev-1? If rev-1 = 0, the while loop will not run, return -1.
Now, we also need to ensure that we treat x strictly greater. By processing batch after queries, we guarantee that only points with x > current point's x are considered. However, what about points with same x but different y? They are not considered because we haven't inserted them yet. So good.
Now, check for points with equal x but also equal y? Still fine.
Now, edge case: multiple points with same x, but some may be dominated by points from earlier batch? Yes, that will be captured.
Now, we should also consider if there are points with same x but we process batch queries, then update. So all points in the batch have the same x. For each, we query only points with x strictly greater. So correct.
Now, consider if there are points with x sorted descending, but we have to be careful about stable sorting? Not needed.
Now, let's test with random small data and brute force to verify.
But before writing final solution, we should also consider alternative approach: we could also compute the number of non-dominated points (maxima) and subtract. But our algorithm directly counts dominated ones.
Now, we must ensure that BIT for max works with negative initial values. We'll use -1 since all z >=0. If there is possibility of z being 0? Then -1 works as sentinel.
Now, what about coordinate compression for y: we need distinct y sorted ascending. That is fine.
But what about the case where y values are huge but not many distinct. That's fine.
Now, we also need to consider if we can use 0-indexed BIT? Using 1-indexed for BIT is easier.
Now, implement BIT:
class BITMax {
private:
vector tree;
int n;
public:
BITMax(int size) : n(size), tree(size+2, -1) {}
void update(int idx, int val) { // idx is 1-indexed
while (idx <= n) {
if (val > tree[idx]) tree[idx] = val;
idx += idx & -idx;
}
}
int query(int idx) { // maximum in [1, idx]
int res = -1;
while (idx > 0) {
if (tree[idx] > res) res = tree[idx];
idx -= idx & -idx;
}
return res;
}
};
Now, note: The BIT stores max for prefix. When we want to query for y > y_i (i.e., indices > idx_y), we map to rev: rev = M - idx_y + 1, and then we query prefix up to rev-1. So we compute rev_i, then maxz = bit.query(rev_i - 1). If maxz > point.z, dominated.
Now, need to compute idx_y: after compression, we have vector y_vals (sorted unique). For a point's y, we find its index: int rank = lower_bound(y_vals.begin(), y_vals.end(), y) - y_vals.begin() + 1; (1-indexed). Then rev = M - rank + 1.
Alternatively, we could compress y in descending order to avoid rev calculation: we sort y descending, assign indices 1..M for descending order. Then y > y_i corresponds to indices less than something? Let's examine: If we sort descending, then the largest y gets index 1, second largest index 2, etc. Then for a point with y_i, its index idx_desc = position in descending order (1-based). Then the set of y > y_i corresponds to indices < idx_desc. So we can query prefix max up to idx_desc - 1. That's similar but without rev. However we still need to compress: we can create a vector of y sorted descending, assign index by order in that list. But easier is to use ascending compression and then compute rev. Either is fine.
We'll do ascending and then rev.
Now, we need to store points with x, y, z and also we need original order? Not needed for counting. We just need to set a flag for each point when processing. Since we are sorting, we lose original order; we can store a boolean array of size N, but we need to know which point is which after sorting? Actually we don't need to output which ones, just count how many are dominated. So we can simply increment count when we find a dominated point during processing. We don't need to map back to original indices. So we can just store points in a vector, sort, and while processing we compute count.
But careful: When we process batches, we may need to handle points with same x but multiple ones. They will be processed together, and we can count them as we query.
We'll do:
vector pts(N);
for i in 0..N-1: pts[i] = {B[i], I[i], R[i]};
Compress y: collect all I.
After sorting points by x descending, we loop.
Implementation steps in code:
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int N;
cin >> N;
vector B(N), I(N), R(N);
for (int i=0; i<N; ++i) cin >> B[i];
for (int i=0; i<N; ++i) cin >> I[i];
for (int i=0; i<N; ++i) cin >> R[i];
vector<tuple<int,int,int>> pts(N); // maybe store as struct
for (int i=0; i<N; ++i) pts[i] = {B[i], I[i], R[i]};
// compress y (intellect)
vector<int> ys = I;
sort(ys.begin(), ys.end());
ys.erase(unique(ys.begin(), ys.end()), ys.end());
int M = ys.size();
// sort pts by x descending
sort(pts.begin(), pts.end(), [](const auto& a, const auto& b) {
return get<0>(a) > get<0>(b); // descending
});
BITMax bit(M);
int ans = 0;
int i = 0;
while (i < N) {
int j = i;
while (j < N && get<0>(pts[j]) == get<0>(pts[i])) ++j;
// first query for all in [i, j)
for (int k = i; k < j; ++k) {
int y = get<1>(pts[k]);
int z = get<2>(pts[k]);
// get compressed rank (1-indexed) in ascending ys
int rank = lower_bound(ys.begin(), ys.end(), y) - ys.begin() + 1;
int rev = M - rank + 1;
// query prefix up to rev-1
int maxz = (rev-1 > 0) ? bit.query(rev-1) : -1;
if (maxz > z) {
++ans;
}
}
// then update
for (int k = i; k < j; ++k) {
int y = get<1>(pts[k]);
int z = get<2>(pts[k]);
int rank = lower_bound(ys.begin(), ys.end(), y) - ys.begin() + 1;
int rev = M - rank + 1;
bit.update(rev, z);
}
i = j;
}
cout << ans << endl;
}
Now, check if we need long long for z? z up to 1e9 fits in int, but we might have sentinel -1. So int is fine. However, BIT query returns int, but safe.
But need to ensure that bit.query(rev-1) when rev-1 is 0 returns -1. In our code we conditionally call bit.query only if rev-1 > 0, else we set maxz = -1. That's fine.
Alternatively, we could call bit.query(0) which will return -1 because while loop not executed, but our BIT implementation query(0) should return initial -1. We can implement query to handle 0 gracefully. So we can simply call bit.query(rev-1) without condition; we need to ensure BIT.query(0) returns -1. In our BIT, while (idx > 0) loop, if idx == 0, loop skipped and return res (initialized -1). So fine. So we can just call bit.query(rev-1) without condition.
Now, we must test on sample.
Now, think about potential pitfalls:
-
When sorting by x descending, we need to handle large N and possible ties: the batch processing works.
-
But there is nuance: Suppose we have points with same x and same y but different z. Since we query before inserting, they won't consider each other. However, what if there is a point with x greater, y greater, but z not greater? Not domination. That's fine.
-
But consider scenario: A: (5,5,5), B: (5,6,6), C: (4,6,7). Actually x: A and B both 5, C 4. Process batch x=5: first A and B (in some order). For A: query with points having larger x? None, so not dominated. For B: query, also none. Then insert A and B. Next batch x=4: C. Query for C: need to see if any point with y>6? Actually C's y=6, so need y>6? But B has y=6, not >, so no. But B's z=6, C's z=7, not >. So C not dominated. That's correct.
But consider scenario where there are points with same x but one could be dominated by another point with same x if we considered them? Not allowed because x must be strictly greater. So correct.
Now, we need to consider if there are points with the same x, but we might incorrectly count them if we insert before querying. So batch processing is crucial.
Now, we also need to ensure that the BIT stores max z for each y, but only from points with larger x. That's maintained because we only update after processing each batch.
Now, we also must consider the possibility of multiple points with same y. The BIT update with max ensures we keep the highest z among points with that y. That's sufficient for existence queries.
Now, verify with a case where there are multiple points with the same y but different z and we might need a lower z later? But we only care about existence of any point with y > y_i and z > z_i. If the highest z for a given y is not enough (z <= z_i), then no point with that y qualifies. But could there be a point with a lower y that actually has a higher z? Wait, we query over y > y_i, so includes all y greater than y_i. Among those, we take the maximum z across all of them. If that maximum > z_i, then there exists at least one point with y > y_i and z > z_i. If the maximum is <= z_i, then no such point exists. So storing max per y and taking max across range is correct.
Now, check if there is a scenario where the maximum across the range is from a point with y > y_i but its z is > z_i, but that point may have been updated after the query? No, because we only update after queries for the current batch, so all points in BIT have x strictly greater than current point's x. So the existence condition is satisfied.
Now, about the compression: we need distinct y values. But what if there are no points? trivial.
Now, complexity: sorting points O(N log N) and BIT operations O(N log N) with constants low.
Now, think about memory: vectors of size N for points, and for BIT size M up to N, OK.
Now, potential overflow: None.
Now, we need to be careful about the input format: there are three lines, each containing N integers separated by spaces. They might be on multiple lines? But problem says lines, but we can just read using cin in a loop.
Now, test with random data: We'll produce a brute-force for small N to test.
But as ChatGPT, I can't run code but will reason.
Consider N=2, points: (1,2,3) and (2,1,4). Does (2,1,4) dominate (1,2,3)? Need x>1 (2>1), y>2 (1 not >2) no. So answer 0. Our algorithm: compress y: {1,2}, M=2. Sort by x desc: (2,1,4) first. Process batch x=2: y=1, rank1? ascending y: [1,2]; y=1 rank1 rev=2 (since M=2, rev = 2-1+1=2). Query prefix up to rev-1=1: bit.query(1) initial -1 -> not dominated. Update rev2 with z=4. Next batch x=1: y=2 rank2 rev=1. Query prefix up to 0 -> -1. Not dominated. So answer 0. Good.
Consider (2,2,2) and (1,1,1): (2,2,2) dominates (1,1,1). Process (2,2,2): y=2, rank? y's {1,2}, so rank2 rev=1? Actually ascending: [1,2] rank2 rev = M-rank+1 =2-2+1=1. Query prefix up to 0 => -1. Update rev1 with z=2. Process (1,1,1): y=1 rank1 rev=2. Query prefix up to rev-1=1 => maxz=2 >1 => dominated. Answer 1. Good.
Now, consider multiple points: (10,1,10), (9,2,9), (8,3,8). This is decreasing in all coordinates. Each successive dominates the previous? Check (10,1,10) vs (9,2,9): x 10>9, y 1 not >2, so no. So no domination. Our algorithm: compress y {1,2,3} M=3. Process x=10: y=1 rank1 rev=3 query prefix up to 2? rev=3 => rev-1=2 query over indices 1-2 (no points) -> -1. Update rev3 with z=10. Process x=9: y=2 rank2 rev=2 query prefix up to 1: maxz = bit.query(1) -> rev1? Initially no, -1? Actually rev1 corresponds to y=3? Wait, mapping: ascending y [1,2,3] rank1->rev=3, rank2->rev=2, rank3->rev=1. After first update, rev3 (y=1) set to 10. rev1 (y=3) and rev2 (y=2) are -1. Query for rev=2 (y=2) prefix up to 1: rev1 is -1, so maxz=-1 <9, not dominated. Update rev2 with 9. Process x=8: y=3 rank3 rev=1 query prefix up to 0 -> -1, not dominated. So answer 0. Good.
Now, consider (5,5,5), (6,6,6), (7,7,7) -> each later dominates earlier? Actually (7,7,7) dominates both (6,6,6) and (5,5,5) etc. But processing in descending order: first x=7: not dominated. Update. Then x=6: query for y=6 rev? compress y {5,6,7} M=3. y=6 rank2 rev=2? Let's compute: ascending [5,6,7]: rank1->rev3 (y5), rank2->rev2 (y6), rank3->rev1 (y7). After first point (x=7,y=7,z=7): update rev1 with 7. For x=6,y=6,rev=2 query prefix up to 1: rev1 maxz=7 >6 => dominated. Then update rev2 with 6. For x=5,y=5,rev=3 query prefix up to 2: rev1? Actually rev3 query up to 2 includes rev1 and rev2? Wait rev=3, rev-1=2, so query indices 1..2. rev1=7, rev2=6 => maxz=7 >5 => dominated. So answer 2 dominated. Good.
Now, consider scenario with many points with same x: (5,1,10), (5,2,9), (5,3,8), (4,4,7). Which ones are dominated? Since x=5 points have no larger x, none are dominated by larger x points. However, could they dominate each other? No because x equal. So answer should be 0. But check our algorithm: batch x=5: query each before update (BIT empty) => not dominated. Then update all. Then x=4: y=4 rev? compress y=1,2,3,4. rev for y=4 is? ascending [1,2,3,4] rank4 rev=1. Query prefix up to 0 => -1. So not dominated. Answer 0. Good.
Now, scenario with batch x=5, and there is a point with x=5 that could be dominated by a point also with x=5 but we don't consider because not strictly greater x. So fine.
Now, we should test a scenario where there are multiple points with same y and different z, to see if BIT max works. For example: Points: A: (10,5,10), B: (9,6,5), C: (8,7,6). We want to see if C is dominated: need x>8,y>7,z>6. There is A: x=10>8, y=5 not >7; B: x=9>8, y=6 not >7; So no. Our algorithm: compress y {5,6,7} M=3. Process x=10: y=5 rank1 rev=3, update rev3 with 10. x=9: y=6 rank2 rev=2, query prefix up to 1: rev1? no point, -1. So B not dominated. Update rev2 with 5. x=8: y=7 rank3 rev=1, query prefix up to 0 -> -1. So not dominated. Good.
Now, scenario where multiple y but need max across range: A: (10,1,100), B: (9,2,1), C: (8,3,2). Is C dominated? Need x>8,y>3,z>2. A has x=10>8, y=1 not >3; B has x=9>8, y=2 not >3; So no. Our algorithm: compress y [1,2,3] rev: y=1 rev3, y=2 rev2, y=3 rev1. Process A: update rev3 with 100. B: query rev2 prefix up to1: rev1 -1, not dominated, update rev2 with 1. C: query rev1 prefix up to0 -> -1, not dominated. Good.
Now, scenario where existence requires combination: A: (10,5,5), B: (9,6,4), C: (8,7,6). Check C: need y>7 and z>6. No point has y>7. So not dominated. That matches.
Now, scenario where the max z across y>y_i might come from a point with z > z_i, but that point has y exactly y_i+? okay.
Now, verify with potential tricky case: Suppose we have points: (10, 10, 10), (9, 9, 9), (8, 11, 8). The third point (8,11,8): need x>8, y>11, z>8. First point has y=10 not >11, second has y=9 not >11. So not dominated. Our algorithm: compress y {9,10,11} M=3. Rev mapping: y9 rank1 rev3, y10 rank2 rev2, y11 rank3 rev1. Process x=10: y10 rev2 update z10. Process x=9: y9 rev3 query prefix up to 2? rev=3 -> rev-1=2 includes rev1? Wait rev1? prefix up to 2 includes rev1 and rev2? Actually rev indices: rev1 corresponds to y=11 (largest), rev2 to y=10, rev3 to y=9. Query for rev=3 prefix up to 2 includes rev1 and rev2. rev1 initially -1, rev2=10 => maxz=10 >9? 10>9, so point (9,9,9) dominated. Then update rev3 with 9. Process x=8: y11 rev1 query prefix up to 0 -> -1. So not dominated. Answer 1. Good.
Now, consider scenario where the order of processing within a batch does not matter because we query all before updating.
Now, we need to be careful about the BIT size: M = number of distinct y values, which can be up to N. In worst case, all y distinct. BIT array of size M+2 is fine.
Now, what about memory usage for points: storing tuple of three ints. That's fine.
Now, check if we need to use 64-bit integers for z? Up to 1e9, int is fine.
Now, we need to read input efficiently: N up to 500k, each line 500k integers, total 1.5 million ints. Using cin with sync off is fine.
Now, confirm that we don't need to worry about negative values: all non-negative.
Now, what about the case where y values are large and we use lower_bound each time per point. In the batch loops, we compute rank twice (once for query, once for update). Could compute once and store. But N=500k, lower_bound O(log M) each, 2*N times = ~1M * log(500k) ~ 20M operations, fine. But we can store rank and rev in the point struct after compression to avoid recomputation. However, we need to sort points by x after compression? Actually we can compute rank and rev before sorting, because y values are known. But careful: When we sort points by x, we need to keep the rank/rev associated. So we can augment each point with its compressed y and rev index. That would save time. Let's do that: after reading points, compress y. For each point, compute rank and rev and store. Then sort points by x descending. Then during processing, we just use stored rev and z. That reduces lower_bound calls to N. Good.
Thus:
struct Point {
int x, y, z;
int rev; // or rank
};
After reading arrays, create points with x,y,z. Then compress y: vector ys = I; sort unique; for each point, compute rank = lower_bound(ys.begin(), ys.end(), point.y) - ys.begin() + 1; rev = M - rank + 1; store rev. Then sort points by x descending (if same x, order doesn't matter). Then proceed.
Now, BIT operations: update(rev, z) and query(rev-1).
Now, answer count increments when maxz > z.
Edge Cases:
-
If M=0? Not possible because N>=1.
-
If all y are same? Then rev mapping: only one y, M=1. rev for that y = 1. Then for each point, rev=1, query(0) returns -1. So no domination from y > y_i because there is no y greater. That's correct.
-
If there are points with y equal to max, then rev=1, query(0) => none. Good.
Now, consider if there are multiple points with same rev index but different z; update with max ensures we keep highest z.
Now, note: When we update, we might be updating the same rev multiple times in a batch if there are multiple points with same y. That's okay; we just take max.
Now, confirm with scenario: points: (10,5,5), (9,6,6), (8,5,4). The third point (8,5,4): y=5, need y>5 to dominate. There is point (9,6,6) with y=6 >5 and z=6 >4, so it should be dominated. Our algorithm: compress y {5,6}, M=2. rev: y5 rank1 rev2, y6 rank2 rev1. Process x=10: y5 rev2, update rev2 with 5. Process x=9: y6 rev1 query rev1-1=0 => -1, not dominated. Update rev1 with 6. Process x=8: y5 rev2 query rev2-1=1 => query prefix up to 1 includes rev1=6 => maxz=6 >4 => dominated. Good.
Now, what about scenario where the dominating point has y > y_i but z just slightly above. That works.
Now, potential pitfall: The BIT only stores max z per prefix, but does it guarantee that the point with that max z also has y > y_i? Since we query prefix up to rev_i-1, which corresponds to rev indices less than rev_i. rev indices correspond to y values greater than y_i. So yes.
Now, verify with rev mapping: ascending y => rank small for small y, large for large y. rev = M - rank + 1 gives rev large for small y, small for large y. So rev index order is descending y. So prefix up to rev_i-1 corresponds to rev indices 1..rev_i-1, which are the smallest rev numbers, i.e., the largest y values, but does that exactly correspond to y > y_i? Let's confirm: If y_i is some value, its rank = r, where r is position in ascending order. Then rev = M - r + 1. The set of y > y_i corresponds to ranks r+1 .. M. Their revs = M - (r+1) + 1 = M - r, ..., M - M + 1 = 1. So rev values from M - r down to 1. Since rev_i = M - r + 1, rev_i - 1 = M - r. So indeed the rev values for y > y_i are exactly 1 .. rev_i - 1. So prefix up to rev_i - 1 covers exactly those y > y_i. Good.
Now, we should ensure BIT query(rev_i - 1) indeed gets max among all points with y > y_i. Since we update with rev indices.
Now, what about points with y equal to y_i? They are not in the prefix because rev_i corresponds to y_i, and rev_i - 1 excludes it. So good.
Now, time complexity: O(N log N). Memory: O(N).
Now, we must also consider if there is any case where we need to consider points with same x but also same y, etc. Already handled.
Now, think about the possibility of large input that may cause stack overflow if using recursion? No recursion.
Now, what about using long long for BIT? Not needed.
Now, implement final solution.
Now, we can also do alternative: use segment tree for max over y range. But BIT is simpler.
Now, we must be careful about the order of sorting: we sort by x descending. But what about ties in x? As said, we process batch together. However, we also need to consider that among points with same x, there may be many; they will be inserted after queries, so they won't affect each other.
Now, check a corner: Suppose there are points with same x and same y and same z, but there is a point with larger x that dominates them? That would be captured.
Now, ensure that we don't need to consider the scenario where a point could be dominated by a point with larger x but also larger y but not larger z? No, need all three.
Now, the algorithm seems correct.
Now, let's consider if there is any other nuance: The problem says Bi, Ii, Ri can be up to 1e9. So coordinates fit in 32-bit signed int? 1e9 < 2^31-1, so int is fine. But using int is okay.
Now, we'll finalize.
But wait, there is a subtlety: Our BIT for max only stores the maximum z among points with a given rev index, and we query prefix max. However, we update each point's z at its rev index, but we never remove or change values when we move to next x batch. That's correct because we only add points as we go from higher x to lower x. All points we add remain in the BIT because they have x larger than any later points (since we process descending). So they are valid for all future queries.
Now, is there any risk that the BIT may store a point that has y > y_i but also has x not strictly greater? No, because we only insert points with x greater than current point's x, since we process descending and only insert after querying the current batch. However, consider points with same x but processed in previous batch? Since we batch by x, all points with same x are inserted after all queries for that x. So when processing a later batch with smaller x, the BIT contains all points with x strictly greater (because they were inserted earlier). So good.
Now, one more nuance: Points with same x but we might have earlier batches with higher x that dominate some of them. That's handled.
Now, what about points with same x but also same y and z? They are not dominating.
Now, I think it's correct.
Now, consider the possibility of integer overflow in BIT updates: not.
Now, verify with a more complex case: random data with N=10, brute force.
But given the constraints, this solution should pass.
Now, we must also consider that we are counting the number of self-murderers. Is it possible that a point is counted multiple times? No, we count once per point when we query.
Now, finalize code.
Now, let's test manually with a case where multiple points dominate same point: That's fine.
Now, think about performance: Sorting 500k points by x descending: O(N log N). Compression: O(N log N) due to sorting y values. BIT operations O(N log N). Should be within time.
Now, we also might use vector of struct for points. Use tuple might be slower; we can use struct with custom comparator.
Now, code structure:
#include <bits/stdc++.h>
using namespace std;
struct Point {
int x, y, z;
int rev; // reversed index for BIT
};
struct BITMax {
int n;
vector tree;
BITMax(int size) : n(size), tree(size+2, -1) {}
void update(int idx, int val) {
while (idx <= n) {
if (val > tree[idx]) tree[idx] = val;
idx += idx & -idx;
}
}
int query(int idx) {
int res = -1;
while (idx > 0) {
if (tree[idx] > res) res = tree[idx];
idx -= idx & -idx;
}
return res;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
vector pts(N);
// read B
for (int i = 0; i < N; ++i) cin >> pts[i].x;
// read I
for (int i = 0; i < N; ++i) cin >> pts[i].y;
// read R
for (int i = 0; i < N; ++i) cin >> pts[i].z;
// compress y
vector<int> ys(N);
for (int i = 0; i < N; ++i) ys[i] = pts[i].y;
sort(ys.begin(), ys.end());
ys.erase(unique(ys.begin(), ys.end()), ys.end());
int M = ys.size();
// assign rev index to each point
for (int i = 0; i < N; ++i) {
int rank = lower_bound(ys.begin(), ys.end(), pts[i].y) - ys.begin() + 1; // 1-indexed rank
pts[i].rev = M - rank + 1;
}
// sort by x descending
sort(pts.begin(), pts.end(), [](const Point& a, const Point& b) {
return a.x > b.x;
});
BITMax bit(M);
int ans = 0;
int i = 0;
while (i < N) {
int j = i;
while (j < N && pts[j].x == pts[i].x) ++j;
// query for points in [i, j)
for (int k = i; k < j; ++k) {
int maxz = bit.query(pts[k].rev - 1);
if (maxz > pts[k].z) {
</think>