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

#include <bits/stdc++.h>
using namespace std;
int const nax = 3e5 + 10;

map<int, int> ss;
int a[nax];
int b[nax];
int ca[nax];
int cb[nax];
int n;

int top;

int main() {
  scanf("%d", &n);

  for (int i = 1; i <= n; ++i) {
    scanf("%d %d", a + i, b + i);
    ca[i] = a[i];
    cb[i] = b[i];
  }
  //  cb[0] = INT_MAX;

  sort(ca + 1, ca + 1 + n);
  sort(cb + 1, cb + 1 + n);

  if (n % 2) {
    int a = ca[n / 2 + 1];
    int b = cb[n / 2 + 1];

    cout << b - a + 1;
  } else {
    int a = ca[n / 2] + ca[n / 2 + 1];
    int b = cb[n / 2] + cb[n / 2 + 1];

    cout << b - a + 1;
  }

  /**
  Stupid me.
  First number that can be median is ca[n / 2 + 1].
  last number is cb[n / 2 + 1].

  This number has real existence and you already know that number will be able
  to be median in continuous range.

  Same for even case

  */

  /*   sort(c + 1, c + 1 + n);


     int cR = c[1].second;
     int cL = c[1].first;
     for (int i = 2 ; i <= n ; ++ i) {

         if (c[i].first > cR) {
             d[++ top] = make_pair(cL, cR);
             cR = c[i].second;
             cL = c[i].second;
         } else {
             cR = max(cR, c[i].second);
         }
     }

     d[++ top] = make_pair(cL, cR);


     long long ans = 0;
     if (n % 2) {
         for (int i = 1 ; i <= top ; ++ i) {
             int l = d[i].first, r = d[i].second;

             int min_bound = ca[(n + 1) / 2];
             int max_bound = cb[(n + 1) / 2];

             l = max(l, min_bound);
             r = min(r, max_bound);

             if (l > r) continue;

             ans += r - l + 1;
         }

         cout << ans;
     } else {

        /// throw;
         #define int long long

         int l_min = INT_MAX, l_max = INT_MIN;

         for (int i = 1 ; i <= top ; ++ i) {
             int l = d[i].first, r = d[i].second;

             int min_bound = ca[n / 2];
             int max_bound = cb[n - n /2];

             l = max(l, min_bound);
             r = min(r, max_bound);

             if (l > r) continue;

             l_min = min(l_min, l);
             l_max = max(l_max, r);

         }


         int r_min = INT_MAX, r_max = INT_MIN;


         for (int i = 1 ; i <= top ; ++ i) {
             int l = d[i].first, r = d[i].second;
             int min_bound = ca[n / 2 + 1];
             int max_bound = cb[n / 2 - 1];

             l = max(l, min_bound);
             r = min(r, max_bound);

             if (l > r) continue;

             r_min = min(r_min, l);
             r_max = max(r_max, r);

         }


         int all = r_max + l_max - r_min - l_min;

         cout << all + 1;
     }
 */
}
/*
    Good Luck
        -Lucina
*/


Error: -6
terminate called without an active exception


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int const nax = 3e5 + 10;

map<int, int> ss;
int a[nax];
int b[nax];
int ca[nax];
int cb[nax];
int n;

pair<int, int> c[nax];
pair<int, int> d[nax];
int top;

int main() {
  scanf("%d", &n);

  for (int i = 1; i <= n; ++i) {
    scanf("%d %d", a + i, b + i);
    ca[i] = a[i];
    cb[i] = b[i];
    c[i] = make_pair(a[i], b[i]);
  }
  cb[0] = INT_MAX;

  sort(ca + 1, ca + 1 + n);
  sort(cb + 1, cb + 1 + n);

  sort(c + 1, c + 1 + n);

  int cR = c[1].second;
  int cL = c[1].first;
  for (int i = 2; i <= n; ++i) {

    if (c[i].first > cR) {
      d[++top] = make_pair(cL, cR);
      cR = c[i].second;
      cL = c[i].second;
    } else {
      cR = max(cR, c[i].second);
    }
  }

  d[++top] = make_pair(cL, cR);

  long long ans = 0;
  if (n % 2) {
    for (int i = 1; i <= top; ++i) {
      int l = d[i].first, r = d[i].second;

      int min_bound = ca[(n + 1) / 2];
      int max_bound = cb[(n + 1) / 2];

      l = max(l, min_bound);
      r = min(r, max_bound);

      if (l > r)
        continue;

      ans += r - l + 1;
    }

    cout << ans;
  } else {

    throw;
#define int long long

    int l_min = INT_MAX, l_max = INT_MIN;

    for (int i = 1; i <= top; ++i) {
      int l = d[i].first, r = d[i].second;

      int min_bound = ca[n / 2];
      int max_bound = cb[n / 2];

      l = max(l, min_bound);
      r = min(r, max_bound);

      if (l > r)
        continue;

      l_min = min(l_min, l);
      l_max = max(l_max, r);
    }

    int r_min = INT_MAX, r_max = INT_MIN;

    for (int i = 1; i <= top; ++i) {
      int l = d[i].first, r = d[i].second;
      int min_bound = ca[n / 2 + 1];
      int max_bound = cb[n / 2 - 1];

      l = max(l, min_bound);
      r = min(r, max_bound);

      if (l > r)
        continue;

      r_min = min(r_min, l);
      r_max = max(r_max, r);
    }

    int all = r_max + l_max - r_min - l_min;

    cout << all + 1;
  }
}
/*
    Good Luck
        -Lucina
*/