https://www.nowcoder.com/acm/contest/16/A
这是一道二维偏序的问题,在一维偏序中我们可以直接排序,然而在二维偏序中,当然可以用n^2的复杂度解决问题。然而如果需要优化的话,我们必须要防止后效性,也就是固定一维考虑另外一维并且防止错解。
不妨设对于每一个物品的两个参数为se与ve,那么我需要知道在ve大于当前物品的物品中,是否存在se也大于当前物品se的,这里就可以使用树状数组统计了。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define ll long long int
#define INF 0x3f3f3f3f
#define lowbit(x) x&(-x)
const int maxn = 1e5 + 10;
int c[maxn];
int n;
void add(int x) {
for (; x <= n; x += lowbit(x)) c[x]++;
}
int query(int x) {
int ans = 0;
for (; x ; x -= lowbit(x)) ans += c[x];
return ans;
}
pair<int, int> pii[maxn];
vector<int> ve;
int main()
{
while (~scanf("%d", &n)) {
ve.clear();
for (int i = 1; i <= n; i++) {
scanf("%d%d", &pii[i].first, &pii[i].second);
ve.push_back(pii[i].second);
}
for (int i = 0; i <= n; i++) c[i] = 0;
sort(pii + 1, pii + 1 + n);
sort(ve.begin(), ve.end());
int ans = 0;
for (int i = n; i >= 1; i--) {
int pos = lower_bound(ve.begin(), ve.end(), pii[i].second) - ve.begin() + 1;
if (query(n) - query(pos)) ans++;
add(pos);
}
printf("%d\n", ans);
}
return 0;
}
//_CRT_SECURE_NO_WARNINGS