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

#include <algorithm>
#include <cstdio>
#include <vector>
#define lowbit(x) ((x) & (-(x)))
#define re register
#define ll long long
#define ull unsigned long long
#define rep(i, a, b) for (re int i = a; i <= b; ++i)
#define per(i, a, b) for (re int i = a; i >= b; --i)
template <typename T> inline void read(T &x) {
  x = 0;
  char s = (char)getchar();
  bool f = false;
  while (!(s >= '0' && s <= '9')) {
    if (s == '-')
      f = true;
    s = (char)getchar();
  }
  while (s >= '0' && s <= '9') {
    x = (x << 1) + (x << 3) + s - '0';
    s = (char)getchar();
  }
  if (f)
    x = (~x) + 1;
}
template <typename T> inline void clear(T *a, int l, int r, int val) {
  memset(&a[l], val, sizeof(T) * (r - l + 1));
}
using namespace std;
const int N = 1e6 + 5;
struct node {
  int a, b;
  inline 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];
#define fi first
#define se second
int n;
signed main() {
  read(n);
  rep(i, 1, n) read(x[i].a), read(x[i].b);
  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 + 1 + cnt);
  if (n & 1) {

    int one = 0, two = 0;
    int 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;
      }
    }
    printf("%d\\n", r - l + 1);
  } else {
    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;
      }
    }
    printf("%d\\n", r1 - l1 + 1 + r2 - l2);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
#define lowbit(x) ((x) & (-(x)))
#define re register
#define ll long long
#define ull unsigned long long
#define rep(i, a, b) for (re int i = a; i <= b; ++i)
#define per(i, a, b) for (re int i = a; i >= b; --i)
template <typename T> inline void read(T &x) {
  x = 0;
  char s = (char)getchar();
  bool f = false;
  while (!(s >= '0' && s <= '9')) {
    if (s == '-')
      f = true;
    s = (char)getchar();
  }
  while (s >= '0' && s <= '9') {
    x = (x << 1) + (x << 3) + s - '0';
    s = (char)getchar();
  }
  if (f)
    x = (~x) + 1;
}
template <typename T> inline void clear(T *a, int l, int r, int val) {
  memset(&a[l], val, sizeof(T) * (r - l + 1));
}
using namespace std;
const int N = 1e5 + 5;
struct node {
  int a, b;
  inline 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];
#define fi first
#define se second
int n;
signed main() {
  read(n);
  rep(i, 1, n) read(x[i].a), read(x[i].b);
  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 + 1 + cnt);
  if (n & 1) {

    int one = 0, two = 0;
    int 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;
      }
    }
    printf("%d\\n", r - l + 1);
  } else {
    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;
      }
    }
    printf("%d\\n", r1 - l1 + 1 + r2 - l2);
  }
  return 0;
}