#include <bits/stdc++.h>
using namespace std;
const int N = 200'000 + 10;
int n;
pair<int, int> h1[N], h2[N];
const int MAX = 1'000'000'000;
namespace IT {
int f[N << 3], lazy[N << 3];
void apply(int s, int l, int r) {
if (!lazy[s]) return;
f[s] += lazy[s];
if (l != r) {
lazy[s << 1] += lazy[s];
lazy[s << 1 | 1] += lazy[s];
}
lazy[s] = 0;
}
void update(int s, int l, int r, int u, int v, int x) {
apply(s, l, r);
if (v < l || u > r) return;
if (u <= l && r <= v) {
lazy[s] += x;
apply(s, l, r);
return;
}
int mid = (l + r) >> 1;
update(s << 1, l, mid, u, v, x);
update(s << 1 | 1, mid + 1, r, u, v, x);
f[s] = min(f[s << 1], f[s << 1 | 1]);
}
int query(int s, int l, int r, int u, int v) {
apply(s, l, r);
if (v < l || u > r) return MAX;
if (u <= l && r <= v) return f[s];
int mid = (l + r) >> 1;
return min(query(s << 1, l, mid, u, v), query(s << 1 | 1, mid + 1, r, u, v));
}
}
vector<int> save[N];
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> h1[i].first >> h1[i].second;
for (int i = 1; i <= n; ++i) cin >> h2[i].first >> h2[i].second;
reverse(h1 + 1, h1 + n + 1);
reverse(h2 + 1, h2 + n + 1);
vector<int> allValue;
{ // discrete
for (int i = 1; i <= n; ++i) {
allValue.push_back(h1[i].second);
allValue.push_back(h2[i].second);
}
sort(allValue.begin(), allValue.end());
allValue.erase(unique(allValue.begin(), allValue.end()), allValue.end());
for (int i = 1; i <= n; ++i) {
h1[i].second = upper_bound(allValue.begin(), allValue.end(), h1[i].second) - allValue.begin();
h2[i].second = upper_bound(allValue.begin(), allValue.end(), h2[i].second) - allValue.begin();
}
}
const int m = allValue.size();
int answer = 0;
for (int i = 1, it = 1; i <= n; ++i) {
const auto& [color, point] = h2[i];
while (it <= n && h1[it].second <= point) {
save[h1[it].first].push_back(h1[it].second);
IT::update(1, 1, m, h1[it].second, m, 1);
it += 1;
}
if (save[color].size() && IT::query(1, 1, m, save[color].back(), m) > 0) {
IT::update(1, 1, m, save[color].back(), m, -1);
save[color].pop_back();
} else {
IT::update(1, 1, m, point, m, -1);
answer += 1;
}
}
cout << answer << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |