Problem p02874 (C++)

Buggy submission (status: Runtime Error):
#if __has_include("../library/Basic/Debug.hpp")

#include "../library/Basic/Debug.hpp"

#else

/* ----- Header Files ----- */
// IO
#include <cstdio>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm
#include <algorithm>
#include <cmath>
#include <numeric>

// container
#include <array>
#include <bitset>
#include <complex>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

// others
#include <cassert>
#include <cstdint>
#include <ctime>
#include <functional>
#include <limits>
#include <random>

/* ----- Type Alias ----- */
using lint = long long int;
using ldouble = long double;
template <class T> using MaxHeap = std::priority_queue<T>;
template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <class T> T ndvec(T v) { return v; }

template <class T, class... Ts> auto ndvec(size_t l, Ts... ts) {
  return std::vector<decltype(ndvec<T>(ts...))>(l, ndvec<T>(ts...));
}

template <class Cost = int> struct Edge {
  int src, dst;
  Cost cost;
  Edge(int src = -1, int dst = -1, Cost cost = 1)
      : src(src), dst(dst), cost(cost){};

  bool operator<(const Edge<Cost> &e) const { return this->cost < e.cost; }
  bool operator>(const Edge<Cost> &e) const { return this->cost > e.cost; }
};

template <class Cost = int> using Edges = std::vector<Edge<Cost>>;
template <class Cost = int> using Graph = std::vector<std::vector<Edge<Cost>>>;

#endif

/* ----- Misc ----- */
void fastio() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
}

struct Fout {
  int precision;
  explicit Fout(int precision) : precision(precision) {}
};

std::ostream &operator<<(std::ostream &os, const Fout &fio) {
  os << std::fixed << std::setprecision(fio.precision);
  return os;
}

/* ----- Constants ----- */
// constexpr int INF = std::numeric_limits<int>::max() / 3;
// constexpr int MOD = 1000000007;
// const Real PI = acos(-1);
// constexpr Real EPS = 1e-10;
// std::mt19937 mt(int(std::time(nullptr)));

struct Seg {
  int l, r;
  int len() { return std::max(0, r - l + 1); }
};

int main() {
  int n;
  std::cin >> n;

  std::vector<Seg> segs(n);
  for (auto &s : segs) {
    std::cin >> s.l >> s.r;
  }

  std::sort(segs.begin(), segs.end(), [](const Seg &a, const Seg &b) {
    return a.r != b.r ? a.r < b.r : a.l < b.l;
  });
  // r最小，l最小を持ってくる
  auto base = segs.front();
  segs.erase(segs.begin());
  --n;

  std::sort(segs.begin(), segs.end(), [](const Seg &a, const Seg &b) {
    return a.l != b.l ? a.l < b.l : a.r < b.r;
  });

  std::vector<int> back(n);
  Seg backs;
  backs.l = 0, backs.r = 1e9;
  back[0] = backs.len();
  for (int i = 1; i <= n; ++i) {
    auto seg = segs[n - i];
    backs.l = std::max(backs.l, seg.l);
    backs.r = std::min(backs.r, seg.r);
    back[i] = backs.len();
  }

  int ans = 0;
  Seg fseg = base;
  for (int i = 0; i < n; ++i) {
    int f = fseg.len();
    int b = back[n - i];
    ans = std::max(ans, f + b);

    fseg.l = std::max(fseg.l, segs[i].l);
    fseg.r = std::min(fseg.r, segs[i].r);
  }

  fseg = base;
  fseg.l = std::max(fseg.l, segs.back().l);
  fseg.r = std::min(fseg.r, segs.back().r);

  for (int i = 0; i < n - 1; ++i) {
    auto seg = segs[i];
    ans = std::max(ans, seg.len() + fseg.len());
  }

  std::cout << ans << std::endl;
  return 0;
}


Fixed submission:
#if __has_include("../library/Basic/Debug.hpp")

#include "../library/Basic/Debug.hpp"

#else

/* ----- Header Files ----- */
// IO
#include <cstdio>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm
#include <algorithm>
#include <cmath>
#include <numeric>

// container
#include <array>
#include <bitset>
#include <complex>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

// others
#include <cassert>
#include <cstdint>
#include <ctime>
#include <functional>
#include <limits>
#include <random>

/* ----- Type Alias ----- */
using lint = long long int;
using ldouble = long double;
template <class T> using MaxHeap = std::priority_queue<T>;
template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <class T> T ndvec(T v) { return v; }

template <class T, class... Ts> auto ndvec(size_t l, Ts... ts) {
  return std::vector<decltype(ndvec<T>(ts...))>(l, ndvec<T>(ts...));
}

template <class Cost = int> struct Edge {
  int src, dst;
  Cost cost;
  Edge(int src = -1, int dst = -1, Cost cost = 1)
      : src(src), dst(dst), cost(cost){};

  bool operator<(const Edge<Cost> &e) const { return this->cost < e.cost; }
  bool operator>(const Edge<Cost> &e) const { return this->cost > e.cost; }
};

template <class Cost = int> using Edges = std::vector<Edge<Cost>>;
template <class Cost = int> using Graph = std::vector<std::vector<Edge<Cost>>>;

#endif

/* ----- Misc ----- */
void fastio() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
}

struct Fout {
  int precision;
  explicit Fout(int precision) : precision(precision) {}
};

std::ostream &operator<<(std::ostream &os, const Fout &fio) {
  os << std::fixed << std::setprecision(fio.precision);
  return os;
}

/* ----- Constants ----- */
// constexpr int INF = std::numeric_limits<int>::max() / 3;
// constexpr int MOD = 1000000007;
// const Real PI = acos(-1);
// constexpr Real EPS = 1e-10;
// std::mt19937 mt(int(std::time(nullptr)));

struct Seg {
  int l, r;
  int len() { return std::max(0, r - l + 1); }
};

int main() {
  int n;
  std::cin >> n;

  std::vector<Seg> segs(n);
  for (auto &s : segs) {
    std::cin >> s.l >> s.r;
  }

  std::sort(segs.begin(), segs.end(), [](const Seg &a, const Seg &b) {
    return a.r != b.r ? a.r < b.r : a.l < b.l;
  });
  // r最小，l最小を持ってくる
  auto base = segs.front();
  segs.erase(segs.begin());
  --n;

  std::sort(segs.begin(), segs.end(), [](const Seg &a, const Seg &b) {
    return a.l != b.l ? a.l < b.l : a.r < b.r;
  });

  std::vector<int> back(n + 1);
  Seg backs;
  backs.l = 0, backs.r = 1e9;
  back[0] = backs.len();
  for (int i = 1; i <= n; ++i) {
    auto seg = segs[n - i];
    backs.l = std::max(backs.l, seg.l);
    backs.r = std::min(backs.r, seg.r);
    back[i] = backs.len();
  }

  int ans = 0;
  Seg fseg = base;
  for (int i = 0; i < n; ++i) {
    int f = fseg.len();
    int b = back[n - i];
    ans = std::max(ans, f + b);

    fseg.l = std::max(fseg.l, segs[i].l);
    fseg.r = std::min(fseg.r, segs[i].r);
  }

  fseg = base;
  fseg.l = std::max(fseg.l, segs.back().l);
  fseg.r = std::min(fseg.r, segs.back().r);

  for (int i = 0; i < n - 1; ++i) {
    auto seg = segs[i];
    ans = std::max(ans, seg.len() + fseg.len());
  }

  std::cout << ans << std::endl;
  return 0;
}