Language: C++

#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>

using namespace std;

typedef long long ll;

#ifdef iq
  mt19937 rnd(228);
#else
  mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif

int main() {
#ifdef iq
  freopen("a.in", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector <pair <int, int> > e;
  for (int i = 0; i < n; i++) {
    int l, r;
    cin >> l >> r;
    e.push_back({l, r});
  }
  sort(e.begin(), e.end(), [] (pair <int, int> a, pair <int, int> b) {
    if (a.first == b.first) {
      return a.second > b.second;
    } else {
      return a.first < b.first;
    }
  });
  vector <pair <int, int> > st;
  vector <pair <int, int> > ot;
  int vl = 0, vr = 1e9;
  for (auto c : e) {
    if (st.empty() || c.second > st.back().second) {
      st.push_back(c);
    } else {
      vl = max(vl, c.first);
      vr = min(vr, c.second);
    }
  }
  int ans = 0;
  auto f = [&] (int i, int j) {
    int a = vl, b = vr;
    int ml = st[j].first;
    int mr = st[i].second;
    if (i) {
      a = max(a, st[i - 1].first);
      b = min(b, st[0].second);
    }
    if (j != (int) st.size() - 1) {
      a = max(a, st.back().first);
      b = min(b, st[i + 1].second);
    }
    if (a) {
      return max(0, mr - ml + 1) + max(0, b - a + 1);
    } else {
      return 0;
    }
  };
  function <void(int, int, int, int)> solve = [&] (int l, int r, int opt_l, int opt_r) {
    if (l >= r) return;
    if (r - l == 1) {
      for (int i = max(l, opt_l); i <= opt_r; i++)
        ans = max(ans, f(l, i));
    } else {
      int m = (l + r) / 2;
      int opt = -1;
      for (int i = opt_l; i <= opt_r; i++) {
        if (i >= l && (opt == -1 || f(m, i) > f(m, opt))) {
          opt = i;
        }
      }
      ans = max(ans, f(m, opt));
      solve(l, m, opt_l, opt);
      solve(m + 1, r, opt, opt_r);
    }
  };
  /*
  for (int i = 0; i < (int) st.size(); i++) {
    for (int j = i; j < (int) st.size(); j++) {
      int a = vl, b = vr;
      int ml = st[j].first;
      int mr = st[i].second;
      if (i) {
        a = max(a, st[i - 1].first);
        b = min(b, st[0].second);
      }
      if (j != (int) st.size() - 1) {
        a = max(a, st.back().first);
        b = min(b, st[i + 1].second);
      }
      if (a) {
        ans = max(ans, max(0, mr - ml + 1) + max(0, b - a + 1));
      }
    }
  }
  */
  solve(0, n, 0, n - 1);
  cout << ans << '\n';
}


Compiler output:
a.cc: In function 'int main()':
a.cc:84:3: error: 'function' was not declared in this scope
   84 |   function <void(int, int, int, int)> solve = [&] (int l, int r, int opt_l, int opt_r) {
      |   ^~~~~~~~
a.cc:22:1: note: 'std::function' is defined in header '<functional>'; this is probably fixable by adding '#include <functional>'
   21 | #include <cstring>
  +++ |+#include <functional>
   22 | 
a.cc:84:36: error: expression list treated as compound expression in functional cast [-fpermissive]
   84 |   function <void(int, int, int, int)> solve = [&] (int l, int r, int opt_l, int opt_r) {
      |                                    ^
a.cc:84:13: error: expected primary-expression before 'void'
   84 |   function <void(int, int, int, int)> solve = [&] (int l, int r, int opt_l, int opt_r) {
      |             ^~~~
a.cc:122:3: error: 'solve' was not declared in this scope
  122 |   solve(0, n, 0, n - 1);
      |   ^~~~~