Problem p03736 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define il inline
#define vd void
typedef long long ll;
il ll gi() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return x * f;
}
int n, q;
struct Segtree {
  ll all_tag;
  ll mn[800010];
#define mid ((l + r) >> 1)
  il vd update(int x, int l, int r, const int &p, const ll &d) {
    mn[x] = std::min(mn[x], d);
    if (l == r)
      return;
    if (p <= mid)
      update(x << 1, l, mid, p, d);
    else
      update(x << 1 | 1, mid + 1, r, p, d);
  }
  il vd Update(int p, ll d) {
    d -= all_tag;
    update(1, 1, n, p, d);
  }
  il ll query(int x, int l, int r, const int &L, const int &R) {
    if (L <= l && r <= R)
      return mn[x];
    if (L <= mid)
      if (mid < R)
        return std::min(query(x << 1, l, mid, L, R),
                        query(x << 1 | 1, mid + 1, r, L, R));
      else
        return query(x << 1, l, mid, L, R);
    else
      return query(x << 1 | 1, mid + 1, r, L, R);
  }
  il ll Query(int l, int r) { return query(1, 1, n, l, r) + all_tag; }
  il vd Update_all(ll d) { all_tag += d; }
#undef mid
  il vd clear() {
    memset(mn, 20, sizeof mn);
    all_tag = 0;
  }
} A, B; // A f[i]+i B f[i]-i
int cx[200010];
il ll solve(int posA, int posB) {
  A.clear(), B.clear();
  A.Update(posA, posA);
  B.Update(posA, -posA);
  int lst = posB, nx;
  for (int i = 1; i <= q; ++i) {
    nx = cx[i];
    ll newf = B.Query(1, nx) + nx;
    if (nx != n)
      newf = std::min(newf, A.Query(nx + 1, n) - nx);
    A.Update_all(abs(nx - lst));
    B.Update_all(abs(nx - lst));
    A.Update(lst, newf + lst);
    B.Update(lst, newf - lst);
    lst = nx;
  }
  ll ans = 9e18;
  for (int i = 1; i <= n; ++i)
    ans = std::min(ans, A.Query(i, i) - i);
  return ans;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  n = gi(), q = gi();
  int posA = gi(), posB = gi();
  for (int i = 1; i <= q; ++i)
    cx[i] = gi();
  printf("%lld\n", std::min(solve(posA, posB), solve(posB, posA)));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define il inline
#define vd void
typedef long long ll;
il ll gi() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return x * f;
}
int n, q;
struct Segtree {
  ll all_tag;
  ll mn[800010];
#define mid ((l + r) >> 1)
  il vd update(int x, int l, int r, const int &p, const ll &d) {
    mn[x] = std::min(mn[x], d);
    if (l == r)
      return;
    if (p <= mid)
      update(x << 1, l, mid, p, d);
    else
      update(x << 1 | 1, mid + 1, r, p, d);
  }
  il vd Update(int p, ll d) {
    d -= all_tag;
    update(1, 1, n, p, d);
  }
  il ll query(int x, int l, int r, const int &L, const int &R) {
    if (L <= l && r <= R)
      return mn[x];
    if (L <= mid)
      if (mid < R)
        return std::min(query(x << 1, l, mid, L, R),
                        query(x << 1 | 1, mid + 1, r, L, R));
      else
        return query(x << 1, l, mid, L, R);
    else
      return query(x << 1 | 1, mid + 1, r, L, R);
  }
  il ll Query(int l, int r) { return query(1, 1, n, l, r) + all_tag; }
  il vd Update_all(ll d) { all_tag += d; }
#undef mid
  il vd clear() {
    memset(mn, 20, sizeof mn);
    all_tag = 0;
  }
} A, B; // A f[i]+i B f[i]-i
int cx[200010];
il ll solve(int posA, int posB) {
  A.clear(), B.clear();
  A.Update(posA, posA);
  B.Update(posA, -posA);
  int lst = posB, nx;
  for (int i = 1; i <= q; ++i) {
    nx = cx[i];
    ll newf = B.Query(1, nx) + nx;
    if (nx != n)
      newf = std::min(newf, A.Query(nx + 1, n) - nx);
    A.Update_all(abs(nx - lst));
    B.Update_all(abs(nx - lst));
    A.Update(lst, newf + lst);
    B.Update(lst, newf - lst);
    lst = nx;
  }
  ll ans = 9e18;
  for (int i = 1; i <= n; ++i)
    ans = std::min(ans, A.Query(i, i) - i);
  return ans;
}
int main() {
  n = gi(), q = gi();
  int posA = gi(), posB = gi();
  for (int i = 1; i <= q; ++i)
    cx[i] = gi();
  printf("%lld\n", std::min(solve(posA, posB), solve(posB, posA)));
  return 0;
}