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

#include <algorithm>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define pb(a) push_back(a)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

typedef long long unsigned int ull;
typedef long long int ll;
typedef pair<ll, ll> P;

vector<vector<ll>> vec_sort(vector<vector<ll>> v, int itr) {
  if (v.size() <= 1) {
    return v;
  }
  ll l = v[0][itr];
  vector<vector<ll>> a, b;
  for (ll i; i < v.size(); i++) {
    if (i == 0) {
      continue;
    }
    if (v[i][itr] <= l) {
      a.pb(v[i]);
    } else {
      b.pb(v[i]);
    }
  }
  a = vec_sort(a, itr);
  b = vec_sort(b, itr);
  a.pb(v[0]);
  a.insert(a.end(), all(b));
  return a;
}

int main() {
  int N;
  cin >> N;
  vector<ll> a, b;
  REP(i, N) {
    ll ai, bi;
    cin >> ai >> bi;
    a.pb(ai);
    b.pb(bi);
  }
  sort(all(a));
  sort(all(b));
  vector<ll> x_min;
  vector<ll> x_max;
  REP(i, N) { x_min.pb(a[i]); }
  REP(i, N) { x_max.pb(b[i]); }

  double med_min, med_max;
  ll ans;
  if (N % 2 == 1) {
    med_min = x_min[(N + 1) / 2 - 1];
    med_max = x_max[(N + 1) / 2 - 1];
    ans = med_max - med_min + 1;
  } else {
    med_min = (x_min[N / 2 - 1] + x_min[N / 2]) / 2.0;
    med_max = (x_max[N / 2 - 1] + x_max[N / 2]) / 2.0;
    ans = 2 * (med_max - med_min) + 1;
  }
  // cout << "[" << med_min << "," << med_max << "]" << endl;

  cout << ans << endl;
  // x[N]_MIN=MAX(A)
  // x[N]_MAX=MAX(B)

  // X[0]_MIN=MIN(A)
  // X[0]_MAX=MIN(B)

  // 1    4   10 10         15       20
  //   2    5       11  12  15          21
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define pb(a) push_back(a)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

typedef long long unsigned int ull;
typedef long long int ll;
typedef pair<ll, ll> P;

vector<vector<ll>> vec_sort(vector<vector<ll>> v, int itr) {
  if (v.size() <= 1) {
    return v;
  }
  ll l = v[0][itr];
  vector<vector<ll>> a, b;
  REP(i, v.size()) {
    if (i == 0) {
      continue;
    }
    if (v[i][itr] <= l) {
      a.pb(v[i]);
    } else {
      b.pb(v[i]);
    }
  }
  a = vec_sort(a, itr);
  b = vec_sort(b, itr);
  a.pb(v[0]);
  a.insert(a.end(), all(b));
  return a;
}

int main() {
  int N;
  cin >> N;
  vector<vector<ll>> ab;
  REP(i, N) {
    ll ai, bi;
    cin >> ai >> bi;
    vector<ll> abi = {ai, bi};
    ab.pb(abi);
  }
  ab = vec_sort(ab, 0);
  // REP(i,N){
  //     cout << ab[i][0] << " " << ab[i][1] << endl;
  // }
  vector<ll> x_min;
  vector<ll> x_max;

  REP(i, N) { x_min.pb(ab[i][0]); }
  ab = vec_sort(ab, 1);
  REP(i, N) { x_max.pb(ab[i][1]); }

  double med_min, med_max;
  ll ans;
  if (N % 2 == 1) {
    med_min = x_min[(N + 1) / 2 - 1];
    med_max = x_max[(N + 1) / 2 - 1];
    ans = med_max - med_min + 1;
  } else {
    med_min = (x_min[N / 2 - 1] + x_min[N / 2]) / 2.0;
    med_max = (x_max[N / 2 - 1] + x_max[N / 2]) / 2.0;
    ans = 2 * (med_max - med_min) + 1;
  }
  // cout << "[" << med_min << "," << med_max << "]" << endl;

  cout << ans << endl;
  // x[N]_MIN=MAX(A)
  // x[N]_MAX=MAX(B)

  // X[0]_MIN=MIN(A)
  // X[0]_MAX=MIN(B)

  // 1    4   10 10         15       20
  //   2    5       11  12  15          21
}