Problem p03940 (C++)

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

const int N = 1e5 + 7;
#define mid ((s + e) / 2)
#define int long long

int n, ed, tad;
int x[N];
int dp[N];
int seg[N * 4];
int laz[N * 4];
int get(int l, int r, int v = 1, int s = 0, int e = N) {
  if (r <= s || e <= l)
    return 2e18;
  if (l <= s && e <= r)
    return seg[v];
  seg[v * 2] += laz[v];
  seg[v * 2 + 1] += laz[v];
  laz[v] = 0;
  return min(get(l, r, v * 2, s, mid), get(l, r, v * 2 + 1, mid, e));
}
void add(int l, int r, int val, int v = 1, int s = 0, int e = N) {
  if (r <= s || e <= l)
    return;
  if (l <= s && e <= r) {
    seg[v] += val;
    laz[v] += val;
    return;
  }
  seg[v * 2] += laz[v];
  seg[v * 2 + 1] += laz[v];
  laz[v * 2] += laz[v];
  laz[v * 2 + 1] += laz[v];
  laz[v] = 0;
  add(l, r, val, v * 2, s, mid);
  add(l, r, val, v * 2 + 1, mid, e);
  seg[v] = min(seg[v * 2], seg[v * 2 + 1]);
}
int tmp[N];
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> ed >> tad;
  int pnt = -1;
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
    tmp[i] = 2e18;
    for (int j = 1; j <= i; j++) {
      tmp[i] = min(tmp[i], tmp[j - 1] + x[j] - x[j - 1] +
                               max(2 * (x[i] - x[j]), tad) + x[i] - x[j]);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i == 1) {
      add(0, 1, 0 + x[1] - 0 + tad);
    }
    add(0, pnt, (x[i] - x[i - 1]) * 2);
    while ((x[i] - x[pnt + 1]) * 2 >= tad) {
      add(pnt, pnt + 1, (x[i] - x[pnt + 1]) * 2 - tad);
      //			cout << "pnt << " << pnt << endl;
      pnt++;
    }
    add(0, i - 1, x[i] - x[i - 1]);
    /*		cout << "all done : " << endl;
                    for (int j = 0; j < i; j++)
                            cout << j << " > " << get(j, j + 1) << "    ";
                    cout << endl;*/
    dp[i] = get(0, i);
    //		cout << "dp[i] : " << i << " " << dp[i] << " " << tmp[i] << endl <<
    //endl << endl;
    add(i, i + 1, dp[i] + x[i + 1] - x[i] + tad);
  }
  cout << ed - x[n] + dp[n];

  return 0;
}


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

const int N = 1e5 + 7;
#define mid ((s + e) / 2)
#define int long long

int n, ed, tad;
int x[N];
int dp[N];
int seg[N * 4];
int laz[N * 4];
int get(int l, int r, int v = 1, int s = 0, int e = N) {
  if (r <= s || e <= l)
    return 2e18;
  if (l <= s && e <= r)
    return seg[v];
  seg[v * 2] += laz[v];
  seg[v * 2 + 1] += laz[v];
  laz[v] = 0;
  return min(get(l, r, v * 2, s, mid), get(l, r, v * 2 + 1, mid, e));
}
void add(int l, int r, int val, int v = 1, int s = 0, int e = N) {
  if (r <= s || e <= l)
    return;
  if (l <= s && e <= r) {
    seg[v] += val;
    laz[v] += val;
    return;
  }
  seg[v * 2] += laz[v];
  seg[v * 2 + 1] += laz[v];
  laz[v * 2] += laz[v];
  laz[v * 2 + 1] += laz[v];
  laz[v] = 0;
  add(l, r, val, v * 2, s, mid);
  add(l, r, val, v * 2 + 1, mid, e);
  seg[v] = min(seg[v * 2], seg[v * 2 + 1]);
}
int tmp[N];
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> ed >> tad;
  int pnt = -1;
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
  }
  for (int i = 1; i <= n; i++) {
    if (i == 1) {
      add(0, 1, 0 + x[1] - 0 + tad);
    }
    add(0, pnt, (x[i] - x[i - 1]) * 2);
    while ((x[i] - x[pnt + 1]) * 2 >= tad) {
      add(pnt, pnt + 1, (x[i] - x[pnt + 1]) * 2 - tad);
      //			cout << "pnt << " << pnt << endl;
      pnt++;
    }
    add(0, i - 1, x[i] - x[i - 1]);
    /*		cout << "all done : " << endl;
                    for (int j = 0; j < i; j++)
                            cout << j << " > " << get(j, j + 1) << "    ";
                    cout << endl;*/
    dp[i] = get(0, i);
    //		cout << "dp[i] : " << i << " " << dp[i] << " " << tmp[i] << endl <<
    //endl << endl;
    add(i, i + 1, dp[i] + x[i + 1] - x[i] + tad);
  }
  cout << ed - x[n] + dp[n];

  return 0;
}