Problem p03940 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop                                                                   \
  char nyaa;                                                                   \
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
const int mn = 1 << 17;
ll dp[mn], x[mn];
int n, nn;
ll dat[2][mn];
void init() {
  nn = 1;
  while (nn < n)
    nn *= 2;
  rep(i, 2 * nn - 1) dat[0][i] = dat[1][i] = INF;
}
void update(int id, int k, ll a) {
  k += nn - 1;
  dat[id][k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[id][k] = min(dat[id][k * 2 + 1], dat[id][k * 2 + 2]);
  }
}
ll query(int id, int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return INF;
  if (a <= l && r <= b)
    return dat[id][k];
  else {
    ll vl = query(id, a, b, k * 2 + 1, l, (l + r) / 2);
    ll vr = query(id, a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
}
int main() {
  cin >> n;
  n++;
  init();
  ll e, t;
  cin >> t >> e;
  rep(i, n - 1) { cin >> x[i]; }
  x[n - 1] = t;
  fill(dp, dp + n + 2, INF);
  dp[0] = x[0];
  update(0, 0, 0);
  update(1, 0, -2 * x[0]);
  rep1(i, n - 1) {
    int loc = lower_bound(x, x + n, x[i - 1] - e / 2) - x;
    ll mi1 = query(1, 0, loc, 0, 0, nn) + x[i] + 2 * x[i - 1];
    ll mi2 = query(0, loc, i, 0, 0, nn) + e + x[i];
    dp[i] = min(mi1, mi2);
    update(0, i, dp[i] - x[i]);
    update(1, i, dp[i] - 3 * x[i]);
  }
  cout << dp[n - 1] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop                                                                   \
  char nyaa;                                                                   \
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
const int mn = 1 << 17;
ll dp[mn], x[mn];
int n, nn;
ll dat[2][2 * mn - 1];
void init() {
  nn = 1;
  while (nn < n)
    nn *= 2;
  rep(i, 2 * nn - 1) dat[0][i] = dat[1][i] = INF;
}
void update(int id, int k, ll a) {
  k += nn - 1;
  dat[id][k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[id][k] = min(dat[id][k * 2 + 1], dat[id][k * 2 + 2]);
  }
}
ll query(int id, int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return INF;
  if (a <= l && r <= b)
    return dat[id][k];
  else {
    ll vl = query(id, a, b, k * 2 + 1, l, (l + r) / 2);
    ll vr = query(id, a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
}
int main() {
  cin >> n;
  n++;
  init();
  ll e, t;
  cin >> t >> e;
  rep(i, n - 1) { cin >> x[i]; }
  x[n - 1] = t;
  fill(dp, dp + n + 2, INF);
  dp[0] = x[0];
  update(0, 0, 0);
  update(1, 0, -2 * x[0]);
  rep1(i, n - 1) {
    int loc = lower_bound(x, x + n, x[i - 1] - e / 2) - x;
    ll mi1 = query(1, 0, loc, 0, 0, nn) + x[i] + 2 * x[i - 1];
    ll mi2 = query(0, loc, i, 0, 0, nn) + e + x[i];
    dp[i] = min(mi1, mi2);
    update(0, i, dp[i] - x[i]);
    update(1, i, dp[i] - 3 * x[i]);
  }
  cout << dp[n - 1] << endl;
  return 0;
}