Problem p03940 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 2e5 + 10;
typedef long long LL;
const LL INF = 1e15 + 10;

int n;
LL x[maxn], e, t;
int ll[maxn], rr[maxn];
LL mini1[maxn], mini2[maxn];
LL dp[maxn];

void build(int p, int l, int r) {
  ll[p] = l;
  rr[p] = r;
  mini1[p] = mini2[p] = INF;
  if (l == r)
    return;
  build(p << 1, l, (l + r) >> 1);
  build((p << 1) | 1, ((l + r) >> 1) + 1, r);
  return;
}

void update(int p, int pos, LL val, int tp) {
  if (!tp)
    mini1[p] = min(mini1[p], val);
  else
    mini2[p] = min(mini2[p], val);
  if (ll[p] == rr[p])
    return;
  if (pos <= rr[p << 1])
    update(p << 1, pos, val, tp);
  else
    update((p << 1) | 1, pos, val, tp);
  return;
}

LL getmini(int p, int l, int r, int tp) {
  if (l > r)
    return INF;
  if (r < ll[p] || rr[p] < l)
    return INF;
  if (l <= ll[p] && rr[p] <= r) {
    if (!tp)
      return mini1[p];
    else
      return mini2[p];
  }
  return min(getmini(p << 1, l, r, tp), getmini((p << 1) | 1, l, r, tp));
}

int main() {
  scanf("%d%lld%lld", &n, &e, &t);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &x[i]);
  x[n + 1] = e;

  dp[0] = x[1];
  build(1, 0, n);
  update(1, 0, dp[0] - x[1], 0);
  update(1, 0, dp[0] - x[1] * 3ll, 1);
  for (int i = 1; i <= n; i++) {
    int tmp = lower_bound(x + 1, x + n + 1, x[i] - (t >> 1)) - x;
    dp[i] = min(getmini(1, 0, tmp - 2, 1) + 3ll * x[i],
                getmini(1, tmp - 1, i - 1, 0) + t + x[i]);
    dp[i] += x[i + 1] - x[i];
    update(1, i, dp[i] - x[i + 1], 0);
    update(1, i, dp[i] - x[i + 1] * 3ll, 1);
    // printf("%d %d\n",tmp,dp[i]);
  }
  printf("%lld\n", dp[n]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 4e5 + 10;
typedef long long LL;
const LL INF = 1e15 + 10;

int n;
LL x[maxn], e, t;
int ll[maxn], rr[maxn];
LL mini1[maxn], mini2[maxn];
LL dp[maxn];

void build(int p, int l, int r) {
  ll[p] = l;
  rr[p] = r;
  mini1[p] = mini2[p] = INF;
  if (l == r)
    return;
  build(p << 1, l, (l + r) >> 1);
  build((p << 1) | 1, ((l + r) >> 1) + 1, r);
  return;
}

void update(int p, int pos, LL val, int tp) {
  if (!tp)
    mini1[p] = min(mini1[p], val);
  else
    mini2[p] = min(mini2[p], val);
  if (ll[p] == rr[p])
    return;
  if (pos <= rr[p << 1])
    update(p << 1, pos, val, tp);
  else
    update((p << 1) | 1, pos, val, tp);
  return;
}

LL getmini(int p, int l, int r, int tp) {
  if (l > r)
    return INF;
  if (r < ll[p] || rr[p] < l)
    return INF;
  if (l <= ll[p] && rr[p] <= r) {
    if (!tp)
      return mini1[p];
    else
      return mini2[p];
  }
  return min(getmini(p << 1, l, r, tp), getmini((p << 1) | 1, l, r, tp));
}

int main() {
  scanf("%d%lld%lld", &n, &e, &t);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &x[i]);
  x[n + 1] = e;

  dp[0] = x[1];
  build(1, 0, n);
  update(1, 0, dp[0] - x[1], 0);
  update(1, 0, dp[0] - x[1] * 3ll, 1);
  for (int i = 1; i <= n; i++) {
    int tmp = lower_bound(x + 1, x + n + 1, x[i] - (t >> 1)) - x;
    dp[i] = min(getmini(1, 0, tmp - 2, 1) + 3ll * x[i],
                getmini(1, tmp - 1, i - 1, 0) + t + x[i]);
    dp[i] += x[i + 1] - x[i];
    update(1, i, dp[i] - x[i + 1], 0);
    update(1, i, dp[i] - x[i + 1] * 3ll, 1);
    // printf("%d %d\n",tmp,dp[i]);
  }
  printf("%lld\n", dp[n]);
  return 0;
}