To solve this problem, we need to determine the number of ladies at a ball who are potential self-murderers. A lady is considered a self-murderer if there exists another lady who is more beautiful, smarter, and richer than her.
Approach
- Sort by Beauty: First, we sort all the ladies in descending order of their beauty (B). This allows us to process each lady knowing that all previously considered ladies have a beauty value greater than or equal to the current lady.
- Coordinate Compression: We compress the intellect (I) values to reduce the problem to a manageable size, especially since I values can be up to 10^9.
- Segment Tree for Maximum Richness Query: Using a segment tree, we dynamically track the maximum richness (R) encountered so far for each compressed intellect rank. This allows us to efficiently query if there exists a previously processed lady with higher intellect and richness than the current lady.
Solution Code
#include <bits/stdc++.h>
using namespace std;
struct Lady {
int B, I, R;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<Lady> ladies(N);
for (int i = 0; i < N; ++i) {
cin >> ladies[i].B;
}
for (int i = 0; i < N; ++i) {
cin >> ladies[i].I;
}
for (int i = 0; i < N; ++i) {
cin >> ladies[i].R;
}
// Sort ladies by descending B
sort(ladies.rbegin(), ladies.rend(), [](const Lady& a, const Lady& b) {
return a.B > b.B;
});
// Coordinate compression for I values
vector<int> all_I;
for (const auto& l : ladies) all_I.push_back(l.I);
sort(all_I.begin(), all_I.end());
all_I.erase(unique(all_I.begin(), all_I.end()), all_I.end());
int m = all_I.size();
// Build segment tree. Using a dynamic approach with a vector
// We need a segment tree for range max queries and point updates
int tree_size = 1;
while (tree_size < m) tree_size <<= 1;
vector<int> seg(2 * tree_size, -1);
auto update = [&](int pos, int val) {
pos += tree_size;
if (seg[pos] < val) {
seg[pos] = val;
pos >>= 1;
while (pos >= 1) {
int new_val = max(seg[2 * pos], seg[2 * pos + 1]);
if (seg[pos] == new_val) break;
seg[pos] = new_val;
pos >>= 1;
}
}
};
auto query = [&](int l, int r) {
// Query the max in [l, r]
if (l > r) return -1;
int res = -1;
l += tree_size;
r += tree_size;
while (l <= r) {
if (l % 2 == 1) {
res = max(res, seg[l]);
l++;
}
if (r % 2 == 0) {
res = max(res, seg[r]);
r--;
}
l >>= 1;
r >>= 1;
}
return res;
};
int ans = 0;
for (const auto& l : ladies) {
int I_val = l.I;
int rank = lower_bound(all_I.begin(), all_I.end(), I_val) - all_I.begin();
int query_l = rank + 1;
int query_r = m - 1;
int max_R = query(query_l, query_r);
if (max_R > l.R) {
ans++;
}
update(rank, l.R);
}
cout << ans << '\n';
return 0;
}
Explanation
- Sorting by Beauty: We first sort all ladies in descending order of their beauty. This allows us to process each lady in order and ensure that we have considered all more beautiful (or equally beautiful) ladies before the current lady.
- Coordinate Compression: We compress the intellect values (I) to a smaller range. This is done by ranking the unique values of I, which helps in efficiently using the segment tree.
- Segment Tree Operations:
- The segment tree is used to keep track of the maximum richness (R) encountered for each compressed intellect rank.
- For each lady, after finding her compressed I rank, we query the segment tree to check if there exists a previously processed lady with a higher I rank and higher R value.
- If such a lady exists, the current lady is a potential self-murderer, and we increment the count.
- We then update the segment tree with the current lady's I rank and R value.
This approach efficiently reduces the problem complexity to O(N log M), where M is the number of unique intellect values, making it feasible for large input sizes up to 500,000.