#include <bits/stdc++.h>   using namespace std;   namespace io {   const int SI = 1 << 21 | 1;   char IB[SI], *IS, *IT, OB[SI], *OS = OB, *OT = OS + SI - 1, c, ch[100];   int f, t;   inline void flush() { fwrite(OB, 1, OS - OB, stdout), OS = OB; }   inline void pc(char x) {     *OS++ = x;     if (OS == OT) flush();   }   template <class I>   inline void rd(I &x) {     for (f = 1, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),                                 IS == IT ? EOF : *IS++)                               : *IS++);          c <  0  || c >  9 ;          c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),                          IS == IT ? EOF : *IS++)                        : *IS++))       if (c ==  - ) f = -1;     for (x = 0; c >=  0  && c <=  9 ; x = (x << 3) + (x << 1) + (c & 15),         c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),                         IS == IT ? EOF : *IS++)                       : *IS++))       ;     x *= f;   }   inline void rds(char *s, int &x) {     for (c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),                          IS == IT ? EOF : *IS++)                        : *IS++);          c < 33 || c > 126;          c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),                          IS == IT ? EOF : *IS++)                        : *IS++))       ;     for (x = 0; c >= 33 && c <= 126;          s[++x] = c, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),                                      IS == IT ? EOF : *IS++)                                    : *IS++))       ;     s[x + 1] =   0 ;   }   template <class I>   inline void print(I x, char k =   n ) {     if (!x) pc( 0 );     if (x < 0) pc( - ), x = -x;     while (x) ch[++t] = x % 10 +  0 , x /= 10;     while (t) pc(ch[t--]);     pc(k);   }   inline void prints(string s) {     int x = s.length();     while (t < x) pc(s[t++]);     pc(  n ), t = 0;   }   struct Flush {     ~Flush() { flush(); }   } flusher;   }  // namespace io   using io::print;   using io::prints;   using io::rd;   using io::rds;   const int N = 3e5 + 7;   int n, a[N], sx[N], tx, sn[N], tn;   map<int, int> p;   struct T {     int l, r, x, c, z;   } t[N << 2];   long long ans;   void build(int p, int l, int r) {     t[p].l = l, t[p].r = r, t[p].c = r - l + 1;     if (l == r) return;     build((p << 1), l, ((t[p].l + t[p].r) >> 1)),         build(((p << 1) | 1), ((t[p].l + t[p].r) >> 1) + 1, r);   }   inline void add(int p, int x) { t[p].x += x, t[p].z += x; }   void upd(int p, int l, int r, int x) {     if (t[p].l >= l && t[p].r <= r) return add(p, x);     if (t[p].z) add((p << 1), t[p].z), add(((p << 1) | 1), t[p].z), t[p].z = 0;     if (l <= ((t[p].l + t[p].r) >> 1)) upd((p << 1), l, r, x);     if (r > ((t[p].l + t[p].r) >> 1)) upd(((p << 1) | 1), l, r, x);     t[p].x = min(t[(p << 1)].x, t[((p << 1) | 1)].x);     t[p].c = (t[(p << 1)].x == t[p].x ? t[(p << 1)].c : 0) +              (t[((p << 1) | 1)].x == t[p].x ? t[((p << 1) | 1)].c : 0);   }   int main() {     rd(n);     for (int i = 1, x, y; i <= n; i++) rd(x), rd(y), a[x] = y;     build(1, 1, n);     for (int i = 1; i <= n; i++) {       while (tx && a[sx[tx]] < a[i])         upd(1, sx[tx - 1] + 1, sx[tx], -a[sx[tx]]), --tx;       while (tn && a[sn[tn]] > a[i])         upd(1, sn[tn - 1] + 1, sn[tn], a[sn[tn]]), --tn;       upd(1, p[a[i]] + 1, i, -1), p[a[i]] = sx[++tx] = sn[++tn] = i;       upd(1, sx[tx - 1] + 1, i, a[i]), upd(1, sn[tn - 1] + 1, i, -a[i]);       ans += t[1].c;     }     print(ans);     return 0;   }   