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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pair<double, double>> Pdd;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

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

double dfs(int k, double r, double l) {
  if (T - E <= r && l <= T + E)
    return 1;
  if (r > T + E || l < T - E)
    return 0;

  if (k == 0) {
    if (T - E <= (r + l) / 2 && (r + l) / 2 <= T + E)
      return 1;
    return 0;
  }
  double res = 0;
  double mid = (r + l) / 2;
  if (mid < T) {
    res = dfs(k - 1, mid, l) * (1 - P) + dfs(k - 1, r, mid) * P;
  } else {
    res = dfs(k - 1, mid, l) * P + dfs(k - 1, r, mid) * (1 - P);
  }
  return res;
}

int main() {
  scanf("%d%lf%lf%lf%lf%lf", &K, &R, &L, &P, &E, &T);
  printf("%.20lf\\n", dfs(K, R, L));
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pair<double, double>> Pdd;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int K;
double R, L, P, E, T;
map<Pdd, double> m;

double dfs(int k, double r, double l) {
  if (T - E <= r && l <= T + E)
    return 1;
  if (r > T + E && l < T - E)
    return 0;
  if (m.find(MP(k, MP(r, l))) != m.end())
    return m[MP(k, MP(r, l))];
  if (k == 0) {
    if (T - E <= (r + l) / 2 && (r + l) / 2 <= T + E)
      return 1;
    return 0;
  }
  double res = 0;
  double mid = (r + l) / 2;
  if (mid < T) {
    res = dfs(k - 1, mid, l) * (1 - P) + dfs(k - 1, r, mid) * P;
  } else {
    res = dfs(k - 1, mid, l) * P + dfs(k - 1, r, mid) * (1 - P);
  }
  return m[MP(k, MP(r, l))] = res;
}

int main() {
  cin >> K >> R >> L >> P >> E >> T;
  cout << dfs(K, R, L) << endl;
  return 0;
}