Problem p02661 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define se second
#define fi first
#define N 3005
#define inf 1e9
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

inline int read() {
  int x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}

struct node {
  int a, b;
  bool operator<(node const &x) const {
    if (a == x.a)
      return b < x.b;
    return a < x.a;
  }
} x[N];
pair<int, int> p[N];
int n;

inline void sol1(int cnt) {
  int one = 0, two = 0, l = 0, r = 0;
  rep(i, 1, cnt) {
    if (p[i].se > 0) {
      one++;
      if (one == n / 2 + 1)
        l = p[i].fi;
    } else {
      two++;
      if (two == n / 2 + 1)
        r = p[i].fi;
    }
  }
  cout << r - l + 1 << '\n';
}

inline void sol2(int cnt) {
  int one = 0, two = 0;
  int l1 = -1, r1 = -1, l2 = -1, r2 = -1;
  rep(i, 1, cnt) {
    if (p[i].se > 0) {
      one++;
      if (one == n / 2)
        l1 = p[i].fi;
      else if (one == n / 2 + 1)
        l2 = p[i].fi;
    } else {
      two++;
      if (two == n / 2)
        r1 = p[i].fi;
      else if (two == n / 2 + 1)
        r2 = p[i].fi;
    }
  }
  cout << r1 - l1 + 1 + r2 - l2 << '\n';
}
signed main() {
  n = read();
  rep(i, 1, n) x[i].a = read(), x[i].b = read();
  sort(x + 1, x + 1 + n);
  int cnt = 0;
  rep(i, 1, n) {
    p[++cnt].fi = x[i].a;
    p[cnt].se = i;
    p[++cnt].fi = x[i].b;
    p[cnt].se = -i;
  }
  sort(p + 1, p + cnt + 1);
  if (n & 1)
    sol1(cnt);
  else
    sol2(cnt);
  return 0;
}


Fixed submission:
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define se second
#define fi first
#define N 1000005
#define inf 1e9
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

inline int read() {
  int x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}

struct node {
  int a, b;
  bool operator<(node const &x) const {
    if (a == x.a)
      return b < x.b;
    return a < x.a;
  }
} x[N];
pair<int, int> p[N];
int n;

inline void sol1(int cnt) {
  int one = 0, two = 0, l = 0, r = 0;
  rep(i, 1, cnt) {
    if (p[i].se > 0) {
      one++;
      if (one == n / 2 + 1)
        l = p[i].fi;
    } else {
      two++;
      if (two == n / 2 + 1)
        r = p[i].fi;
    }
  }
  cout << r - l + 1 << '\n';
}

inline void sol2(int cnt) {
  int one = 0, two = 0;
  int l1 = -1, r1 = -1, l2 = -1, r2 = -1;
  rep(i, 1, cnt) {
    if (p[i].se > 0) {
      one++;
      if (one == n / 2)
        l1 = p[i].fi;
      else if (one == n / 2 + 1)
        l2 = p[i].fi;
    } else {
      two++;
      if (two == n / 2)
        r1 = p[i].fi;
      else if (two == n / 2 + 1)
        r2 = p[i].fi;
    }
  }
  cout << r1 - l1 + 1 + r2 - l2 << '\n';
}
signed main() {
  n = read();
  rep(i, 1, n) x[i].a = read(), x[i].b = read();
  sort(x + 1, x + 1 + n);
  int cnt = 0;
  rep(i, 1, n) {
    p[++cnt].fi = x[i].a;
    p[cnt].se = i;
    p[++cnt].fi = x[i].b;
    p[cnt].se = -i;
  }
  sort(p + 1, p + cnt + 1);
  if (n & 1)
    sol1(cnt);
  else
    sol2(cnt);
  return 0;
}