func_code_string: #include <bits/stdc++.h>   using namespace std;   int maxl[300005][20], minl[300005][20];   int vis[2 * 300005];   int a[300005];   int lmax[300005], lmin[300005], rmax[300005], rmin[300005];   int min(int a, int b) {     if (a > b) return b;     return a;   }   int max(int a, int b) {     if (a > b) return a;     return b;   }   void S_table(int n) {     int l = int(log((double)n) / log(2.0));     for (int j = 1; j <= l; j++) {       for (int i = 1; i + (1 << (j - 1)) - 1 <= n; ++i) {         maxl[i][j] = max(maxl[i][j - 1], maxl[i + (1 << (j - 1))][j - 1]);         minl[i][j] = min(minl[i][j - 1], minl[i + (1 << (j - 1))][j - 1]);       }     }   }   int w[300005];   int rmq(int l, int r) {     int k = w[r - l + 1];     int a1 = max(maxl[l][k], maxl[r - (1 << k) + 1][k]);     return a1;   }   void init(void) {     int i;     for (i = 1; i <= 300002; i++) {       int k = int(log((double)(i)) / log(2.0));       w[i] = k;     }   }   int rmq1(int l, int r) {     int k = w[r - l + 1];     int a2 = min(minl[l][k], minl[r - (1 << k) + 1][k]);     return a2;   }   struct pi {     int a, b;   } pp[300005];   int cmp(pi a, pi b) { return a.a < b.a; }   long long dfs(int x, int y) {     if (x == y) return 1;     if (y - x == 1) {       if (abs(pp[y].b - pp[x].b) == 1) return 3;       return 2;     }     long long s = 0;     s = dfs(x, (x + y) / 2);     s += dfs((x + y) / 2 + 1, y);     int mid = (x + y) / 2;     int i;     lmin[mid + 1] = 1000000000;     lmax[mid + 1] = -1000000000;     for (i = mid; i >= x; i--) {       lmin[i] = min(lmin[i + 1], pp[i].b);       lmax[i] = max(lmax[i + 1], pp[i].b);     }     rmin[mid] = 1000000000;     rmax[mid] = -1000000000;     for (i = mid + 1; i <= y; i++) {       rmin[i] = min(rmin[i - 1], pp[i].b);       rmax[i] = max(rmax[i - 1], pp[i].b);     }     for (i = mid; i >= x; i--) {       int a, b;       a = lmax[i];       b = lmin[i];       if (a - b + 1 > mid - i + 1) {         int p = a - b + 1 - (mid - i + 1);         if (p <= y - mid) {           if (rmax[mid + p] < a && rmin[mid + p] > b) {             s++;           }         }       }     }     for (i = mid + 1; i <= y; i++) {       int a, b;       a = rmax[i];       b = rmin[i];       if (a - b + 1 > i - mid) {         int p = a - b + 1 - (i - mid);         if (p <= mid - x + 1) {           if (lmax[mid - p + 1] < a && lmin[mid - p + 1] > b) {             s++;           }         }       }     }     int ss, tt;     ss = mid + 1;     tt = mid + 1;     for (i = mid; i >= x; i--) {       int a = lmax[i];       int b = lmin[i];       while (tt <= y && rmin[tt] >= b) {         vis[rmax[tt] - tt + 300005]++;         tt++;       }       while (ss <= y && rmax[ss] < a) {         vis[rmax[ss] - ss + 300005]--;         ss++;       }       if (tt <= ss) continue;       int p = vis[lmin[i] - i + 300005];       if (p > 0) s += p;     }     ss = mid;     tt = mid;     for (i = mid + 1; i <= y; i++) {       vis[rmax[i] - i + 300005] = 0;     }     for (i = mid + 1; i <= y; i++) {       int a = rmax[i];       int b = rmin[i];       while (tt >= x && lmin[tt] >= b) {         vis[lmax[tt] + tt]++;         tt--;       }       while (ss >= x && lmax[ss] < a) {         vis[lmax[ss] + ss]--;         ss--;       }       if (tt >= ss) continue;       int p = vis[rmin[i] + i];       if (p > 0) s += p;     }     for (i = x; i <= mid; i++) {       vis[lmax[i] + i] = 0;     }     return s;   }   int main() {     int i, n;     init();     cin >> n;     for (i = 1; i <= n; i++) {       int a, b;       scanf( %d%d , &a, &b);       pp[a].b = b;     }     for (i = 1; i <= n; i++) {       maxl[i][0] = pp[i].b;       minl[i][0] = pp[i].b;     }     S_table(n);     cout << dfs(1, n) << endl;   }   