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

#include "bits/stdc++.h"
#include <unordered_set>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define vll vector<vector<long long>>
#define vl vector<long long>
#define vi vector<int>
#define vii vector<vector<int>>
#define pb push_back
#define pf push_front
#define ld long double
#define Sort(a) sort(a.begin(), a.end())
#define cSort(a, cmp) sort(a.begin(), a.end(), cmp)
#define reSort(a) sort(a.rbegin(), a.rend())
static const ll llMAX = numeric_limits<long long>::max();
static const int intMAX = numeric_limits<int>::max();
static const ll llMIN = numeric_limits<long long>::min();
static const int intMIN = numeric_limits<int>::min();
static const ll d_5 = 100000;
static const ll d9_7 = 1000000007;
static const ll d_9 = 1000000000;
static const double PI = 3.14159265358979323846;
template <class T> T gcd(T a, T b) {
  if (a == 0) {
    return b;

  } else if (b == 0) {
    return a;
  }
  int flag1 = 0, flag2 = 0;
  if (a > 0) {
    flag1 = 1;
    a = a * (-1);
  }
  if (b > 0) {
    flag2 = 1;
    b = b * (-1);
  }
  while (1) {
    if (a < b)
      swap(a, b);
    if (!b)
      break;
    a %= b;
  }

  return a;
}
ll digitpower(ll a, ll b) { // aのb乗を計算
  if (b == 1) {
    return a;
  } else if (b == 0) {
    return 1;
  }
  int mode = 0;
  if (mode == 0) {
    if (b % 2 == 1) {
      ll tmp = digitpower(a, (b - 1) / 2);
      tmp %= d9_7;
      tmp *= tmp;
      tmp %= d9_7;
      tmp *= a;
      return (tmp) % d9_7;
    } else {
      ll tmp = digitpower(a, (b) / 2);
      tmp %= d9_7;
      tmp *= tmp;
      tmp %= d9_7;
      return (tmp) % d9_7;
    }
  } else {
    if (b % 2 == 1) {
      ll tmp = digitpower(a, (b - 1) / 2);
      tmp *= tmp;
      tmp *= a;
      return (tmp);
    } else {
      ll tmp = digitpower(a, (b) / 2);
      tmp *= tmp;
      return (tmp);
    }
  }
  return 0;
}
template <class T> void Printvector(std::vector<T> &a) {
  int size = a.size();
  rep(i, size) { cout << a[i] << " "; }
  cout << endl;
}
template <class T> void Printvector(std::vector<std::vector<T>> &a) {
  int size = a.size();
  rep(i, size) {
    int size2 = a[i].size();
    rep(j, size2) { cout << a[i][j] << " "; }
    cout << endl;
  }
  cout << endl;
}
unordered_set<ll> primes(ll n) {
  unordered_set<ll> ret;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ret.insert(i);
      ret.insert(n / i);
    }
  }
  return ret;
}
long long modinv(long long a, long long m) { // modの逆元
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
template <class T> vector<T> getaccum(vector<T> a) {
  int size = a.size();
  vector<T> ans(size);
  ans[0] = a[0];
  for (int i = 0; i < size - 1; i++) {
    ans[i + 1] = ans[i] + a[i + 1];
    // ans[i+1]%=d9_7;
  }
  return ans;
}
struct c {
  ll a;
  ll b;
};
// 比較関数を定義
bool cmp(const c &left, const c &right) { return left.a < right.a; }
int main(void) {
  int n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> b(n);
  rep(i, n) { cin >> a[i] >> b[i]; }
  Sort(a);
  Sort(b);
  if (n % 2 == 1) {
    cout << b[n / 2] - a[n / 2] + 1 << endl;
  } else {
    ld tmp = ((ld)b[n / 2] + (ld)b[n / 2 - 1]) / 2 -
             ((ld)a[n / 2] + (ld)a[n / 2 - 1]) / 2;
    tmp /= 0.5;
    cout << (long long int)(tmp + 1) << endl;
  }

  return 0;
}

// clang++ -g -fsanitize=address -fno-omit-frame-pointer 1.cpp -o test.exe

//<<std::setprecision(30)

// 重複削除
/* std::sort(vec.begin(), vec.end());

 vec.erase(std::unique(vec.begin(), vec.end()), vec.end());*/

// ペアの全探索
/*do{
              int s=0;
              for(int i=0;i<n/2;i++)s+=v[i]^v[i+n/2];
              r=max(s,r);
              reverse(v.begin()+n/2,v.end());
      }while(next_permutation(v.begin(),v.end()));*/


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_set>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define vll vector<vector<long long>>
#define vl vector<long long>
#define vi vector<int>
#define vii vector<vector<int>>
#define pb push_back
#define pf push_front
#define ld long double
#define Sort(a) sort(a.begin(), a.end())
#define cSort(a, cmp) sort(a.begin(), a.end(), cmp)
#define reSort(a) sort(a.rbegin(), a.rend())
static const ll llMAX = numeric_limits<long long>::max();
static const int intMAX = numeric_limits<int>::max();
static const ll llMIN = numeric_limits<long long>::min();
static const int intMIN = numeric_limits<int>::min();
static const ll d_5 = 100000;
static const ll d9_7 = 1000000007;
static const ll d_9 = 1000000000;
static const double PI = 3.14159265358979323846;
template <class T> T gcd(T a, T b) {
  if (a == 0) {
    return b;

  } else if (b == 0) {
    return a;
  }
  int flag1 = 0, flag2 = 0;
  if (a > 0) {
    flag1 = 1;
    a = a * (-1);
  }
  if (b > 0) {
    flag2 = 1;
    b = b * (-1);
  }
  while (1) {
    if (a < b)
      swap(a, b);
    if (!b)
      break;
    a %= b;
  }

  return a;
}
ll digitpower(ll a, ll b) { // aのb乗を計算
  if (b == 1) {
    return a;
  } else if (b == 0) {
    return 1;
  }
  int mode = 0;
  if (mode == 0) {
    if (b % 2 == 1) {
      ll tmp = digitpower(a, (b - 1) / 2);
      tmp %= d9_7;
      tmp *= tmp;
      tmp %= d9_7;
      tmp *= a;
      return (tmp) % d9_7;
    } else {
      ll tmp = digitpower(a, (b) / 2);
      tmp %= d9_7;
      tmp *= tmp;
      tmp %= d9_7;
      return (tmp) % d9_7;
    }
  } else {
    if (b % 2 == 1) {
      ll tmp = digitpower(a, (b - 1) / 2);
      tmp *= tmp;
      tmp *= a;
      return (tmp);
    } else {
      ll tmp = digitpower(a, (b) / 2);
      tmp *= tmp;
      return (tmp);
    }
  }
  return 0;
}
template <class T> void Printvector(std::vector<T> &a) {
  int size = a.size();
  rep(i, size) { cout << a[i] << " "; }
  cout << endl;
}
template <class T> void Printvector(std::vector<std::vector<T>> &a) {
  int size = a.size();
  rep(i, size) {
    int size2 = a[i].size();
    rep(j, size2) { cout << a[i][j] << " "; }
    cout << endl;
  }
  cout << endl;
}
unordered_set<ll> primes(ll n) {
  unordered_set<ll> ret;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ret.insert(i);
      ret.insert(n / i);
    }
  }
  return ret;
}
long long modinv(long long a, long long m) { // modの逆元
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
template <class T> vector<T> getaccum(vector<T> a) {
  int size = a.size();
  vector<T> ans(size);
  ans[0] = a[0];
  for (int i = 0; i < size - 1; i++) {
    ans[i + 1] = ans[i] + a[i + 1];
    // ans[i+1]%=d9_7;
  }
  return ans;
}
struct c {
  ll a;
  ll b;
};
// 比較関数を定義
bool cmp(const c &left, const c &right) { return left.a < right.a; }
int main(void) {
  int n;
  cin >> n;
  vector<c> d(n);
  rep(i, n) { cin >> d[i].a >> d[i].b; }
  ll l = 0;
  ll r = 1e9 + 100;
  ll ansl, ansr;
  while (l != r) {
    ll mid = (l + r + 1) / 2;
    int lcnt = 0, rcnt = 0, other = 0;
    rep(i, n) {
      if (d[i].b < mid) {
        rcnt++;
      } else if (d[i].a > mid) {
        lcnt++;
      } else {
        other++;
      }
    }
    if (lcnt > n / 2) {
      // でかすぎた
      r = mid - 1;
    } else if (rcnt > n / 2) {
      l = mid + 1;
    }
    if (n % 2 == 0) {
      if (other >= 2) {
        l = mid;
      } else {
        if (lcnt == rcnt) {
          ll maxlb = 0;
          ll maxrb = 0;
          ll minla = 1e9 + 100;
          ll minra = 1e9 + 100;
          rep(i, n) {
            if (d[i].a > mid) {
              maxrb = max(d[i].b, maxrb);
              minra = min(minra, d[i].a);
            } else {
              maxlb = max(d[i].b, maxlb);
              minla = min(minla, d[i].a);
            }
          }
          if (((long double)(minla + minra)) / 2 <= (ld)mid &&
              ((long double)(minla + minra)) / 2 >= (ld)mid) {
            // ok
            l = mid;
          } else {
            // ng
            r = mid - 1;
          }
        } else {
          // ok
          // 今回は上限を求めたいから、こっち
          l = mid;
        }
      }

    } else {
      // ok
      l = mid;
    }
  }
  ansr = l; // 上限値

  l = 0;
  r = 1e9 + 100;
  while (l != r) {
    ll mid = (l + r + 1) / 2;
    int lcnt = 0, rcnt = 0, other = 0;
    rep(i, n) {
      if (d[i].b < mid) {
        rcnt++;
      } else if (d[i].a > mid) {
        lcnt++;
      } else {
        other++;
      }
    }
    if (lcnt > n / 2) {
      // でかすぎた
      r = mid - 1;
    } else if (rcnt > n / 2) {
      l = mid + 1;
    }
    if (n % 2 == 0) {
      if (other >= 2) {
        l = mid;
      } else {
        if (lcnt == rcnt) {
          ll maxlb = 0;
          ll maxrb = 0;
          ll minla = 1e9 + 100;
          ll minra = 1e9 + 100;
          rep(i, n) {
            if (d[i].a > mid) {
              maxrb = max(d[i].b, maxrb);
              minra = min(minra, d[i].a);
            } else {
              maxlb = max(d[i].b, maxlb);
              minla = min(minla, d[i].a);
            }
          }
          if (((long double)(minla + minra)) / 2 <= (ld)mid &&
              ((long double)(minla + minra)) / 2 >= (ld)mid) {
            // ok
            r = mid;
          } else {
            // ng
            l = mid + 1;
          }
        } else {
          // ok
          // 今回は下限を求めたいから、こっち
          r = mid;
        }
      }

    } else {
      // ok
      r = mid;
    }
  }
  ansr = l; // 下限値
  cout << ansr - ansl + 1 << endl;
  return 0;
}

// clang++ -g -fsanitize=address -fno-omit-frame-pointer 1.cpp -o test.exe

//<<std::setprecision(30)

// 重複削除
/* std::sort(vec.begin(), vec.end());

 vec.erase(std::unique(vec.begin(), vec.end()), vec.end());*/

// ペアの全探索
/*do{
              int s=0;
              for(int i=0;i<n/2;i++)s+=v[i]^v[i+n/2];
              r=max(s,r);
              reverse(v.begin()+n/2,v.end());
      }while(next_permutation(v.begin(),v.end()));*/