func_code_string: #include <bits/stdc++.h>   using namespace std;   int a[300010];   struct _ {     int val, rpos;   };   vector<_> min_, max_;   int vmin[1200010], cmin[1200010], add[1200010];   void pushup(int rt) {     if (vmin[rt << 1] == vmin[rt << 1 | 1]) {       vmin[rt] = vmin[rt << 1];       cmin[rt] = cmin[rt << 1] + cmin[rt << 1 | 1];     } else {       if (vmin[rt << 1] < vmin[rt << 1 | 1]) {         vmin[rt] = vmin[rt << 1];         cmin[rt] = cmin[rt << 1];       } else {         vmin[rt] = vmin[rt << 1 | 1];         cmin[rt] = cmin[rt << 1 | 1];       }     }   }   void build(int l, int r, int rt) {     if (l == r) {       cmin[rt] = 1;       return;     }     int m = l + r >> 1;     build(l, m, rt << 1);     build(m + 1, r, rt << 1 | 1);     pushup(rt);   }   void pushdown(int rt) {     if (add[rt]) {       add[rt << 1] += add[rt];       vmin[rt << 1] += add[rt];       add[rt << 1 | 1] += add[rt];       vmin[rt << 1 | 1] += add[rt];       add[rt] = 0;       return;     }   }   void update(int L, int R, int c, int l, int r, int rt) {     if (L <= l && r <= R) {       add[rt] += c;       vmin[rt] += c;       return;     }     int m = l + r >> 1;     pushdown(rt);     if (m >= L) update(L, R, c, l, m, rt << 1);     if (m < R) update(L, R, c, m + 1, r, rt << 1 | 1);     pushup(rt);   }   struct __ {     int v, c;     __ operator+(const __ &ob) const {       __ rv;       if (v == ob.v) {         rv.v = v;         rv.c = c + ob.c;       } else {         if (v < ob.v) {           rv.v = v;           rv.c = c;         } else {           rv.v = ob.v;           rv.c = ob.c;         }       }       return rv;     }   };   __ query(int L, int R, int l, int r, int rt) {     if (L <= l && r <= R) return (__){vmin[rt], cmin[rt]};     int m = l + r >> 1;     pushdown(rt);     __ ans;     ans.v = 998244353;     if (m >= L) ans = ans + query(L, R, l, m, rt << 1);     if (m < R) ans = ans + query(L, R, m + 1, r, rt << 1 | 1);     return ans;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     build(1, n, 1);     int i;     int x, y;     for (i = 1; i <= n; ++i) {       cin >> x >> y;       a[x] = y;     }     min_.push_back((_){-1, 0});     max_.push_back((_){n + 1, 0});     long long ans = 0;     for (i = 1; i <= n; ++i) {       while (min_.back().val >= a[i]) {         update(min_[min_.size() - 2].rpos + 1, min_.back().rpos, min_.back().val,                1, n, 1);         min_.pop_back();       }       update(min_.back().rpos + 1, i, -a[i], 1, n, 1);       min_.push_back((_){a[i], i});       while (max_.back().val <= a[i]) {         update(max_[max_.size() - 2].rpos + 1, max_.back().rpos, -max_.back().val,                1, n, 1);         max_.pop_back();       }       update(max_.back().rpos + 1, i, a[i], 1, n, 1);       max_.push_back((_){a[i], i});       update(1, i, -1, 1, n, 1);       ans += query(1, i, 1, n, 1).c;     }     cout << ans <<   n ;     return 0;   }   