func_code_string: #include <bits/stdc++.h>   using namespace std;   template <class T>   inline void read(T &x) {     char ch;     bool flag = false;     while (!isdigit(ch = getchar())) flag |= ch ==  - ;     for (x = ch ^ 48; isdigit(ch = getchar()); x = x * 10 + (ch ^ 48))       ;     if (flag) x = -x;   }   inline int input() {     int x;     char ch;     bool flag = false;     while (!isdigit(ch = getchar())) flag |= ch ==  - ;     for (x = ch ^ 48; isdigit(ch = getchar()); x = x * 10 + (ch ^ 48))       ;     return flag ? -x : x;   }   const int mod = 1e9 + 7;   inline int add(int x, int y) {     x += y;     return x >= mod ? x - mod : x;   }   inline void inc(int &x, int y) {     x += y;     x -= x >= mod ? mod : 0;   }   inline int Power(int x, int y) {     int res = 1;     while (y) {       if (y & 1) res = (long long)res * x % mod;       x = (long long)x * x % mod, y >>= 1;     }     return res;   }   template <class T>   void chkmax(T &x, T y) {     x = x > y ? x : y;   }   template <class T>   void chkmin(T &x, T y) {     x = x < y ? x : y;   }   template <class T>   T gcd(T x, T y) {     return !y ? x : gcd(x % y, y);   }   int n, a[300005];   int cnt[300005 << 1];   int lmax[300005], lmin[300005], rmax[300005], rmin[300005];   long long ans;   void divide(int l, int r) {     if (l == r) return void();     int mid = l + r >> 1;     divide(l, mid), divide(mid + 1, r);     rmin[mid] = INT_MAX, rmax[mid] = 0;     lmin[mid + 1] = INT_MAX, lmax[mid + 1] = 0;     for (int i = mid; i >= l; --i) {       lmin[i] = min(lmin[i + 1], a[i]);       lmax[i] = max(lmax[i + 1], a[i]);     }     for (int i = mid + 1; i <= r; ++i) {       rmin[i] = min(rmin[i - 1], a[i]);       rmax[i] = max(rmax[i - 1], a[i]);     }     for (int i = mid, lp = mid + 1, rp = mid + 1; i >= l; --i) {       int pos = lmax[i] - lmin[i] + i;       if (pos > mid && pos <= r && rmin[pos] > lmin[i] && rmax[pos] < lmax[i])         ++ans;       while (lp <= r && rmax[lp] < lmax[i]) --cnt[300005 + rmax[lp] - lp], ++lp;       while (rp <= r && rmin[rp] > lmin[i]) ++cnt[300005 + rmax[rp] - rp], ++rp;       if (lp < rp) ans += cnt[300005 + lmin[i] - i];     }     for (int i = mid + 1; i <= r; ++i) cnt[300005 + rmax[i] - i] = 0;     for (int i = mid + 1, lp = mid, rp = mid; i <= r; ++i) {       int pos = rmin[i] - rmax[i] + i;       if (pos >= l && pos <= mid && lmin[pos] > rmin[i] && lmax[pos] < rmax[i])         ++ans;       while (lp >= l && lmin[lp] > rmin[i]) ++cnt[lmax[lp] + lp], --lp;       while (rp >= l && lmax[rp] < rmax[i]) --cnt[lmax[rp] + rp], --rp;       if (lp < rp) ans += cnt[rmin[i] + i];     }     for (int i = l; i <= mid; ++i) cnt[lmax[i] + i] = 0;   }   int main() {     ios::sync_with_stdio(false);     cin >> n, ans = n;     for (int i = 1; i <= n; ++i) {       int x, y;       cin >> x >> y;       a[x] = y;     }     divide(1, n);     printf( %lld n , ans);     return 0;   }   