Problem p03736 (C++)

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

typedef pair<int, int> pii;
typedef long long ll;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()
#define dump(x) cerr << #x << " = " << (x) << endl
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U> void chmin(T &t, const U &u) {
  if (t > u)
    t = u;
}
template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

const int SZ = 1 << 17;

template <class T> struct segtree {
  vector<T> dat;

  const T inf = numeric_limits<T>::max();

  void init() {
    dat.resize(SZ * 2);
    rep(i, SZ * 2) dat[i] = inf;
  }

  void update(int pos, T x) {
    pos += SZ - 1;
    dat[pos] = x;

    while (pos > 0) {
      pos = (pos - 1) / 2;
      dat[pos] = min(dat[pos * 2 + 1], dat[pos * 2 + 2]);
    }
  }

  T get(int a, int b, int k = 0, int l = 0, int r = SZ) {
    if (b <= l || r <= a)
      return inf;
    if (a <= l && r <= b)
      return dat[k];
    return min(get(a, b, k * 2 + 1, l, (l + r) / 2),
               get(a, b, k * 2 + 2, (l + r) / 2, r));
  }
};

segtree<ll> segp, segn;

const ll INF = TEN(18);

int main() {
  int N, Q, A, B;
  cin >> N >> Q >> A >> B;
  --A;
  --B;

  V<int> x(Q + 1);
  rep(i, Q) {
    cin >> x[i + 1];
    --x[i + 1];
  }
  x[0] = A;

  segp.init();
  segn.init();
  segp.update(B, B);
  segn.update(B, -B);

  ll off = 0;

  rep(i, Q) {
    int p = x[i], q = x[i + 1];
    ll d = abs(p - q);
    ll mn = INF;
    {
      ll l = segn.get(0, q + 1);
      if (l != segn.inf) {
        l = l + q - d;
      }
      chmin(mn, l);
    }
    {
      ll r = segp.get(q, N);
      if (r != segp.inf) {
        r = r - q - d;
      }
      chmin(mn, r);
    }

    segp.update(p, mn + p);
    segn.update(p, mn - p);

    off += d;
  }

  ll ans = INF;
  rep(i, N) {
    {
      ll x = segp.get(i, i + 1);
      if (x != segp.inf) {
        chmin(ans, x - i + off);
      }
    }
    {
      ll x = segn.get(i, i + 1);
      if (x != segn.inf) {
        chmin(ans, x + i + off);
      }
    }
  }
  cout << ans << endl;

  return 0;
}

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

typedef pair<int, int> pii;
typedef long long ll;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()
#define dump(x) cerr << #x << " = " << (x) << endl
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U> void chmin(T &t, const U &u) {
  if (t > u)
    t = u;
}
template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

const int SZ = 1 << 18;

template <class T> struct segtree {
  vector<T> dat;

  const T inf = numeric_limits<T>::max();

  void init() {
    dat.resize(SZ * 2);
    rep(i, SZ * 2) dat[i] = inf;
  }

  void update(int pos, T x) {
    pos += SZ - 1;
    dat[pos] = x;

    while (pos > 0) {
      pos = (pos - 1) / 2;
      dat[pos] = min(dat[pos * 2 + 1], dat[pos * 2 + 2]);
    }
  }

  T get(int a, int b, int k = 0, int l = 0, int r = SZ) {
    if (b <= l || r <= a)
      return inf;
    if (a <= l && r <= b)
      return dat[k];
    return min(get(a, b, k * 2 + 1, l, (l + r) / 2),
               get(a, b, k * 2 + 2, (l + r) / 2, r));
  }
};

segtree<ll> segp, segn;

const ll INF = TEN(18);

int main() {
  int N, Q, A, B;
  cin >> N >> Q >> A >> B;
  --A;
  --B;

  V<int> x(Q + 1);
  rep(i, Q) {
    cin >> x[i + 1];
    --x[i + 1];
  }
  x[0] = A;

  segp.init();
  segn.init();
  segp.update(B, B);
  segn.update(B, -B);

  ll off = 0;

  rep(i, Q) {
    int p = x[i], q = x[i + 1];
    ll d = abs(p - q);
    ll mn = INF;
    {
      ll l = segn.get(0, q + 1);
      if (l != segn.inf) {
        l = l + q - d;
      }
      chmin(mn, l);
    }
    {
      ll r = segp.get(q, N);
      if (r != segp.inf) {
        r = r - q - d;
      }
      chmin(mn, r);
    }

    segp.update(p, mn + p);
    segn.update(p, mn - p);

    off += d;
  }

  ll ans = INF;
  rep(i, N) {
    {
      ll x = segp.get(i, i + 1);
      if (x != segp.inf) {
        chmin(ans, x - i + off);
      }
    }
    {
      ll x = segn.get(i, i + 1);
      if (x != segn.inf) {
        chmin(ans, x + i + off);
      }
    }
  }
  cout << ans << endl;

  return 0;
}