题目链接:https://ac.nowcoder.com/acm/contest/16/A
思路:二维偏序问题,按照第一维排序,再用树状数组处理第二维即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define bug prllf("*********\n")
#define debug(x) cerr<<#x<<" = "<<(x)<<endl
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int mod = 998244353;
const double eps = 1e-8;
const double pi = acos(-1);
template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}
const int N = 1e5 + 7;
int n, c[N];
vector<int> v;
void add(int x, int y) {while(x <= n) c[x] += y, x += x&-x;}
int sum(int x) {int ret = 0;while(x > 0) ret += c[x], x -= x&-x; return ret;}
int getid(int x){return lower_bound(v.begin(),v.end(),x)-v.begin() + 1;}
struct node
{
int x, y;
bool operator <(const node &tmp)const{
return x < tmp.x;
}
}a[N];
int main()
{
IO; cin >> n;
for(int i = 0; i < n; i++)
{
cin >> a[i].x >> a[i].y;
v.push_back(a[i].y);
}
sort(a, a + n);
sort(all(v)); v.erase(unique(all(v)), v.end());
int ans = 0;
for(int i = n - 1; i >= 0; i--)
{
int pos = getid(a[i].y);
if(sum(pos) != n - 1 - i) ans++;
add(pos, 1);
}
cout << ans << endl;
}