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

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007

#define MAX 300000

/*
4
1 3
2 6
4 7
5 8

4
1 3
2 4
6 8
7 9
*/
struct S {
  int A;
  int B;
  int sortnum;
  bool operator<(const S s) const {
    if (sortnum == 0) {
      return B > s.B;
    } else {
      return A < s.A;
    }
  }
};

signed main() {
  int N;
  S AB[MAX];

  cin >> N;
  for (int i = 0; i < N; i++) {
    int A, B;
    cin >> A;
    cin >> B;
    S tmps;
    tmps.A = A;
    tmps.B = B;
    tmps.sortnum = 0;
    AB[i] = tmps;
  }

  sort(AB, AB + N);

  int rl, ll, r2, l2;
  int num = (N - 1) / 2;

  rl = AB[num].B;
  r2 = AB[num + 1].B;
  for (int i = 0; i < N; i++) {
    AB[i].sortnum = 1;
  }

  sort(AB, AB + N);

  ll = AB[num].A;
  l2 = AB[num + 1].A;

  int ln = ll;
  int rn = rl;

  if (N % 2 == 0) {
    cout << rl + r2 - ll - l2 + 1 << endl;
  } else {
    cout << rn - ln + 1 << endl;
  }
}


Error: 0

Fixed submission:

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007

#define MAX 3000

/*
4
1 3
2 6
4 7
5 8
*/
struct S {
  int A;
  int B;
  int sortnum;
  bool operator<(const S s) const {
    if (sortnum == 0) {
      return B > s.B;
    } else {
      return A < s.A;
    }
  }
};

signed main() {
  int N;
  S AB[MAX];

  cin >> N;
  for (int i = 0; i < N; i++) {
    int A, B;
    cin >> A;
    cin >> B;
    S tmps;
    tmps.A = A;
    tmps.B = B;
    tmps.sortnum = 0;
    AB[i] = tmps;
  }

  sort(AB, AB + N);

  // cout<<AB[0].A<<endl;

  int rl, ll, r2, l2;
  int num = (N - 1) / 2;

  rl = AB[num].B;
  r2 = AB[num + 1].B;
  for (int i = 0; i < N; i++) {
    AB[i].sortnum = 1;
  }

  sort(AB, AB + N);

  ll = AB[num].A;
  l2 = AB[num + 1].A;
  // cout<<ll<<","<<rl<<","<<l2<<","<<r2<<endl;

  int ans = 0;

  int ln = ll;
  int rn = rl;

  if (N % 2 == 0) {
    ln = (ll + l2) / 2;
    if ((ll + l2) % 2 == 1) {
      ln++;
      ans++;
    }
    rn = (rl + r2) / 2;
    if ((rl + r2) % 2 == 1) {
      ans++;
    }
  }
  // cout<<ll<<","<<rl<<","<<l2<<","<<r2<<","<<ans<<endl;
  for (int i = 0; i < N; i++) {
    int l = max(ln, AB[i].A);
    int r = min(rn, AB[i].B);
    ans += r - l + 1;
    ln = r + 1;
    if (ln == rn + 1)
      break;
  }

  cout << ans << endl;
}