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

// テ・ツ淞コテヲツ慊ャテ」ツδ?」ツδウテ」ツδ療」ツδャテ」ツδシテ」ツδ?

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

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

double solve(int k, double lb, double ub, double q) {
  double mid = (ub + lb) / 2;
  double ret = 0.0;
  if (k == K) {
    if (T - E <= mid && mid <= T + E)
      return q;
    else
      return 0.0;
  }
  double a = T - E, b = T + E;
  if (mid < T) {
    // lb = mid
    // correct
    if (a <= mid && ub <= b)
      ret += q * (1 - P);
    else if (!(b < mid || ub < a))
      ret += solve(k + 1, mid, ub, q * (1 - P));

    // wrong
    if (a <= lb && mid <= b)
      ret += q * P;
    else if (!(b < lb || mid < a))
      ret += solve(k + 1, lb, mid, q * P);
  } else {
    // ub = mid
    // correct
    if (a <= lb && mid <= b)
      ret += q * (1 - P);
    else if (!(b < lb || mid < a))
      ret += solve(k + 1, lb, mid, q * (1 - P));

    // wrong
    if (a <= mid && ub <= b)
      ret += q * P;
    else if (!(b < mid || ub < a))
      ret += solve(k + 1, mid, ub, q * P);
  }
  return ret;
}

signed main() {
  cin >> K >> R >> L >> P >> E >> T;
  printf("%.12f\\n", solve(0, R, L, 1.0));
  return 0;
}

Error: TLE

Fixed submission:

// テ・ツ淞コテヲツ慊ャテ」ツδ?」ツδウテ」ツδ療」ツδャテ」ツδシテ」ツδ?

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

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

double solve(int k, double lb, double ub, double q) {
  // printf("k = %lld, lb = %.12f, ub = %.12f, q = %.12f\\n", k, lb, ub, q);
  double mid = (ub + lb) / 2;
  double ret = 0.0;
  if (k == K) {
    if (T - E <= mid && mid <= T + E)
      return q;
    else
      return 0.0;
  }
  if (mid < T) {
    // lb = mid
    // correct
    if (T - E <= mid && ub <= T + E)
      ret += q * (1 - P);
    else
      ret += solve(k + 1, mid, ub, q * (1 - P));

    // wrong
    if (T - E <= lb && mid <= T + E)
      ret += q * P;
    else
      ret += solve(k + 1, lb, mid, q * P);
  } else {
    // ub = mid
    // correct
    if (T - E <= lb && mid <= T + E)
      ret += q * (1 - P);
    else
      ret += solve(k + 1, lb, mid, q * (1 - P));

    // wrong
    if (T - E <= mid && ub <= T + E)
      ret += q * P;
    else
      ret += solve(k + 1, mid, ub, q * P);
  }
  return ret;
}

signed main() {
  cin >> K >> R >> L >> P >> E >> T;
  printf("%.12f\\n", solve(0, R, L, 1.0));
  return 0;
}