Problem p02874 (C++)

Buggy submission (status: Time Limit Exceeded):
#define y1 askjdkasldjlkasd
#include <bits/stdc++.h>
#undef y1

using namespace std;

#define pb push_back
#define mp make_pair
#define fi(a, b) for (int i = a; i <= b; i++)
#define fj(a, b) for (int j = a; j <= b; j++)
#define fo(a, b) for (int o = a; o <= b; o++)
#define fdi(a, b) for (int i = a; i >= b; i--)
#define fdj(a, b) for (int j = a; j >= b; j--)
#define fdo(a, b) for (int o = a; o >= b; o--)
#define sz(x) (int)x.size()

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<ll> vll;

#ifdef LOCAL
#define err(...) fprintf(stderr, __VA_ARGS__)
#else
#define err(...) while (0)
#endif

double START_TIME;

void exit() {
#ifdef LOCAL
  cerr << "TIME: " << setprecision(5) << fixed
       << (clock() - START_TIME) / CLOCKS_PER_SEC << endl;
#endif
  exit(0);
}

template <typename A, typename B>
ostream &operator<<(ostream &os, pair<A, B> p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  fi(0, sz(v) - 1) { os << v[i] << " "; }
  return os;
}

template <typename T> ostream &operator<<(ostream &os, set<T> t) {
  for (auto z : t) {
    os << z << " ";
  }
  return os;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, map<T1, T2> t) {
  cerr << endl;
  for (auto z : t) {
    os << "\t" << z.first << " -> " << z.second << endl;
  }
  return os;
}

#ifdef LOCAL
#define dbg(x)                                                                 \
  { cerr << __LINE__ << "\t" << #x << ": " << x << endl; }
#define dbg0(x, n)                                                             \
  {                                                                            \
    cerr << __LINE__ << "\t" << #x << ": ";                                    \
    for (int ABC = 0; ABC < n; ABC++)                                          \
      cerr << x[ABC] << ' ';                                                   \
    cerr << endl;                                                              \
  }
#else
#define dbg(x)                                                                 \
  while (0) {                                                                  \
  }
#define dbg0(x, n)                                                             \
  while (0) {                                                                  \
  }
#endif

#ifdef LOCAL
#define ass(x)                                                                 \
  if (!(x)) {                                                                  \
    cerr << __LINE__ << "\tassertion failed: " << #x << endl, abort();         \
  }
#else
#define ass(x) assert(x)
#endif

///////////////////////////////////////////////////

const int MAX = 1e5 + 41;
const int INF = 1e9 + 41;

int n;
int a[MAX];
int b[MAX];
int ans;

int f(int l, int r) { return max(0, r - l + 1); }

int calc1() {
  multiset<int> l, r;
  fi(0, n - 1) {
    l.insert(a[i]);
    r.insert(b[i]);
  }
  int res = 0;
  fi(0, n - 1) {
    l.erase(l.find(a[i]));
    r.erase(r.find(b[i]));
    int v1 = b[i] - a[i] + 1;
    int v2 = max(0, (*r.begin()) - (*l.rbegin()) + 1);
    res = max(res, v1 + v2);
    //
    //		err("l = %d r = %d v1 = %d v2  %d\n", a[i], b[i], v1, v2);
    //
    l.insert(a[i]);
    r.insert(b[i]);
  }
  return res;
}

void solve() {
  fi(0, n - 1) ans = calc1();
  dbg(ans);
  int l = 0, r = 0, p = -1;
  fi(0, n - 1) {
    if (p == -1 || (r > b[i] || (r == b[i] && l < a[i]))) {
      l = a[i];
      r = b[i];
      p = i;
    }
  }
  vi v;
  fi(0, n - 1) {
    if (i != p)
      v.pb(i);
  }
  sort(v.begin(), v.end(), [&](int i, int j) { return a[i] < a[j]; });

  int R1 = r;
  int L2 = -INF;
  int R2 = INF;
  fdi(sz(v) - 1, 0) {
    int cl = a[v[i]];
    int cr = b[v[i]];
    int L1 = (i ? max(a[v[i - 1]], l) : l);
    //
    // err("i = %d L1 = %d R1 = %d L2 = %d R2 = %d cl = %d cr = %d\n", i, L1,
    // R1, L2, R2, cl, cr);
    //
    if (cl > l) {
      L2 = max(L2, cl);
      R2 = min(R2, cr);
      ans = max(ans, f(L1, R1) + f(L2, R2));
    } else {
      ans = max(ans, f(max(L2, cl), min(R2, cr)) + f(L1, R1));
    }
  }
  printf("%d\n", ans);
}

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  START_TIME = (double)clock();
#endif

  scanf("%d", &n);
  fi(0, n - 1) scanf("%d %d", &a[i], &b[i]);
  solve();

  exit();
  return 0;
}


