func_code_string: #include <bits/stdc++.h>   using namespace std;   long long ans = 0;   void solve1(vector<int> &ar, bool yeah) {     int sz = ar.size();     int mid = sz / 2 + yeah * (sz % 2) - 1;     int L = mid;     int minL = ar[mid];     int maxL = ar[mid];     int minR = (1 << 30);     int maxR = 0;     for (int R = mid + 1; R < sz; ++R) {       minR = min(minR, ar[R]);       maxR = max(maxR, ar[R]);       while (L > 0 && ar[L - 1] < maxR && ar[L - 1] > minR) {         minL = min(minL, ar[L - 1]);         maxL = max(maxL, ar[--L]);       }       if (maxR > maxL && minR < minL) ans += (L <= (R - maxR + minR));     }   }   void solve2(vector<int> ar, bool yeah) {     int sz = ar.size();     int mid = sz / 2 + yeah * (sz % 2) - 1;     int L = mid;     int minL = ar[mid];     int maxL = ar[mid];     int minR = (1 << 30);     int maxR = 0;     map<int, int> m;     m[minL - L]++;     int dL = L;     for (int R = mid + 1; R < sz; ++R) {       minR = min(minR, ar[R]);       maxR = max(maxR, ar[R]);       while (L > 0 && ar[L - 1] < maxR) {         minL = min(minL, ar[L - 1]);         maxL = max(maxL, ar[--L]);         ar[L] = minL;         m[ar[L] - L]++;       }       while (dL >= L && ar[dL] > minR) {         m[ar[dL] - dL]--;         --dL;       }       if (maxR > maxL) {         ans += m[maxR - R];         int pmr = 0;       }     }   }   int ar[300005];   void go(int a, int b) {     if (a == b) {       ans++;       return;     }     vector<int> V;     for (int i = a; i <= b; ++i) V.push_back(ar[i]);     solve1(V, 1);     reverse(V.begin(), V.end());     solve1(V, 0);     reverse(V.begin(), V.end());     solve2(V, 1);     reverse(V.begin(), V.end());     solve2(V, 0);     go(a, (a + b) / 2);     go((a + b) / 2 + 1, b);   }   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       int a, b;       scanf( %d%d , &a, &b);       ar[a] = b;     }     go(1, n);     cout << ans << endl;   }   