func_code_string: #include <bits/stdc++.h>   using namespace std;   const int MAXN = 600010;   struct wl {     int k, l, pos;   };   wl w[MAXN];   wl temp[MAXN];   int n;   int p[MAXN];   int cnt, tcnt;   int *t;   int x[MAXN], y[MAXN];   int find(int p) {     int l = 0, r = tcnt;     while (l < r) {       int mid = (l + r) / 2;       if (p > t[mid])         l = mid + 1;       else         r = mid;     }     return l;   }   void init() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d%d , &x[i], &y[i]);       p[cnt++] = x[i];       p[cnt++] = y[i];     }     sort(p, p + cnt);     int new_cnt = 0;     new_cnt = unique(p, p + cnt) - p;     t = p;     cnt = new_cnt;     tcnt = cnt;     new_cnt = 0;     w[new_cnt].k = 0;     w[new_cnt++].l = t[0] - 1;     for (int i = 0; i < cnt - 1; i++) {       w[new_cnt].k = t[i];       w[new_cnt++].l = 1;       w[new_cnt].k = t[i] + 1;       w[new_cnt++].l = t[i + 1] - t[i] - 1;     }     w[new_cnt].k = t[cnt - 1];     w[new_cnt++].l = 1;     cnt = new_cnt;     for (int i = 0; i < n; i++) {       int xx = find(x[i]) + 1, yy = find(y[i]) + 1;       swap(w[(xx - 1) * 2 + 1], w[(yy - 1) * 2 + 1]);     }   }   long long ans = 0;   long long sum[MAXN];   void solve(int l, int r) {     if (l == r) return;     int p1 = l, p2, p = l;     int mid = (l + r) / 2;     p2 = mid + 1;     solve(l, mid);     solve(mid + 1, r);     sum[l] = w[l].l;     for (int i = l + 1; i <= mid; i++) sum[i] = sum[i - 1] + w[i].l;     while (p1 <= mid && p2 <= r) {       if (w[p1].k <= w[p2].k) {         temp[p++] = w[p1++];         continue;       }       if (w[p1].k > w[p2].k) {         temp[p] = w[p2];         ans += (sum[mid] - sum[p1] + (long long)w[p1].l) * (long long)(w[p2].l);         p2++;         p++;       }     }     for (int i = p2; i <= r; i++) temp[p++] = w[i];     for (int i = p1; i <= mid; i++) temp[p++] = w[i];     for (int i = l; i <= r; i++) w[i] = temp[i];     return;   }   int main() {     init();     solve(0, cnt - 1);     cout << ans << endl;     return 0;   }   