Problem p03585 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--)
#define IREP(i, n) IFOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
#define Lower_bound(v, x)                                                      \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x)                                                      \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define int long long
#define INF 1000000000000000000
using namespace std;

typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> Pii;
typedef pair<double, int> Pdi;

template <typename T> void readvec(vector<T> &a);
void readindex(vector<int> &a);

class BIT {
public:
  vector<int> bit;
  int M;

  BIT(int M) : bit(vector<int>(M + 1, 0)), M(M) {}

  int sum(int i) {
    if (!i)
      return 0;
    return bit[i] + sum(i - (i & -i));
  }

  void add(int i, int x) {
    if (i > M)
      return;
    bit[i] += x;
    add(i + (i & -i), x);
  }
};

double calcans(int N, vector<double> &A, vector<double> &B, vector<double> &C) {
  // 傾き順にソート
  vector<Pdi> p(N);
  REP(i, N) p[i] = Pdi(-1.0 * A[i] / B[i], i);
  SORT(p);
  vector<int> invp(N);
  // 傾きの順位(1~N)
  REP(i, N) invp[p[i].second] = i + 1;

  int N_tot = N * (N - 1) / 2;

  double x1 = -1.0e10, x2 = 1.0e10;
  while (x2 - x1 > 1.0e-12) {
    double x = (x1 + x2) / 2.0;
    vector<Pdi> y(N);
    // y(x)でソート
    REP(i, N) y[i] = Pdi(-1.0 * A[i] / B[i] * x + C[i] / B[i], i);
    SORT(y);
    int count = 0;
    BIT b(N);
    REP(i, N) {
      int i0 = y[i].second;
      count += b.sum(invp[i0]);
      b.add(invp[i0], 1);
    }
    if (count >= (N_tot + 1) / 2)
      x2 = x;
    else
      x1 = x;
  }
  return x2;
}

signed main() {

  int N;
  cin >> N;
  vector<double> A(N), B(N), C(N);
  REP(i, N) cin >> A[i] >> B[i] >> C[i];
  cout << setprecision(15) << calcans(N, A, B, C) << " " << calcans(N, B, A, C);

  return 0;
}

template <typename T> void readvec(vector<T> &a) {
  REP(i, a.size()) { cin >> a[i]; }
}
void readindex(vector<int> &a) {
  REP(i, a.size()) {
    cin >> a[i];
    a[i]--;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--)
#define IREP(i, n) IFOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
#define Lower_bound(v, x)                                                      \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x)                                                      \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define int long long
#define INF 1000000000000000000
using namespace std;

typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> Pii;
typedef pair<double, int> Pdi;

template <typename T> void readvec(vector<T> &a);
void readindex(vector<int> &a);

class BIT {
public:
  vector<int> bit;
  int M;

  BIT(int M) : bit(vector<int>(M + 1, 0)), M(M) {}

  int sum(int i) {
    if (!i)
      return 0;
    return bit[i] + sum(i - (i & -i));
  }

  void add(int i, int x) {
    if (i > M)
      return;
    bit[i] += x;
    add(i + (i & -i), x);
  }
};

double calcans(int N, vector<double> &A, vector<double> &B, vector<double> &C) {
  // 傾き順にソート
  vector<Pdi> p(N);
  REP(i, N) p[i] = Pdi(-1.0 * A[i] / B[i], i);
  SORT(p);
  vector<int> invp(N);
  // 傾きの順位(1~N)
  REP(i, N) invp[p[i].second] = i + 1;

  int N_tot = N * (N - 1) / 2;

  double x1 = -1.0e10, x2 = 1.0e10;
  while (x2 - x1 > max(1.0e-12, max(abs(x1), abs(x2)) * 1.0e-12)) {
    double x = (x1 + x2) / 2.0;
    vector<Pdi> y(N);
    // y(x)でソート
    REP(i, N) y[i] = Pdi(-1.0 * A[i] / B[i] * x + C[i] / B[i], i);
    SORT(y);
    int count = 0;
    BIT b(N);
    REP(i, N) {
      int i0 = y[i].second;
      count += b.sum(invp[i0]);
      b.add(invp[i0], 1);
    }
    if (count >= (N_tot + 1) / 2)
      x2 = x;
    else
      x1 = x;
  }
  return x2;
}

signed main() {

  int N;
  cin >> N;
  vector<double> A(N), B(N), C(N);
  REP(i, N) cin >> A[i] >> B[i] >> C[i];
  cout << setprecision(15) << calcans(N, A, B, C) << " " << calcans(N, B, A, C);

  return 0;
}

template <typename T> void readvec(vector<T> &a) {
  REP(i, a.size()) { cin >> a[i]; }
}
void readindex(vector<int> &a) {
  REP(i, a.size()) {
    cin >> a[i];
    a[i]--;
  }
}