Problem p03940 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#define int long long
using namespace std;
const int N = 1e5 + 5;
const long long inf = 2e12 + 7;
long long dp[N], dp1[4 * N], lazy[4 * N], pos[N];
void upd(int idx, int l, int r, int lef, int rig, int val) {
  if (l > rig || r < lef) {
    return;
  }
  if (l >= lef && r <= rig) {
    if (dp1[idx] <= val) {
      return;
    } else {
      if (dp1[idx] > val) {
        lazy[idx] = val;
        dp1[idx] = val;
      }
      return;
    }
  }
  if (lazy[idx] != inf) {
    dp1[2 * idx] = min(lazy[idx], dp1[2 * idx]);
    dp1[2 * idx + 1] = min(dp1[2 * idx + 1], lazy[idx]);
    lazy[2 * idx] = min(lazy[2 * idx], lazy[idx]);
    lazy[2 * idx + 1] = min(lazy[2 * idx + 1], lazy[idx]);
    lazy[idx] = inf;
  }
  upd(2 * idx, l, (l + r) / 2, lef, rig, val);
  upd(2 * idx + 1, (l + r) / 2 + 1, r, lef, rig, val);
  dp1[idx] = max(dp[2 * idx], dp[2 * idx + 1]);
}
long long getans(int idx, int l, int r, int pos1) {
  if (l > pos1 || r < pos1) {
    return inf;
  }
  if (l == r) {
    return dp1[idx];
  }
  if (lazy[idx] != inf) {
    dp1[2 * idx] = min(lazy[idx], dp1[2 * idx]);
    dp1[2 * idx + 1] = min(dp1[2 * idx + 1], lazy[idx]);
    lazy[2 * idx] = min(lazy[2 * idx], lazy[idx]);
    lazy[2 * idx + 1] = min(lazy[2 * idx + 1], lazy[idx]);
    lazy[idx] = inf;
  }
  return min(getans(2 * idx, l, (l + r) / 2, pos1),
             getans(2 * idx + 1, (l + r) / 2 + 1, r, pos1));
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, end, time, i, j, k;
  cin >> n >> end >> time;
  for (i = 1; i <= n; i++) {
    cin >> pos[i];
  }
  for (i = 1; i < N; i++) {
    dp[i] = inf;
  }
  for (i = 1; i < 4 * N; i++) {
    dp1[i] = inf;
  }
  for (i = 1; i < 4 * N; i++) {
    lazy[i] = inf;
  }
  for (i = 0; i < n; i++) {
    if (i != 0) {
      dp[i] = min(dp[i], getans(1, 1, n, i) + 3 * pos[i]);
    }
    k = upper_bound(pos + 1, pos + 1 + n, (time + 2 * pos[i + 1]) / 2) - pos -
        1;
    dp[k] = min(dp[k], dp[i] + time + pos[k] - pos[i]);
    if (k != n) {
      upd(1, 1, n, k + 1, n, dp[i] - 2 * pos[i + 1] - pos[i]);
    }
  }
  dp[n] = min(dp[n], getans(1, 1, n, n) + 3 * pos[n]);
  cout << dp[n] + end - pos[n];
}

Fixed submission:
#include <algorithm>
#include <iostream>
#define int long long
using namespace std;
const int N = 1e5 + 5;
const long long inf = 2e12 + 7;
long long dp[N], dp1[4 * N], lazy[4 * N], pos[N];
void upd(int idx, int l, int r, int lef, int rig, int val) {
  if (l > rig || r < lef) {
    return;
  }
  if (l >= lef && r <= rig) {
    if (dp1[idx] <= val) {
      return;
    } else {
      if (dp1[idx] > val) {
        lazy[idx] = val;
        dp1[idx] = val;
      }
      return;
    }
  }
  if (lazy[idx] != inf) {
    dp1[2 * idx] = min(lazy[idx], dp1[2 * idx]);
    dp1[2 * idx + 1] = min(dp1[2 * idx + 1], lazy[idx]);
    lazy[2 * idx] = min(lazy[2 * idx], lazy[idx]);
    lazy[2 * idx + 1] = min(lazy[2 * idx + 1], lazy[idx]);
    lazy[idx] = inf;
  }
  upd(2 * idx, l, (l + r) / 2, lef, rig, val);
  upd(2 * idx + 1, (l + r) / 2 + 1, r, lef, rig, val);
  dp1[idx] = max(dp1[2 * idx], dp1[2 * idx + 1]);
}
long long getans(int idx, int l, int r, int pos1) {
  if (l > pos1 || r < pos1) {
    return inf;
  }
  if (l == r) {
    return dp1[idx];
  }
  if (lazy[idx] != inf) {
    dp1[2 * idx] = min(lazy[idx], dp1[2 * idx]);
    dp1[2 * idx + 1] = min(dp1[2 * idx + 1], lazy[idx]);
    lazy[2 * idx] = min(lazy[2 * idx], lazy[idx]);
    lazy[2 * idx + 1] = min(lazy[2 * idx + 1], lazy[idx]);
    lazy[idx] = inf;
  }
  return min(getans(2 * idx, l, (l + r) / 2, pos1),
             getans(2 * idx + 1, (l + r) / 2 + 1, r, pos1));
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, end, time, i, j, k;
  cin >> n >> end >> time;
  for (i = 1; i <= n; i++) {
    cin >> pos[i];
  }
  for (i = 1; i < N; i++) {
    dp[i] = inf;
  }
  for (i = 1; i < 4 * N; i++) {
    dp1[i] = inf;
  }
  for (i = 1; i < 4 * N; i++) {
    lazy[i] = inf;
  }
  for (i = 0; i < n; i++) {
    if (i != 0) {
      dp[i] = min(dp[i], getans(1, 1, n, i) + 3 * pos[i]);
    }
    k = upper_bound(pos + 1, pos + 1 + n, (time + 2 * pos[i + 1]) / 2) - pos -
        1;
    dp[k] = min(dp[k], dp[i] + time + pos[k] - pos[i]);
    if (k != n) {
      upd(1, 1, n, k + 1, n, dp[i] - 2 * pos[i + 1] - pos[i]);
    }
  }
  dp[n] = min(dp[n], getans(1, 1, n, n) + 3 * pos[n]);
  cout << dp[n] + end - pos[n];
}