func_code_string: #include <bits/stdc++.h>   using namespace std;   const int N = 300005;   struct Tree {     int l, r, num1;     long long num2;   } T[N * 4];   struct Ask {     int l, r, col;     Ask(int _l = 0, int _r = 0, int _col = 0) {       l = _l;       r = _r;       col = _col;     }     inline bool operator<(const Ask &b) const {       return l < b.l || (b.l == l && r < b.r);     }   };   set<Ask> S;   set<int> S2;   vector<int> v1[N << 1], v2[N << 1];   vector<pair<int, int> > A[N], B[N];   int l[N], r[N], a[N << 1], tot, n, k, R[N], L[N];   long long now, ppp;   inline int pd(int x) {     long long now = 0, cnt = 0;     for (int i = 1, j = 0; i <= n; i++) {       if (i != 1) {         for (pair<int, int> k : A[i - 1])           if (k.first > j) now -= k.second;       }       while (now < x) {         j++;         for (pair<int, int> k : B[j])           if (k.first < i) now += k.second;       }       cnt += n - j + 1;       R[i] = j;     }     ppp = cnt - k;     return cnt >= k;   }   inline int ef(int x) {     int l = 1, r = tot;     while (l < r) {       int mid = (l + r) / 2;       if (a[mid] < x)         l = mid + 1;       else         r = mid;     }     return l;   }   inline void build(int x, int l, int r) {     T[x].l = l;     T[x].r = r;     if (l == r) return;     int mid = (l + r) / 2;     build(x * 2, l, mid);     build(x * 2 + 1, mid + 1, r);   }   inline void insert(int x, int y, int z) {     T[x].num1++;     T[x].num2 += z;     if (T[x].l == T[x].r) return;     int mid = (T[x].l + T[x].r) / 2;     if (y <= mid)       insert(x * 2, y, z);     else       insert(x * 2 + 1, y, z);   }   inline long long find(int x, int l, int r, long long z) {     if (T[x].l > r || l > T[x].r) return 0;     if (T[x].l >= l && T[x].r <= r) return T[x].num1 * z - T[x].num2;     return find(x * 2, l, r, z) + find(x * 2 + 1, l, r, z);   }   inline long long solve(int l, int r) {     l++;     return find(1, l, L[r], r);   }   inline void Insert(int x) {     set<int>::iterator iter = S2.upper_bound(x), iter2 = iter;     iter2--;     now -= solve(*iter2, *iter);     now += solve(*iter2, x);     now += solve(x, *iter);     S2.insert(x);   }   inline void Del(int x) {     set<int>::iterator iter = S2.find(x), iter2 = iter;     iter2--;     iter++;     now += solve(*iter2, *iter);     now -= solve(*iter2, x);     now -= solve(x, *iter);     S2.erase(x);   }   signed main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d%d , &l[i], &r[i]), r[i]--;     l[n + 1] = 1;     r[n + 1] = 1e9;     S.insert(Ask(1, 1e9, 0));     for (int i = 1; i <= n + 1; i++) {       set<Ask>::iterator iter = S.upper_bound(Ask(l[i], 1e9, 0));       iter--;       vector<Ask> add;       add.push_back(Ask(l[i], r[i], i));       for (; iter != S.end() && (*iter).l <= r[i];) {         int len = min(r[i], (*iter).r) - max(l[i], (*iter).l) + 1;         if ((*iter).col != 0) A[(*iter).col].push_back(make_pair(i, len));         B[i].push_back(make_pair((*iter).col, len));         if ((*iter).l < l[i])           add.push_back(Ask((*iter).l, l[i] - 1, (*iter).col));         if ((*iter).r > r[i])           add.push_back(Ask(r[i] + 1, (*iter).r, (*iter).col));         S.erase(iter++);       }       for (Ask j : add) S.insert(j);     }     int lll = 0, rrr = 1e9;     while (lll < rrr) {       int mid = (lll + rrr + 1) / 2;       if (pd(mid))         lll = mid;       else         rrr = mid - 1;     }     pd(lll);     for (int i = 1; i <= n; i++) L[R[i]] = i;     for (int i = 1; i <= n + 1; i++) L[i] = max(L[i - 1], L[i]);     for (int i = 1; i <= n; i++) {       r[i]++;       a[++tot] = l[i];       a[++tot] = r[i];     }     sort(a + 1, a + tot + 1);     int num = 1;     for (int i = 2; i <= tot; i++)       if (a[i] != a[num]) a[++num] = a[i];     tot = num;     for (int i = 1; i <= n; i++)       l[i] = ef(l[i]), r[i] = ef(r[i]), v1[l[i]].push_back(i),       v2[r[i]].push_back(i);     long long ans = 0;     k = ppp;     now = k;     S2.insert(0);     S2.insert(n + 1);     build(1, 1, n);     for (int i = 1; i <= n; i++) insert(1, i, R[i]);     for (int i = 1; i < tot; i++) {       for (int j : v1[i]) Insert(j);       for (int j : v2[i]) Del(j);       ans += (long long)(k - now) * (a[i + 1] - a[i]);     }     printf( %lld n , ans - ppp * lll);     return 0;   }   