#include <bits/stdc++.h>   using namespace std;   namespace Quick_Function {   template <typename Temp>   void Read(Temp &x) {     x = 0;     char ch = getchar();     bool op = 0;     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) op = 1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = (x << 1) + (x << 3) + (ch ^ 48);       ch = getchar();     }     if (op) x = -x;   }   template <typename T, typename... Args>   void Read(T &t, Args &...args) {     Read(t);     Read(args...);   }   template <typename Temp>   Temp Max(Temp x, Temp y) {     return x > y ? x : y;   }   template <typename Temp>   Temp Min(Temp x, Temp y) {     return x < y ? x : y;   }   template <typename Temp>   Temp Abs(Temp x) {     return x < 0 ? (-x) : x;   }   template <typename Temp>   void Swap(Temp &x, Temp &y) {     x ^= y ^= x ^= y;   }   }  // namespace Quick_Function   using namespace Quick_Function;   const int MAXN = 5e6 + 5;   bool vis[MAXN];   int n, ans;   struct Splay_Node {     int son[2], fa, val, tag;   };   struct Splay_Tree {     Splay_Node t[MAXN];     int root, tot, Top, stk[MAXN];     int Ident(int pos) { return t[t[pos].fa].son[1] == pos ? 1 : 0; }     void Connect(int pos, int fa, int flag) {       t[fa].son[flag] = pos, t[pos].fa = fa;     }     void Push_Down(int pos) {       if (!t[pos].tag) return;       if (t[pos].son[0]) {         t[t[pos].son[0]].val += t[pos].tag, t[t[pos].son[0]].tag += t[pos].tag;       }       if (t[pos].son[1]) {         t[t[pos].son[1]].val += t[pos].tag, t[t[pos].son[1]].tag += t[pos].tag;       }       t[pos].tag = 0;     }     int New(int val, int fa) {       t[++tot].fa = fa, t[tot].val = val;       return tot;     }     void Build() {       root = New(-0x3f3f3f3f, 0);       t[root].son[1] = New(0x3f3f3f3f, root);     }     void Rotate(int pos) {       int fa = t[pos].fa, grand = t[fa].fa;       int flag1 = Ident(pos), flag2 = Ident(fa);       Connect(pos, grand, flag2);       Connect(t[pos].son[flag1 ^ 1], fa, flag1);       Connect(fa, pos, flag1 ^ 1);     }     void Splay(int pos, int to) {       int tmp = pos;       Top = 0;       stk[++Top] = tmp;       while (tmp) stk[++Top] = tmp = t[tmp].fa;       while (Top) Push_Down(stk[Top--]);       for (int fa = t[pos].fa; t[pos].fa != to; Rotate(pos), fa = t[pos].fa)         if (t[fa].fa != to) Ident(pos) == Ident(fa) ? Rotate(fa) : Rotate(pos);       if (!to) root = pos;     }     void Insert(int &pos, int val, int fa) {       if (!pos) {         ++ans;         pos = New(val, fa);         Splay(pos, 0);         return;       }       Push_Down(pos);       if (val < t[pos].val)         Insert(t[pos].son[0], val, pos);       else         Insert(t[pos].son[1], val, pos);     }     void Erase(int pos) {       Splay(pos, 0);       vis[pos] = 1;       int l = t[pos].son[0], r = t[pos].son[1];       while (t[l].son[1]) l = t[l].son[1];       while (t[r].son[0]) r = t[r].son[0];       Splay(l, 0);       Splay(r, l);       t[r].son[0] = 0;       --ans;     }     int Get_Pre(int val) {       int pos, res, newroot;       pos = newroot = root;       while (pos) {         Push_Down(pos);         if (t[pos].val < val) {           res = pos;           pos = t[pos].son[1];         } else           pos = t[pos].son[0];       }       Splay(newroot, 0);       return res;     }     int Get_Nxt(int val) {       int pos, res, newroot;       pos = newroot = root;       while (pos) {         Push_Down(pos);         if (t[pos].val > val) {           res = pos;           pos = t[pos].son[0];         } else           pos = t[pos].son[1];       }       Splay(newroot, 0);       return res;     }     void Move(int l, int r) {       int u = Get_Nxt(l - 1), v = Get_Pre(r);       if (t[u].val > t[v].val) return;       if (u == v)         t[u].val++;       else if (t[u].val < t[v].val) {         Splay(u, 0);         Splay(v, u);         int rson = t[v].son[0];         ++t[u].val;         ++t[v].val, t[rson].val++;         if (rson) ++t[rson].tag;       }     }     void Body() {       int pos = rand() % tot + 1;       if (!vis[pos]) {         Splay(pos, 0);         return;       }       pos = rand() % tot + 1;       if (!vis[pos]) {         Splay(pos, 0);         return;       }       pos = rand() % tot + 1;       if (!vis[pos]) {         Splay(pos, 0);         return;       }       pos = rand() % tot + 1;       if (!vis[pos]) {         Splay(pos, 0);         return;       }       pos = rand() % tot + 1;       if (!vis[pos]) {         Splay(pos, 0);         return;       }     }   };   Splay_Tree tree;   int main() {     srand(time(0));     Read(n);     tree.Build();     tree.Insert(tree.root, 0, 0);     ans = 0;     for (int i = 1, l, r; i <= n; i++) {       Read(l, r);       int pos = tree.Get_Nxt(r - 1);       if (pos && pos != 1 && pos != 2) tree.Erase(pos);       tree.Move(l, r);       tree.Insert(tree.root, l, 0);       tree.Body();     }     printf( %d , ans);     return 0;   }   