这是2021csp-s复赛的第2题一开始用栈只对了一个所以考虑到优先队列
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
struct range {
int x, y;
} a[100005], b[100005];
int res1[100005], res2[100005];
int n;
bool cmp(const range& a, const range& b) {
return a.x < b.x;
}
int main() {
int m1, m2;
cin >> n >> m1 >> m2;
for (int i = 1; i <= m1; i++) {
cin >> a[i].x >> a[i].y;
}
for (int i = 1; i <= m2; i++) {
cin >> b[i].x >> b[i].y;
}
sort(a + 1, a + m1 + 1, cmp);
sort(b + 1, b + m2 + 1, cmp);
stack<pii> lq1, lq2;
stack<int> wq1, wq2;
for (int i = 1; i <= n; i++) {
wq1.push(i);
wq2.push(i);
}
for (int i = 1; i <= m1; i++) {
while (!lq1.empty() && a[i].x >= lq1.top().first) {
wq1.push(lq1.top().second);
lq1.pop();
}
if (!wq1.empty()) {
int dest = wq1.top();
wq1.pop();
res1[dest]++;
lq1.push(make_pair(a[i].y, dest));
}
}
for (int i = 1; i <= m2; i++) {
while (!lq2.empty() && b[i].x >= lq2.top().first) {
wq2.push(lq2.top().second);
lq2.pop();
}
if (!wq2.empty()) {
int dest = wq2.top();
wq2.pop();
res2[dest]++;
lq2.push(make_pair(b[i].y, dest));
}
}
for (int i = 1; i <= n; i++) {
res1[i] += res1[i - 1];
res2[i] += res2[i - 1];
}
int ans = 0;
for (int i = 0; i <= n; i++) {
ans = max(ans, res1[i] + res2[n - i]);
}
cout << ans << endl;
return 0;
}