Problem p02661 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {
  int N;
  cin >> N;
  vector<ll> a(N);
  vector<ll> b(N);

  for (int i = 0; i < N; i++) {
    cin >> a[i] >> b[i];
  }

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());

  int n = 0, m = 0, t = 0, u = 0;

  if (N % 2 == 1) {
    n = a[N / 2];
    m = b[N / 2];
    cout << m - n + 1 << endl;
  }

  else {
    n = a[(N / 2) - 1];
    m = b[(N / 2) - 1];
    t = a[(N / 2)];
    u = b[(N / 2)];

    cout << u + m - t - n + 1 << endl;
  }

  /*

  if (N % 2 == 1) {
          for (int i = 0; i < (N + 1) / 2; i++) {
                  for (int j = 0; j < N - 1 - i; j++) {
                          if (a[j] > a[j + 1]) {
                                  ll p = a[j];
                                  a[j] = a[j + 1];
                                  a[j + 1] = p;
                          }
                          if (b[j] > b[j + 1]) {
                                  ll q = b[j];
                                  b[j] = b[j + 1];
                                  b[j + 1] = q;
                          }
                  }
                  if (i == (N - 1) / 2) {
                          n = a[(N - 1) / 2];
                          m = b[(N - 1) / 2];
                  }
          }
          cout << m - n + 1 << endl;
  }

  else {
          for (int i = 0; i < (N / 2) + 1; i++) {
                  for (int j = 0; j < N - 1 - i; j++) {
                          if (a[j] > a[j + 1]) {
                                  ll p = a[j];
                                  a[j] = a[j + 1];
                                  a[j + 1] = p;
                          }
                          if (b[j] > b[j + 1]) {
                                  ll q = b[j];
                                  b[j] = b[j + 1];
                                  b[j + 1] = q;
                          }
                  }
                  if (i == (N / 2)-1) {
                          n = a[(N / 2) - 1];
                          m = b[(N / 2) - 1];
                  }
                  else if (i == (N / 2)) {
                          t = a[(N / 2)];
                          u = b[(N / 2)];
                  }

          }

          cout << u + m - t - n + 1 << endl;
  }

  */

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {
  int N;
  cin >> N;
  vector<ll> a(N + 5);
  vector<ll> b(N + 5);

  for (int i = 0; i < N; i++) {
    cin >> a[i] >> b[i];
  }

  int n = 0, m = 0, t = 0, u = 0;

  if (N % 2 == 1) {
    for (int i = 0; i < (N + 1) / 2; i++) {
      for (int j = 0; j < N - 1 - i; j++) {
        if (a[j] > a[j + 1]) {
          ll p = a[j];
          a[j] = a[j + 1];
          a[j + 1] = p;
        }
        if (b[j] > b[j + 1]) {
          ll q = b[j];
          b[j] = b[j + 1];
          b[j + 1] = q;
        }
      }
      if (i == (N - 1) / 2) {
        n = a[(N - 1) / 2];
        m = b[(N - 1) / 2];
      }
      /*else {
              a[N - i - 1] = 0;
              b[N - i - 1] = 0;
      }*/
    }
    cout << m - n + 1 << endl;
  }

  else {
    for (int i = 0; i < (N / 2) + 1; i++) {
      for (int j = 0; j < N - 1 - i; j++) {
        if (a[j] > a[j + 1]) {
          ll p = a[j];
          a[j] = a[j + 1];
          a[j + 1] = p;
        }
        if (b[j] > b[j + 1]) {
          ll q = b[j];
          b[j] = b[j + 1];
          b[j + 1] = q;
        }
      }
      if (i == (N / 2) - 1) {
        n = a[(N / 2) - 1];
        m = b[(N / 2) - 1];
      } else if (i == (N / 2)) {
        t = a[(N / 2)];
        u = b[(N / 2)];
      }
    }

    cout << u + m - t - n + 1 << endl;
  }

  return 0;
}