Problem p03736 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
#define N 262144
lint dat[2][N * 2 + 10];
lint inf = 1145141919810364364LL;
//[a,b)の最小値
// 外からは(a,b,0,0,n)として呼ぶ
lint query(int a, int b, int id, int k = 0, int l = 0, int r = N) {
  if (r <= a || b <= l)
    return inf;
  if (a <= l && r <= b)
    return dat[id][k];
  lint vl = query(a, b, id, k * 2 + 1, l, (l + r) / 2);
  lint vr = query(a, b, id, k * 2 + 2, (l + r) / 2, r);
  return min(vl, vr);
}
void update(int id, int k, int a) {
  k += N - 1;
  dat[id][k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[id][k] = min(dat[id][k * 2 + 1], dat[id][k * 2 + 2]);
  }
  return;
}
int x[252521];
int main() {
  lint sum = 0, out = inf, out2 = inf;
  int n, q, a, b;
  cin >> n >> q >> a >> b;
  x[0] = a;
  rep(i, 2) rep(j, N * 2 + 5) dat[i][j] = inf;
  update(0, b, n - b);
  update(1, b, b);
  rep(i, q) cin >> x[i + 1];
  rep(i, q) {
    // rep(j,n+1) cout<<dat[0][j+N-1]-(n-j)<<',';cout<<endl;
    lint dif = abs(x[i] - x[i + 1]);
    sum += dif;
    lint ne = min(query(0, x[i + 1] + 1, 0) - (n - x[i + 1]),
                  query(x[i + 1], n + 1, 1) - x[i + 1]);
    // cout<<ne<<endl;
    update(0, x[i], ne - dif + n - x[i]);
    update(1, x[i], ne - dif + x[i]);
  }
  rep(i, n + 1) out = min(out, dat[0][i + N - 1] - (n - i));
  rep(i, n + 1) out2 = min(out2, dat[1][i + N - 1] - i);
  assert(out == out2);
  cout << out + sum << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
#define N 262144
lint dat[2][N * 2 + 10];
lint inf = 1145141919810364364LL;
//[a,b)の最小値
// 外からは(a,b,0,0,n)として呼ぶ
lint query(int a, int b, int id, int k = 0, int l = 0, int r = N) {
  if (r <= a || b <= l)
    return inf;
  if (a <= l && r <= b)
    return dat[id][k];
  lint vl = query(a, b, id, k * 2 + 1, l, (l + r) / 2);
  lint vr = query(a, b, id, k * 2 + 2, (l + r) / 2, r);
  return min(vl, vr);
}
void update(int id, int k, lint a) {
  k += N - 1;
  dat[id][k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[id][k] = min(dat[id][k * 2 + 1], dat[id][k * 2 + 2]);
  }
  return;
}
int x[252521];
int main() {
  lint sum = 0, out = inf, out2 = inf;
  int n, q, a, b;
  cin >> n >> q >> a >> b;
  x[0] = a;
  rep(i, 2) rep(j, N * 2 + 5) dat[i][j] = inf;
  update(0, b, n - b);
  update(1, b, b);
  rep(i, q) cin >> x[i + 1];
  rep(i, q) {
    // rep(j,n+1) cout<<dat[0][j+N-1]-(n-j)<<',';cout<<endl;
    lint dif = abs(x[i] - x[i + 1]);
    sum += dif;
    lint ne = min(query(0, x[i + 1] + 1, 0) - (n - x[i + 1]),
                  query(x[i + 1], n + 1, 1) - x[i + 1]);
    // cout<<ne<<endl;
    update(0, x[i], ne - dif + n - x[i]);
    update(1, x[i], ne - dif + x[i]);
  }
  rep(i, n + 1) out = min(out, dat[0][i + N - 1] - (n - i));
  rep(i, n + 1) out2 = min(out2, dat[1][i + N - 1] - i);
  assert(out == out2);
  cout << out + sum << endl;
}