Problem p03585 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}

typedef int VAL;
const int MAX_B = 610000;

int SIZE_B;
struct BIT {
  VAL dat[MAX_B + 1];

  void init(int n = 1) {
    SIZE_B = n;
    for (int i = 0; i <= SIZE_B; ++i)
      dat[i] = 0;
  }

  inline void add(int a, VAL x) {
    for (int i = a; i <= SIZE_B; i += i & -i)
      dat[i] = dat[i] + x;
  }

  inline VAL sum(int a) {
    VAL res = 0;
    for (int i = a; i > 0; i -= i & -i)
      res = res + dat[i];
    return res;
  }

  inline VAL sum(int a, int b) { return sum(b - 1) - sum(a - 1); }

  void print() {
    for (int i = 1; i <= SIZE_B; ++i)
      cout << sum(i, i + 1) << ",";
    cout << endl;
  }
};

BIT bit;

int N;
const int MAX = 510000;
typedef pair<pair<double, double>, double> LINE;
vector<LINE> lines;

bool cmpx(LINE a, LINE b) {
  return -a.first.first / a.first.second > -b.first.first / b.first.second;
}

bool cmpy(LINE a, LINE b) {
  return -a.first.second / a.first.first > -b.first.second / b.first.first;
}

double inter[MAX];
map<double, int> ma;
int id[MAX];

double solve(bool isx) {
  if (isx)
    sort(lines.begin(), lines.end(), cmpx);
  else
    sort(lines.begin(), lines.end(), cmpy);

  // COUT(lines);

  double lo = -(1LL << 30), hi = 1LL << 30;
  for (int iter = 0; iter < 200; ++iter) {
    double mid = (lo + hi) / 2;
    ma.clear();

    for (int i = 0; i < N; ++i) {
      LINE line = lines[i];
      double po = 0;
      if (isx)
        po = (line.second - line.first.first * mid) / line.first.second;
      else
        po = (line.second - line.first.second * mid) / line.first.first;
      inter[i] = po;
      ma[po] = 0;
    }
    int co = 0;
    for (map<double, int>::iterator it = ma.begin(); it != ma.end(); ++it) {
      it->second = co++;
    }
    for (int i = 0; i < N; ++i) {
      id[i] = ma[inter[i]];
      // cout << i << ": " << lines[i] << ", " << "(" << mid << ")" << inter[i]
      // << "; " << id[i] << endl;
    }

    int count = 0;
    bit.init(N + 5);
    for (int i = 0; i < N; ++i) {
      int add = id[i];
      int tmp = bit.sum(add + 2, N + 3);
      count += tmp;
      bit.add(add + 1, 1);
    }

    int aida = (N * (N - 1) / 2 - 1) / 2;
    if (count >= aida + 1)
      hi = mid;
    else
      lo = mid;

    // cout << mid << ": " << ", " << aida << ": " << count << endl;
  }

  return hi;
}

int main() {
  while (cin >> N) {
    lines.resize(N);
    for (int i = 0; i < N; ++i) {
      cin >> lines[i].first.first >> lines[i].first.second >> lines[i].second;
    }
    double X = solve(true);
    double Y = solve(false);
    cout << fixed << setprecision(10) << X << " " << Y << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}

typedef int VAL;
const int MAX_B = 610000;

int SIZE_B;
struct BIT {
  VAL dat[MAX_B + 1];

  void init(int n = 1) {
    SIZE_B = n;
    for (int i = 0; i <= SIZE_B; ++i)
      dat[i] = 0;
  }

  inline void add(int a, VAL x) {
    for (int i = a; i <= SIZE_B; i += i & -i)
      dat[i] = dat[i] + x;
  }

  inline VAL sum(int a) {
    VAL res = 0;
    for (int i = a; i > 0; i -= i & -i)
      res = res + dat[i];
    return res;
  }

  inline VAL sum(int a, int b) { return sum(b - 1) - sum(a - 1); }

  void print() {
    for (int i = 1; i <= SIZE_B; ++i)
      cout << sum(i, i + 1) << ",";
    cout << endl;
  }
};

BIT bit;

int N;
const int MAX = 510000;
typedef pair<pair<double, double>, double> LINE;
vector<LINE> lines;

bool cmpx(LINE a, LINE b) {
  return -a.first.first / a.first.second > -b.first.first / b.first.second;
}

bool cmpy(LINE a, LINE b) {
  return -a.first.second / a.first.first > -b.first.second / b.first.first;
}

double inter[MAX];
map<double, int> ma;
int id[MAX];

double solve(bool isx) {
  if (isx)
    sort(lines.begin(), lines.end(), cmpx);
  else
    sort(lines.begin(), lines.end(), cmpy);

  // COUT(lines);

  double lo = -(1LL << 30), hi = 1LL << 30;
  for (int iter = 0; iter < 100; ++iter) {
    double mid = (lo + hi) / 2;
    ma.clear();

    for (int i = 0; i < N; ++i) {
      LINE line = lines[i];
      double po = 0;
      if (isx)
        po = (line.second - line.first.first * mid) / line.first.second;
      else
        po = (line.second - line.first.second * mid) / line.first.first;
      inter[i] = po;
      ma[po] = 0;
    }
    int co = 0;
    for (map<double, int>::iterator it = ma.begin(); it != ma.end(); ++it) {
      it->second = co++;
    }
    for (int i = 0; i < N; ++i) {
      id[i] = ma[inter[i]];
      // cout << i << ": " << lines[i] << ", " << "(" << mid << ")" << inter[i]
      // << "; " << id[i] << endl;
    }

    int count = 0;
    bit.init(N + 5);
    for (int i = 0; i < N; ++i) {
      int add = id[i];
      int tmp = bit.sum(add + 2, N + 3);
      count += tmp;
      bit.add(add + 1, 1);
    }

    int aida = (N * (N - 1) / 2 - 1) / 2;
    if (count >= aida + 1)
      hi = mid;
    else
      lo = mid;

    // cout << mid << ": " << ", " << aida << ": " << count << endl;
  }

  return hi;
}

int main() {
  while (cin >> N) {
    lines.resize(N);
    for (int i = 0; i < N; ++i) {
      cin >> lines[i].first.first >> lines[i].first.second >> lines[i].second;
    }
    double X = solve(true);
    double Y = solve(false);
    cout << fixed << setprecision(10) << X << " " << Y << endl;
  }
}