// #pragma GCC optimize("O3,unroll-loops") // #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #include "bits/stdc++.h" using namespace std; using ll = long long int; mt19937_64 RNG(chrono::high_resolution_clock::now().time_since_epoch().count()); struct Node { using T = int; T unit = 0; T f(T a, T b) { return max(a, b); } Node *l = 0, *r = 0; int lo, hi; T mset = 0; T val = unit; Node(int _lo,int _hi):lo(_lo),hi(_hi){} T query(int L, int R) { if (R <= lo || hi <= L) return unit; if (L <= lo && hi <= R) return val; push(); return f(l->query(L, R), r->query(L, R)); } void set(int L, int R, T x) { if (R <= lo || hi <= L) return; if (L <= lo && hi <= R) { mset = x; val = x; } else { push(), l->set(L, R, x), r->set(L, R, x); val = f(l->val, r->val); } } void push() { if (!l) { int mid = lo + (hi - lo)/2; l = new Node(lo, mid); r = new Node(mid, hi); } if (mset) l->set(lo,hi,mset), r->set(lo,hi,mset), mset = 0; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector> a(n); for (auto &[x,y] : a) cin >> x >> y; sort(begin(a), end(a), [&] (auto x, auto y) {return x[1] < y[1];}); Node *seg = new Node(0, 1'000'000'007); int ans = 0; for (auto [l, r] : a) { int best = seg -> query(0, l+1); ans = max(ans, best + 1); int cur = seg -> query(r, r+1); if (best + 1 > cur) seg -> set(r, r+1, best + 1); } cout << n - ans << '\n'; }