Problem p02661 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';

ll solve(int n, const vector<ll> &a, const vector<ll> &b, int num) {
  ll ng = 0, ok = 1e9 + 1;
  while (ok - ng > 1) {
    ll mid = (ok + ng) / 2;

    int le = 0, cnt = 0;
    for (int i = 0; i < n; i++) {
      if (b[i] < mid)
        ++le;
      else if (mid >= a[i])
        ++cnt;
    }

    if (num <= le + cnt) {
      ok = mid;
    } else {
      ng = mid;
    }
    // cerr << ok << "," << ng << newl;
  }
  return ok;
}

ll solve2(int n, const vector<ll> &a, const vector<ll> &b, int num) {
  ll ok = 0, ng = 1e9 + 1;
  while (ng - ok > 1) {
    ll mid = (ok + ng) / 2;

    int le = 0, cnt = 0;
    for (int i = 0; i < n; i++) {
      if (b[i] < mid)
        ++le;
      else if (mid >= a[i])
        ++cnt;
    }

    if (le < num) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  return ok;
}

void solve3(int n, const vector<ll> &a, const vector<ll> &b) {
  map<ll, vector<int>> memo;
  for (int i = 0; i < n; i++) {
    memo[a[i]].push_back(0);
    memo[b[i]].push_back(1);
  }

  int le = 0, cnt = 0;
  ll minv = -1, maxv = -1;
  for (auto &p : memo) {
    for (int i : p.second) {
      if (i == 0) {
        ++cnt;
      } else {
        --cnt;
        ++le;
      }
    }

    if (le <= n / 2 + 1 && n / 2 + 1 <= le + cnt) {
      if (minv == -1)
        minv = p.first;
      maxv = max(maxv, p.first);
    }
  }

  cout << maxv - minv + 1 << newl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  vector<ll> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }

  if (n % 2 == 1) {
    cout << solve2(n, a, b, (n + 1) / 2) - solve(n, a, b, (n + 1) / 2) + 1
         << newl;
  } else {
    assert(false);
    ll foo = solve(n, a, b, (n + 1) / 2 + 1);
    ll bar = solve(n, a, b, (n + 1) / 2);
    ll foo2 = solve2(n, a, b, (n + 1) / 2 + 1);
    ll bar2 = solve2(n, a, b, (n + 1) / 2);
    cout << (foo2 + bar2) - (foo + bar) + 1 << newl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';

ll solve(int n, const vector<ll> &a, const vector<ll> &b, int num) {
  ll ng = 0, ok = 1e9 + 1;
  while (ok - ng > 1) {
    ll mid = (ok + ng) / 2;

    int le = 0, cnt = 0;
    for (int i = 0; i < n; i++) {
      if (b[i] < mid)
        ++le;
      else if (mid >= a[i])
        ++cnt;
    }

    if (num <= le + cnt) {
      ok = mid;
    } else {
      ng = mid;
    }
    // cerr << ok << "," << ng << newl;
  }
  return ok;
}

ll solve2(int n, const vector<ll> &a, const vector<ll> &b, int num) {
  ll ok = 0, ng = 1e9 + 1;
  while (ng - ok > 1) {
    ll mid = (ok + ng) / 2;

    int le = 0, cnt = 0;
    for (int i = 0; i < n; i++) {
      if (b[i] < mid)
        ++le;
      else if (mid >= a[i])
        ++cnt;
    }

    if (le < num) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  return ok;
}

void solve3(int n, const vector<ll> &a, const vector<ll> &b) {
  map<ll, vector<int>> memo;
  for (int i = 0; i < n; i++) {
    memo[a[i]].push_back(0);
    memo[b[i]].push_back(1);
  }

  int le = 0, cnt = 0;
  ll minv = -1, maxv = -1;
  for (auto &p : memo) {
    for (int i : p.second) {
      if (i == 0) {
        ++cnt;
      } else {
        --cnt;
        ++le;
      }
    }

    if (le <= n / 2 + 1 && n / 2 + 1 <= le + cnt) {
      if (minv == -1)
        minv = p.first;
      maxv = max(maxv, p.first);
    }
  }

  cout << maxv - minv + 1 << newl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  vector<ll> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }

  if (n % 2 == 1) {
    cout << solve2(n, a, b, (n + 1) / 2) - solve(n, a, b, (n + 1) / 2) + 1
         << newl;
  } else {
    ll foo = solve(n, a, b, (n + 1) / 2 + 1);
    ll bar = solve(n, a, b, (n + 1) / 2);
    ll foo2 = solve2(n, a, b, (n + 1) / 2 + 1);
    ll bar2 = solve2(n, a, b, (n + 1) / 2);
    cout << (foo2 + bar2) - (foo + bar) + 1 << newl;
  }
  return 0;
}