Fixed submission:
#define y1 askjdkasldjlkasd
#include <bits/stdc++.h>
#undef y1

using namespace std;

#define pb push_back
#define mp make_pair
#define fi(a, b) for (int i = a; i <= b; i++)
#define fj(a, b) for (int j = a; j <= b; j++)
#define fo(a, b) for (int o = a; o <= b; o++)
#define fdi(a, b) for (int i = a; i >= b; i--)
#define fdj(a, b) for (int j = a; j >= b; j--)
#define fdo(a, b) for (int o = a; o >= b; o--)
#define sz(x) (int)x.size()

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<ll> vll;

#ifdef LOCAL
#define err(...) fprintf(stderr, __VA_ARGS__)
#else
#define err(...) while (0)
#endif

double START_TIME;

void exit() {
#ifdef LOCAL
  cerr << "TIME: " << setprecision(5) << fixed
       << (clock() - START_TIME) / CLOCKS_PER_SEC << endl;
#endif
  exit(0);
}

template <typename A, typename B>
ostream &operator<<(ostream &os, pair<A, B> p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  fi(0, sz(v) - 1) { os << v[i] << " "; }
  return os;
}

template <typename T> ostream &operator<<(ostream &os, set<T> t) {
  for (auto z : t) {
    os << z << " ";
  }
  return os;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, map<T1, T2> t) {
  cerr << endl;
  for (auto z : t) {
    os << "\t" << z.first << " -> " << z.second << endl;
  }
  return os;
}

#ifdef LOCAL
#define dbg(x)                                                                 \
  { cerr << __LINE__ << "\t" << #x << ": " << x << endl; }
#define dbg0(x, n)                                                             \
  {                                                                            \
    cerr << __LINE__ << "\t" << #x << ": ";                                    \
    for (int ABC = 0; ABC < n; ABC++)                                          \
      cerr << x[ABC] << ' ';                                                   \
    cerr << endl;                                                              \
  }
#else
#define dbg(x)                                                                 \
  while (0) {                                                                  \
  }
#define dbg0(x, n)                                                             \
  while (0) {                                                                  \
  }
#endif

#ifdef LOCAL
#define ass(x)                                                                 \
  if (!(x)) {                                                                  \
    cerr << __LINE__ << "\tassertion failed: " << #x << endl, abort();         \
  }
#else
#define ass(x) assert(x)
#endif

///////////////////////////////////////////////////

const int MAX = 1e5 + 41;
const int INF = 1e9 + 41;

int n;
int a[MAX];
int b[MAX];
int ans;

int f(int l, int r) { return max(0, r - l + 1); }

int calc1() {
  multiset<int> l, r;
  fi(0, n - 1) {
    l.insert(a[i]);
    r.insert(b[i]);
  }
  int res = 0;
  fi(0, n - 1) {
    l.erase(l.find(a[i]));
    r.erase(r.find(b[i]));
    int v1 = b[i] - a[i] + 1;
    int v2 = max(0, (*r.begin()) - (*l.rbegin()) + 1);
    res = max(res, v1 + v2);
    //
    //		err("l = %d r = %d v1 = %d v2  %d\n", a[i], b[i], v1, v2);
    //
    l.insert(a[i]);
    r.insert(b[i]);
  }
  return res;
}

void solve() {
  ans = calc1();
  dbg(ans);
  int l = 0, r = 0, p = -1;
  fi(0, n - 1) {
    if (p == -1 || (r > b[i] || (r == b[i] && l < a[i]))) {
      l = a[i];
      r = b[i];
      p = i;
    }
  }
  vi v;
  fi(0, n - 1) {
    if (i != p)
      v.pb(i);
  }
  sort(v.begin(), v.end(), [&](int i, int j) { return a[i] < a[j]; });

  int R1 = r;
  int L2 = -INF;
  int R2 = INF;
  fdi(sz(v) - 1, 0) {
    int cl = a[v[i]];
    int cr = b[v[i]];
    int L1 = (i ? max(a[v[i - 1]], l) : l);
    //
    // err("i = %d L1 = %d R1 = %d L2 = %d R2 = %d cl = %d cr = %d\n", i, L1,
    // R1, L2, R2, cl, cr);
    //
    if (cl > l) {
      L2 = max(L2, cl);
      R2 = min(R2, cr);
      ans = max(ans, f(L1, R1) + f(L2, R2));
    } else {
      ans = max(ans, f(max(L2, cl), min(R2, cr)) + f(L1, R1));
    }
  }
  printf("%d\n", ans);
}

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  START_TIME = (double)clock();
#endif

  scanf("%d", &n);
  fi(0, n - 1) scanf("%d %d", &a[i], &b[i]);
  solve();

  exit();
  return 0;
}