Problem p01418 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(i, N) for (ll i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100

ll sq(ll num) { return num * num; }
ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x % MOD;
  ll res = sq(mod_pow(x, n / 2));
  res %= MOD;
  if (n % 2 == 1) {
    res *= x;
    res %= MOD;
  }
  return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a * b % MOD; }

int k;
double R, L, p, e, t;

double solve(int cnt, double r, double l) {
  /*if (l + EPS < t - e || t + e + EPS < r)return 0.0;*/
  ////cout << cnt << " " << r << " " << l << endl;
  double h = (r + l) / 2;
  if (cnt == 0) {
    // cout << r << " " << l << " " << h << endl;
    if (t - e < h + EPS && h < t + e + EPS)
      return 1.0;
    return 0.0;
  }
  double res = 0;
  if (t < h + EPS) {
    res += (1 - p) * solve(cnt - 1, r, h);
    res += p * solve(cnt - 1, h, l);
  } else {
    res += (1 - p) * solve(cnt - 1, h, l);
    res += p * solve(cnt - 1, r, h);
  }
  // cout << "!" << cnt << " " << res << endl;
  return res;
}

int main() {
  cin >> k >> R >> L >> p >> e >> t;
  printf("%.10lf\n", solve(k, R, L));
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(i, N) for (ll i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100

ll sq(ll num) { return num * num; }
ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x % MOD;
  ll res = sq(mod_pow(x, n / 2));
  res %= MOD;
  if (n % 2 == 1) {
    res *= x;
    res %= MOD;
  }
  return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a * b % MOD; }

int k;
double R, L, p, e, t;

double solve(int cnt, double r, double l) {
  if (l + EPS < t - e || t + e + EPS < r)
    return 0.0;
  ////cout << cnt << " " << r << " " << l << endl;
  double h = (r + l) / 2;
  if (cnt == 0) {
    // cout << r << " " << l << " " << h << endl;
    if (t - e < h + EPS && h < t + e + EPS)
      return 1.0;
    return 0.0;
  }
  double res = 0;
  if (t < h + EPS) {
    res += (1 - p) * solve(cnt - 1, r, h);
    res += p * solve(cnt - 1, h, l);
  } else {
    res += (1 - p) * solve(cnt - 1, h, l);
    res += p * solve(cnt - 1, r, h);
  }
  // cout << "!" << cnt << " " << res << endl;
  return res;
}

int main() {
  cin >> k >> R >> L >> p >> e >> t;
  printf("%.10lf\n", solve(k, R, L));
}