Problem p03736 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y))
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int const MOD = 1000000007;
ll mod_pow(ll x, ll n) {
  return (!n) ? 1 : (mod_pow((x * x) % MOD, n / 2) * ((n & 1) ? x : 1)) % MOD;
}
int madd(int a, int b) { return (a + b) % MOD; }
int msub(int a, int b) { return (a - b + MOD) % MOD; }
int mmul(int a, int b) { return (a * b) % MOD; }
int minv(int a) { return mod_pow(a, MOD - 2); }
int mdiv(int a, int b) { return mmul(a, minv(b)); }

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

int n, q, a, b;
int x[200010];
int node[1 << 18], node2[1 << 18];

// node [j] := dp[i][j] + j
// node2[j] := dp[i][j] - j にそれぞれ対応
struct LazySegTree {
  int SIZE;
  LazySegTree() {
    SIZE = 1;
    while (SIZE < n)
      SIZE *= 2;
    rep(i, 0, 2 * SIZE) node[i] = node2[i] = INF;
  }

  // x 番目に要素 val を入れる
  // この時にインデックス分の足し引きをしておく
  void setnum(int x, int val) {
    int p = x + SIZE - 1;
    node[p] = val + x;
    node2[p] = val - x;
    while (p > 0) {
      p = (p - 1) / 2;
      node[p] = min(node[2 * p + 1], node[2 * p + 2]);
      node2[p] = min(node2[2 * p + 1], node2[2 * p + 2]);
    }
  }

  // x 番目の要素取得 (一番下の段を見れば良い)
  int getnum(int x) { return node[x + SIZE - 1] - x; }

  // [a, b) の最小値
  int getmin1(int a, int b, int l = 0, int r = -1, int k = 0) {
    if (r == -1)
      r = SIZE;
    if (r <= a || b <= l)
      return INF;
    if (a <= l && r <= b)
      return node[k];

    int ans = INF;
    ans = min(ans, getmin1(a, b, l, (l + r) / 2, 2 * k + 1));
    ans = min(ans, getmin1(a, b, (l + r) / 2, r, 2 * k + 2));
    return ans;
  }

  int getmin2(int a, int b, int l = 0, int r = -1, int k = 0) {
    if (r == -1)
      r = SIZE;
    if (r <= a || b <= l)
      return INF;
    if (a <= l && r <= b)
      return node2[k];

    int ans = INF;
    ans = min(ans, getmin2(a, b, l, (l + r) / 2, 2 * k + 1));
    ans = min(ans, getmin2(a, b, (l + r) / 2, r, 2 * k + 2));
    return ans;
  }
};

signed main() {
  cin >> n >> q >> a >> b;
  a--;
  b--;
  x[0] = b;
  rep(i, 0, q) cin >> x[i + 1], x[i + 1]--;
  LazySegTree lz;
  lz.setnum(a, 0);
  int addall = 0;
  rep(i, 0, q) {
    int diff = abs(x[i + 1] - x[i]);

    // 前の x[i] 番目の値を取ってくる
    ll elem = lz.getnum(x[i]);

    // 0 <= j < x[i+1] について、min{ dp[i][j] + x[i+1] - j } を取る
    // dp[i][j] - j の部分に関しては SegTree で計算
    // (SegTree に全体加算は反映させないので diff 分引いてがんばる)
    elem = min(elem, lz.getmin2(0, x[i + 1]) + x[i + 1] - diff);

    // x[i+1] <= j < n について、min{ dp[i][j] - x[i+1] - j } を取る
    // dp[i][j] + j の部分に関しては SegTree で計算
    elem = min(elem, lz.getmin1(x[i + 1], n) - x[i + 1] - diff);
    lz.setnum(x[i], elem);
    addall += diff;
  }
  int ans = INF;
  rep(i, 0, n) ans = min(ans, lz.getnum(i));
  cout << ans + addall << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y))
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int const MOD = 1000000007;
ll mod_pow(ll x, ll n) {
  return (!n) ? 1 : (mod_pow((x * x) % MOD, n / 2) * ((n & 1) ? x : 1)) % MOD;
}
int madd(int a, int b) { return (a + b) % MOD; }
int msub(int a, int b) { return (a - b + MOD) % MOD; }
int mmul(int a, int b) { return (a * b) % MOD; }
int minv(int a) { return mod_pow(a, MOD - 2); }
int mdiv(int a, int b) { return mmul(a, minv(b)); }

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

int n, q, a, b;
int x[200010];
int node[1 << 19], node2[1 << 19];

// node [j] := dp[i][j] + j
// node2[j] := dp[i][j] - j にそれぞれ対応
struct LazySegTree {
  int SIZE;
  LazySegTree() {
    SIZE = 1;
    while (SIZE < n)
      SIZE *= 2;
    rep(i, 0, 2 * SIZE) node[i] = node2[i] = INF;
  }

  // x 番目に要素 val を入れる
  // この時にインデックス分の足し引きをしておく
  void setnum(int x, int val) {
    int p = x + SIZE - 1;
    node[p] = val + x;
    node2[p] = val - x;
    while (p > 0) {
      p = (p - 1) / 2;
      node[p] = min(node[2 * p + 1], node[2 * p + 2]);
      node2[p] = min(node2[2 * p + 1], node2[2 * p + 2]);
    }
  }

  // x 番目の要素取得 (一番下の段を見れば良い)
  int getnum(int x) { return node[x + SIZE - 1] - x; }

  // [a, b) の最小値
  int getmin1(int a, int b, int l = 0, int r = -1, int k = 0) {
    if (r == -1)
      r = SIZE;
    if (r <= a || b <= l)
      return INF;
    if (a <= l && r <= b)
      return node[k];

    int ans = INF;
    ans = min(ans, getmin1(a, b, l, (l + r) / 2, 2 * k + 1));
    ans = min(ans, getmin1(a, b, (l + r) / 2, r, 2 * k + 2));
    return ans;
  }

  int getmin2(int a, int b, int l = 0, int r = -1, int k = 0) {
    if (r == -1)
      r = SIZE;
    if (r <= a || b <= l)
      return INF;
    if (a <= l && r <= b)
      return node2[k];

    int ans = INF;
    ans = min(ans, getmin2(a, b, l, (l + r) / 2, 2 * k + 1));
    ans = min(ans, getmin2(a, b, (l + r) / 2, r, 2 * k + 2));
    return ans;
  }
};

signed main() {
  cin >> n >> q >> a >> b;
  a--;
  b--;
  x[0] = b;
  rep(i, 0, q) cin >> x[i + 1], x[i + 1]--;
  LazySegTree lz;
  lz.setnum(a, 0);
  int addall = 0;
  rep(i, 0, q) {
    int diff = abs(x[i + 1] - x[i]);

    // 前の x[i] 番目の値を取ってくる
    ll elem = lz.getnum(x[i]);

    // 0 <= j < x[i+1] について、min{ dp[i][j] + x[i+1] - j } を取る
    // dp[i][j] - j の部分に関しては SegTree で計算
    // (SegTree に全体加算は反映させないので diff 分引いてがんばる)
    elem = min(elem, lz.getmin2(0, x[i + 1]) + x[i + 1] - diff);

    // x[i+1] <= j < n について、min{ dp[i][j] - x[i+1] - j } を取る
    // dp[i][j] + j の部分に関しては SegTree で計算
    elem = min(elem, lz.getmin1(x[i + 1], n) - x[i + 1] - diff);
    lz.setnum(x[i], elem);
    addall += diff;
  }
  int ans = INF;
  rep(i, 0, n) ans = min(ans, lz.getnum(i));
  cout << ans + addall << endl;
  return 0;
}