Problem p02661 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define fastIO                                                                 \\
  std::ios_base::sync_with_stdio(false);                                       \\
  std::cin.tie(0);                                                             \\
  std::cout.tie(0)
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define endl '\\n'
typedef vector<int> VI;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef double db;
// mt19937 mrand(random_device{}());
// int rnd(int x) { return mrand() % x;}
const ll mod = 1000000007;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

const int maxn = 2e5 + 10;
int n;
int x[maxn], y[maxn];

int main() {
  fastIO;
  cin >> n;
  rep(i, 0, n) { cin >> x[i] >> y[i]; }
  sort(x, x + n);
  sort(y, y + n);
  if (n & 1) {
    cout << y[n / 2] - x[n / 2] + 1;
  } else {
    cout << y[n / 2] + y[n / 2 - 1] - x[n / 2] - x[n / 2 - 1] + 1;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fastIO                                                                 \\
  std::ios_base::sync_with_stdio(false);                                       \\
  std::cin.tie(0);                                                             \\
  std::cout.tie(0)
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define endl '\\n'
typedef vector<int> VI;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef double db;
// mt19937 mrand(random_device{}());
// int rnd(int x) { return mrand() % x;}
const ll mod = 1000000007;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

const int maxn = 2e5 + 10;
int n;
int x[maxn], y[maxn];
vector<int> v;

int get_id(int x) { return lower_bound(all(v), x) - v.begin() + 1; }
int d[maxn];

int main() {
  fastIO;
  cin >> n;
  rep(i, 0, n) {
    cin >> x[i] >> y[i];
    v.pb(x[i]);
    v.pb(y[i]);
  }
  sort(all(v));
  v.erase(unique(all(v)), v.end());
  rep(i, 0, n) {
    // cout << get_id(x[i]) << " " << get_id(y[i]) << endl;
    d[get_id(x[i])]++;
    d[get_id(y[i]) + 1]--;
  }
  int s = 0, t = 0;
  int m = v.size();
  rep(i, 1, m + 1) {
    d[i] += d[i - 1];
    if (d[i] >= n - 1) {
      if (!s)
        s = v[i - 1];
      t = v[i - 1];
    }
  }
  cout << t - s + 1 << endl;
  return 0;
}