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

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define SORT(x) sort((x).begin(), (x).end())
#define REP(i, x, y) for (int i = (x); i < (y); ++i)
#define MP(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

int k;
double r, l, p, e, t;

double DFS(int i, double left, double right) {
  double mid = (left + right) / 2;
  if (i == k) {
    if (e >= abs(mid - t))
      return 1;
    else
      return 0;
  }

  if (t - e <= left && right <= t + e)
    return 1;
  else if (right <= t - e || t + e <= left)
    return 0;

  if (mid >= t) {
    return DFS(i + 1, left, mid) * (1 - p) + DFS(i + 1, mid, right) * p;
  } else {
    return DFS(i + 1, mid, right) * (1 - p) + DFS(i + 1, left, mid) * p;
  }
}

void Solve() {
  cin >> k >> r >> l;
  cin >> p >> e >> t;

  cout << fixed << setprecision(6) << DFS(0, r, l) << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define SORT(x) sort((x).begin(), (x).end())
#define REP(i, x, y) for (int i = (x); i < (y); ++i)
#define MP(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

int k;
double r, l, p, e, t;

double DFS(int i, double left, double right) {
  double mid = (left + right) / 2;
  if (i == k) {
    if (t - e <= mid && mid <= t + e)
      return 1.0;
    else
      return 0.0;
  }
  if (mid >= t) {
    return DFS(i + 1, left, mid) * (1.0 - p) + DFS(i + 1, mid, right) * p;
  } else {
    return DFS(i + 1, mid, right) * (1.0 - p) + DFS(i + 1, left, mid) * p;
  }
}

void Solve() {
  cin >> k >> r >> l;
  cin >> p >> e >> t;

  cout << fixed << setprecision(6) << DFS(0, r, l) << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}