Problem p01337 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;

long double a, b, c, d;
int n;

long double calc(long double pos) {
  return a * pos * pos * pos + b * pos * pos + c * pos + d;
}

long double find_answer(long double L, long double R, bool ok) {
  long double p1 = calc(L), p2 = calc(R);
  if (p1 > p2)
    swap(p1, p2);
  if (!(p1 <= 1e-14 && -1e-14 <= p2))
    return -1e9;

  long double M;
  for (int i = 0; i < 100; i++) {
    M = (L + R) / 2;
    long double ret = calc(M);
    bool res = false;
    if (ret > 0)
      res = true;

    if ((ok ^ res) == false)
      R = M;
    else
      L = M;
  }
  return M;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a >> b >> c >> d;
    d /= a;
    c /= a;
    b /= a;
    a = 1;

    long double A = 3.0 * a, B = 2.0 * b, C = c;
    vector<tuple<long double, long double, bool>> T;
    if (B * B - 4.0 * A * C < -1e-5) {
      T.push_back(make_tuple(-10000, 10000, true));
    } else {
      long double V = sqrtl(B * B - 4.0 * A * C);
      long double W1 = (-B - V) / (2.0 * A);
      long double W2 = (-B + V) / (2.0 * A);
      T.push_back(make_tuple(-10000, W1, true));
      T.push_back(make_tuple(W1, W2, false));
      T.push_back(make_tuple(W2, 10000, true));
    }
    vector<long double> U;
    for (int j = 0; j < T.size(); j++) {
      long double V = find_answer(get<0>(T[j]), get<1>(T[j]), get<2>(T[j]));
      if (V >= -1e8)
        U.push_back(V);
    }
    sort(U.begin(), U.end());

    int t1 = 0, t2 = 0;
    for (int j = 0; j < U.size(); j++) {
      if (U[j] < -1e-5)
        t1++;
      else
        t2++;
    }
    if (fabs(d) < 1e-10)
      t2--;
    if (fabs(c) < 1e-10 && fabs(d) < 1e-10)
      t2--;
    if (fabs(b) < 1e-10 && fabs(c) < 1e-10 && fabs(d) < 1e-10)
      t2--;
    cout << t2 << " " << t1 << endl;
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
long double a, b, c, d;
vector<pair<long double, long double>> vec;
int cnt1, cnt2;
long double kai(long double x) { return x * x * x * a + x * x * b + x * c + d; }
pair<long double, long double> kai2(long double ax, long double bx,
                                    long double cx) {
  bx /= ax;
  cx /= ax;
  ax = 1.0l;
  long double D = bx * bx - 4.0l * ax * cx;
  if (D < 0) {
    return make_pair(-1e9, -1e9);
  }
  long double E = sqrtl(D);
  long double F = (-1.0l * bx + E) / 2.0l;
  long double G = (-1.0l * bx - E) / 2.0l;
  return make_pair(G, F);
}
int main() {
  int n;
  cin >> n;
  for (int h = 0; h < n; h++) {
    cin >> a >> b >> c >> d;
    vec.clear();
    cnt1 = 0;
    cnt2 = 0;
    b /= a;
    c /= a;
    d /= a;
    a = 1.0l;
    for (long double i = -101.00031415926l; i <= 101.0l; i += 1e-4) {
      vec.push_back(make_pair(kai(i), i));
    }
    long double l = 1e9, r = 1e9, m;
    int type = 0;
    for (int i = 0; i < vec.size() - 1; i++) {
      if (vec[i].first < 0.0l && vec[i + 1].first > 0.0l) {
        l = vec[i].second;
        r = vec[i + 1].second;
        type = 1;
      }
      if (vec[i].first > 0.0l && vec[i + 1].first < 0.0l) {
        l = vec[i].second;
        r = vec[i + 1].second;
        type = 2;
      }
    }
    if (type == 1) {
      for (int i = 0; i < 1000; i++) {
        m = (l + r) / 2.0l;
        if (kai(m) < 0.0l)
          l = m;
        if (kai(m) > 0.0l)
          r = m;
      }
    }
    if (type == 2) {
      for (int i = 0; i < 1000; i++) {
        m = (l + r) / 2.0l;
        if (kai(m) < 0.0l)
          r = m;
        if (kai(m) > 0.0l)
          l = m;
      }
    }
    if (fabs(m) < 1e-50)
      type = 0;
    if (type == 0) {
      cout << "0 0" << endl;
      continue;
    }
    long double a1 = a, b1 = b, c1 = c, d1 = d;
    long double e1 = a1;
    a1 -= e1;
    b1 += m * e1;
    long double e2 = b1;
    b1 -= e2;
    c1 += m * e2;
    long double e3 = c1;
    c1 -= e3;
    d1 += m * e3;

    long double ans1 = m;
    long double ans2 = kai2(e1, e2, e3).first;
    long double ans3 = kai2(e1, e2, e3).second;

    if (fabs(ans1) > 1e-8) {
      if (ans1 > 0.0l)
        cnt1++;
      if (ans1 < 0.0l)
        cnt2++;
    }
    if (fabs(ans2) > 1e-8) {
      if (ans2 > 0.0l)
        cnt1++;
      if (ans2 < 0.0l)
        cnt2++;
    }
    if (fabs(ans3) > 1e-8) {
      if (ans3 > 0.0l)
        cnt1++;
      if (ans3 < 0.0l)
        cnt2++;
    }

    cout << cnt1 << ' ' << cnt2 << endl;
  }
  return 0;
}