Problem p03940 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 100005;

const long long INF = 1e18;

int n, a[MAXN], e, t;

#define lc (nd << 1)
#define rc (nd << 1 | 1)

struct node {
  long long x, y;
  node() { x = y = INF; }
  node(long long _x, long long _y) { x = _x, y = _y; }
  friend node operator+(const node &a, const node &b) {
    return (node){min(a.x, b.x), min(a.y, b.y)};
  }
};

node dat[MAXN * 4];
long long tag[MAXN * 4];

inline void update(int nd) { dat[nd] = dat[lc] + dat[rc]; }
inline void put_tag(int nd, int dt) {
  dat[nd].x += dt, dat[nd].y += dt, tag[nd] += dt;
}
inline void pdw(int nd) {
  put_tag(lc, tag[nd]);
  put_tag(rc, tag[nd]);
  tag[nd] = 0;
}
inline void modify(int nd, int L, int R, int opl, int opr, int dt) {
  pdw(nd);
  if (L == opl && R == opr)
    put_tag(nd, dt);
  else {
    int mid = (L + R) >> 1;
    if (opr <= mid)
      modify(lc, L, mid, opl, opr, dt);
    else if (opl > mid)
      modify(rc, mid + 1, R, opl, opr, dt);
    else
      modify(lc, L, mid, opl, mid, dt),
          modify(rc, mid + 1, R, mid + 1, opr, dt);
    update(nd);
  }
}
inline void modify(int nd, int L, int R, int pos, const node &pt) {
  pdw(nd);
  if (L == R)
    dat[nd] = pt;
  else {
    int mid = (L + R) >> 1;
    if (pos <= mid)
      modify(lc, L, mid, pos, pt);
    else
      modify(rc, mid + 1, R, pos, pt);
    update(nd);
  }
}
inline node query(int nd, int L, int R, int opl, int opr) {
  if (opl > opr)
    return (node){INF, INF};
  pdw(nd);
  if (L == opl && R == opr)
    return dat[nd];
  else {
    int mid = (L + R) >> 1;
    if (opr <= mid)
      return query(lc, L, mid, opl, opr);
    else if (opl > mid)
      return query(rc, mid + 1, R, opl, opr);
    else
      return query(lc, L, mid, opl, mid) + query(rc, mid + 1, R, mid + 1, opr);
  }
}
const int root = 1;

int main() {
  scanf("%d%d%d", &n, &e, &t);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  modify(root, 0, n, 0, (node){0, -2 * a[1]});
  int pos = -1;
  long long dpv;
  for (int i = 1; i <= n; i++) {
    while (pos + 1 < i && 2ll * (a[i] - a[pos + 2]) > t)
      pos++;
    dpv = min(query(root, 0, n, 0, pos).y + 3ll * a[i] - a[i - 1],
              query(root, 0, n, pos + 1, i - 1).x + a[i] - a[i - 1] + t);
    modify(root, 0, n, i, (node){dpv, dpv - 2 * a[i + 1]});
    modify(root, 0, n, 0, i - 1, a[i] - a[i - 1]);
    // for (int j = 0; j < i; j++) dp[i][i] = min(dp[i][i],
    // dp[i-1][j]+a[i]-a[i-1]+max(t, 2*(a[i]-a[j+1])));
  }
  cout << dpv + e - a[n] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 100005;

const long long INF = 1e18;

int n, a[MAXN], e, t;

#define lc (nd << 1)
#define rc (nd << 1 | 1)

struct node {
  long long x, y;
  node() { x = y = INF; }
  node(long long _x, long long _y) { x = _x, y = _y; }
  friend node operator+(const node &a, const node &b) {
    return (node){min(a.x, b.x), min(a.y, b.y)};
  }
};

node dat[MAXN * 8];
long long tag[MAXN * 8];

inline void update(int nd) { dat[nd] = dat[lc] + dat[rc]; }
inline void put_tag(int nd, int dt) {
  dat[nd].x += dt, dat[nd].y += dt, tag[nd] += dt;
}
inline void pdw(int nd) {
  put_tag(lc, tag[nd]);
  put_tag(rc, tag[nd]);
  tag[nd] = 0;
}
inline void modify(int nd, int L, int R, int opl, int opr, int dt) {
  pdw(nd);
  if (L == opl && R == opr)
    put_tag(nd, dt);
  else {
    int mid = (L + R) >> 1;
    if (opr <= mid)
      modify(lc, L, mid, opl, opr, dt);
    else if (opl > mid)
      modify(rc, mid + 1, R, opl, opr, dt);
    else
      modify(lc, L, mid, opl, mid, dt),
          modify(rc, mid + 1, R, mid + 1, opr, dt);
    update(nd);
  }
}
inline void modify(int nd, int L, int R, int pos, const node &pt) {
  pdw(nd);
  if (L == R)
    dat[nd] = pt;
  else {
    int mid = (L + R) >> 1;
    if (pos <= mid)
      modify(lc, L, mid, pos, pt);
    else
      modify(rc, mid + 1, R, pos, pt);
    update(nd);
  }
}
inline node query(int nd, int L, int R, int opl, int opr) {
  if (opl > opr)
    return (node){INF, INF};
  pdw(nd);
  if (L == opl && R == opr)
    return dat[nd];
  else {
    int mid = (L + R) >> 1;
    if (opr <= mid)
      return query(lc, L, mid, opl, opr);
    else if (opl > mid)
      return query(rc, mid + 1, R, opl, opr);
    else
      return query(lc, L, mid, opl, mid) + query(rc, mid + 1, R, mid + 1, opr);
  }
}
const int root = 1;

int main() {
  scanf("%d%d%d", &n, &e, &t);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  modify(root, 0, n, 0, (node){0, -2 * a[1]});
  int pos = -1;
  long long dpv;
  for (int i = 1; i <= n; i++) {
    while (pos + 1 < i && 2ll * (a[i] - a[pos + 2]) > t)
      pos++;
    dpv = min(query(root, 0, n, 0, pos).y + 3ll * a[i] - a[i - 1],
              query(root, 0, n, pos + 1, i - 1).x + a[i] - a[i - 1] + t);
    modify(root, 0, n, i, (node){dpv, dpv - 2 * a[i + 1]});
    modify(root, 0, n, 0, i - 1, a[i] - a[i - 1]);
    // for (int j = 0; j < i; j++) dp[i][i] = min(dp[i][i],
    // dp[i-1][j]+a[i]-a[i-1]+max(t, 2*(a[i]-a[j+1])));
  }
  cout << dpv + e - a[n] << endl;
  return 0;
}