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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
const double EPS = 1e-10;

class C {
public:
  double p, l, r;
  int k;
  C() {}
  C(double ll, double rr, int kk, double pp) {
    l = ll;
    r = rr;
    k = kk;
    p = pp;
  }
};

int K, R, L;
double P, E, T;

int check(double l, double r) {
  double a = T - E, b = T + E;
  if (a <= r && b >= l)
    return 1;
  if (l < a || r > b)
    return -1;
  return 0;
}

int main() {
  cin >> K >> R >> L;
  double ans = 0.0;
  cin >> P >> E >> T;
  queue<C> open;
  open.push(C(L, R, K, 1.0));
  while (!open.empty()) {
    C tmp = open.front();
    open.pop();
    double &l = tmp.l, &r = tmp.r, &p = tmp.p;
    int &k = tmp.k;
    double m = (l + r) / 2.0;
    if (k <= 0) {
      if (abs(m - T) + EPS <= E)
        ans += p;
      continue;
    }
    int f = check(l, r);
    if (f == -1)
      continue;
    if (f == 1) {
      ans += p;
      continue;
    }
    if (m + EPS >= T) {
      open.push(C(m, r, k - 1, p * (1.0 - P)));
      open.push(C(l, m, k - 1, p * P));
    } else {
      open.push(C(l, m, k - 1, p * (1.0 - P)));
      open.push(C(m, r, k - 1, p * P));
    }
  }
  printf("%.6lf\\n", ans);
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
const double EPS = 1e-10;

class C {
public:
  double p, l, r;
  int k;
  C() {}
  C(double ll, double rr, int kk, double pp) {
    l = ll;
    r = rr;
    k = kk;
    p = pp;
  }
};

int K, R, L;
double P, E, T;

int main() {
  cin >> K >> R >> L;
  double ans = 0.0;
  cin >> P >> E >> T;
  queue<C> open;
  open.push(C(L, R, K, 1.0));
  while (!open.empty()) {
    C tmp = open.front();
    open.pop();
    double &l = tmp.l, &r = tmp.r, &p = tmp.p;
    int &k = tmp.k;
    double m = (l + r) / 2.0;
    if (k <= 0) {
      if (abs(m - T) + EPS <= E)
        ans += p;
      continue;
    }
    if (m + EPS >= T) {
      open.push(C(m, r, k - 1, p * (1.0 - P)));
      open.push(C(l, m, k - 1, p * P));
    } else {
      open.push(C(l, m, k - 1, p * (1.0 - P)));
      open.push(C(m, r, k - 1, p * P));
    }
  }
  printf("%.6lf\\n", ans);
  return 0;
}