Problem p03736 (C++)

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

#define taskname ""
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define for0(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef vector<cd> vcd;
typedef vector<int> vi;

template <class T> using v2d = vector<vector<T>>;
template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

const int maxN = 2e5 + 10;
const int maxQ = 2e5 + 10;
const ll inf = 1e18;

int n, q, a, b, x[maxQ];
ll f[maxN * 4], g[maxN * 4], h[maxN * 4], lazy[maxN * 4];
int L, R, idx;
ll val;

void down(int x) {
  ll t = lazy[x];
  if (t == 0) {
    return;
  }
  lazy[x] = 0;
  lazy[x * 2] += t;
  lazy[x * 2 + 1] += t;
  f[x * 2] += t;
  f[x * 2 + 1] += t;
  g[x * 2] += t;
  g[x * 2 + 1] += t;
  h[x * 2] += t;
  h[x * 2 + 1] += t;
}

void update_r(int x, int lo, int hi) {
  if (lo > R || hi < L) {
    return;
  }
  if (lo >= L && hi <= R) {
    f[x] += val;
    g[x] += val;
    h[x] += val;
    lazy[x] += val;
    return;
  }
  down(x);
  int mid = (lo + hi) / 2;
  update_r(x * 2, lo, mid);
  update_r(x * 2 + 1, mid + 1, hi);
  f[x] = min(f[x * 2], f[x * 2 + 1]);
  g[x] = min(g[x * 2], g[x * 2 + 1]);
  h[x] = min(h[x * 2], h[x * 2 + 1]);
}

void update_p(int x, int lo, int hi) {
  if (lo == hi && lo == idx) {
    if (h[x] > val) {
      f[x] = val + idx;
      g[x] = val - idx;
      h[x] = val;
    }
    return;
  }
  down(x);
  int mid = (lo + hi) / 2;
  if (idx <= mid) {
    update_p(x * 2, lo, mid);
  } else {
    update_p(x * 2 + 1, mid + 1, hi);
  }
  f[x] = min(f[x * 2], f[x * 2 + 1]);
  g[x] = min(g[x * 2], g[x * 2 + 1]);
  h[x] = min(h[x * 2], h[x * 2 + 1]);
}

ll query_f(int x, int lo, int hi) {
  if (lo > R || hi < L) {
    return inf;
  }
  if (lo >= L && hi <= R) {
    return f[x];
  }
  down(x);
  int mid = (lo + hi) / 2;
  return min(query_f(x * 2, lo, mid), query_f(x * 2 + 1, mid + 1, hi));
}

ll query_g(int x, int lo, int hi) {
  if (lo > R || hi < L) {
    return inf;
  }
  if (lo >= L && hi <= R) {
    return g[x];
  }
  down(x);
  int mid = (lo + hi) / 2;
  return min(query_g(x * 2, lo, mid), query_g(x * 2 + 1, mid + 1, hi));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  freopen("flower0.in", "r", stdin);
  cin >> n >> q >> a >> b;
  for1(i, q) { cin >> x[i]; }
  fill(f + 1, f + n * 4 + 1, inf);
  fill(g + 1, g + n * 4 + 1, inf);
  fill(h + 1, h + n * 4 + 1, inf);
  idx = a, val = abs(x[1] - b);
  update_p(1, 1, n);
  idx = b, val = abs(x[1] - a);
  update_p(1, 1, n);
  fore(i, 2, q) {
    L = 1, R = x[i];
    ll d = query_g(1, 1, n) + x[i];
    L = x[i] + 1, R = n;
    uin(d, query_f(1, 1, n) - x[i]);
    L = 1, R = n, val = abs(x[i] - x[i - 1]);
    update_r(1, 1, n);
    idx = x[i - 1], val = d;
    update_p(1, 1, n);
  }
  cout << h[1];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define taskname ""
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define for0(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef vector<cd> vcd;
typedef vector<int> vi;

template <class T> using v2d = vector<vector<T>>;
template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

const int maxN = 2e5 + 10;
const int maxQ = 2e5 + 10;
const ll inf = 1e18;

int n, q, a, b, x[maxQ];
ll f[maxN * 4], g[maxN * 4], h[maxN * 4], lazy[maxN * 4];
int L, R, idx;
ll val;

void down(int x) {
  ll t = lazy[x];
  if (t == 0) {
    return;
  }
  lazy[x] = 0;
  lazy[x * 2] += t;
  lazy[x * 2 + 1] += t;
  f[x * 2] += t;
  f[x * 2 + 1] += t;
  g[x * 2] += t;
  g[x * 2 + 1] += t;
  h[x * 2] += t;
  h[x * 2 + 1] += t;
}

void update_r(int x, int lo, int hi) {
  if (lo > R || hi < L) {
    return;
  }
  if (lo >= L && hi <= R) {
    f[x] += val;
    g[x] += val;
    h[x] += val;
    lazy[x] += val;
    return;
  }
  down(x);
  int mid = (lo + hi) / 2;
  update_r(x * 2, lo, mid);
  update_r(x * 2 + 1, mid + 1, hi);
  f[x] = min(f[x * 2], f[x * 2 + 1]);
  g[x] = min(g[x * 2], g[x * 2 + 1]);
  h[x] = min(h[x * 2], h[x * 2 + 1]);
}

void update_p(int x, int lo, int hi) {
  if (lo == hi && lo == idx) {
    if (h[x] > val) {
      f[x] = val + idx;
      g[x] = val - idx;
      h[x] = val;
    }
    return;
  }
  down(x);
  int mid = (lo + hi) / 2;
  if (idx <= mid) {
    update_p(x * 2, lo, mid);
  } else {
    update_p(x * 2 + 1, mid + 1, hi);
  }
  f[x] = min(f[x * 2], f[x * 2 + 1]);
  g[x] = min(g[x * 2], g[x * 2 + 1]);
  h[x] = min(h[x * 2], h[x * 2 + 1]);
}

ll query_f(int x, int lo, int hi) {
  if (lo > R || hi < L) {
    return inf;
  }
  if (lo >= L && hi <= R) {
    return f[x];
  }
  down(x);
  int mid = (lo + hi) / 2;
  return min(query_f(x * 2, lo, mid), query_f(x * 2 + 1, mid + 1, hi));
}

ll query_g(int x, int lo, int hi) {
  if (lo > R || hi < L) {
    return inf;
  }
  if (lo >= L && hi <= R) {
    return g[x];
  }
  down(x);
  int mid = (lo + hi) / 2;
  return min(query_g(x * 2, lo, mid), query_g(x * 2 + 1, mid + 1, hi));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> q >> a >> b;
  for1(i, q) { cin >> x[i]; }
  fill(f + 1, f + n * 4 + 1, inf);
  fill(g + 1, g + n * 4 + 1, inf);
  fill(h + 1, h + n * 4 + 1, inf);
  idx = a, val = abs(x[1] - b);
  update_p(1, 1, n);
  idx = b, val = abs(x[1] - a);
  update_p(1, 1, n);
  fore(i, 2, q) {
    L = 1, R = x[i];
    ll d = query_g(1, 1, n) + x[i];
    L = x[i] + 1, R = n;
    uin(d, query_f(1, 1, n) - x[i]);
    L = 1, R = n, val = abs(x[i] - x[i - 1]);
    update_r(1, 1, n);
    idx = x[i - 1], val = d;
    update_p(1, 1, n);
  }
  cout << h[1];
  return 0;
}