Problem p03585 (C++)

Buggy submission (status: Runtime Error):
// #define LOCAL
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
#define SZ(x) (int)((x).size())
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  };
} fast_ios_;

struct Line {
  int a, b, c;
};

ostream &operator<<(ostream &os, const Line &obj) {
  os << obj.a << "," << obj.b << "," << obj.c;
  return os;
}

template <typename T, size_t N> struct fenwick {
  T c[N];
  void clear() { fill(c + 1, c + N, 0); }
  void add(int x, T y) {
    for (; x < N; x += x & -x) {
      c[x] += y;
    }
  }
  T sum(int x) {
    T ret = 0;
    for (; x; x -= x & -x) {
      ret += c[x];
    }
    return ret;
  }
  T query(int L, int R) { return sum(R) - sum(L - 1); }
};

const int N = 1e4 + 10;
fenwick<int, N> A;

double solve(vector<Line> &a) {
  int n = SZ(a);
  sort(a.begin(), a.end(), [](const Line &u, const Line &v) {
    return -1.0 * u.a / u.b > -1.0 * v.a / v.b;
  });
  int64 m = 1LL * n * (n - 1) / 2;
  m = (m + 1) / 2;
  double low = -1e10, high = 1e10;
  trace(m, a);
  for (int k = 0; k < 100; ++k) {
    double mid = (low + high) / 2;
    vector<pair<double, int>> p(n);
    for (int i = 0; i < n; ++i) {
      p[i].first = (a[i].c - a[i].a * mid) / a[i].b;
      p[i].second = i;
    }
    sort(p.begin(), p.end());
    A.clear();
    int64 cnt = 0;
    for (int i = 0; i < n; ++i) {
      cnt += A.sum(n - p[i].second);
      A.add(n - p[i].second, 1);
    }
    // trace(mid, cnt);
    if (cnt < m) {
      low = mid;
    } else {
      high = mid;
    }
  }
  return high;
}

int main() {
  int n;
  cin >> n;
  vector<Line> a(n);
  for (auto &x : a) {
    cin >> x.a >> x.b >> x.c;
  }
  double x = solve(a);
  for (auto &x : a) {
    swap(x.a, x.b);
  }
  double y = solve(a);
  cout << x << " " << y << '\n';
  return 0;
}


Fixed submission:
// #define LOCAL
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
#define SZ(x) (int)((x).size())
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  };
} fast_ios_;

struct Line {
  int a, b, c;
};

ostream &operator<<(ostream &os, const Line &obj) {
  os << obj.a << "," << obj.b << "," << obj.c;
  return os;
}

template <typename T, size_t N> struct fenwick {
  T c[N];
  void clear() { fill(c + 1, c + N, 0); }
  void add(int x, T y) {
    for (; x < N; x += x & -x) {
      c[x] += y;
    }
  }
  T sum(int x) {
    T ret = 0;
    for (; x; x -= x & -x) {
      ret += c[x];
    }
    return ret;
  }
  T query(int L, int R) { return sum(R) - sum(L - 1); }
};

const int N = 4e4 + 10;
fenwick<int, N> A;

double solve(vector<Line> &a) {
  int n = SZ(a);
  sort(a.begin(), a.end(), [](const Line &u, const Line &v) {
    return -1.0 * u.a / u.b > -1.0 * v.a / v.b;
  });
  int64 m = 1LL * n * (n - 1) / 2;
  m = (m + 1) / 2;
  double low = -1e10, high = 1e10;
  trace(m, a);
  for (int k = 0; k < 100; ++k) {
    double mid = (low + high) / 2;
    vector<pair<double, int>> p(n);
    for (int i = 0; i < n; ++i) {
      p[i].first = (a[i].c - a[i].a * mid) / a[i].b;
      p[i].second = i;
    }
    sort(p.begin(), p.end());
    A.clear();
    int64 cnt = 0;
    for (int i = 0; i < n; ++i) {
      cnt += A.sum(n - p[i].second);
      A.add(n - p[i].second, 1);
    }
    // trace(mid, cnt);
    if (cnt < m) {
      low = mid;
    } else {
      high = mid;
    }
  }
  return high;
}

int main() {
  int n;
  cin >> n;
  vector<Line> a(n);
  for (auto &x : a) {
    cin >> x.a >> x.b >> x.c;
  }
  double x = solve(a);
  for (auto &x : a) {
    swap(x.a, x.b);
  }
  double y = solve(a);
  cout << x << " " << y << '\n';
  return 0;
}