Problem p03736 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
typedef long long LL;
int read() {
  char c = getchar();
  int f = 1, sum = 0;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum * f;
}
const LL inf = 1e18;
int n, q, x, y;
int pos[maxn];
LL f[maxn << 2], g[maxn << 2], h[maxn << 2], tag[maxn << 2];
void add(int x, LL v) {
  if (f[x] != inf)
    f[x] += v, g[x] += v, h[x] += v, tag[x] += v;
}
void pushup(int id) {
  f[id] = min(f[id << 1], f[id << 1 | 1]);
  g[id] = min(g[id << 1], g[id << 1 | 1]);
  h[id] = min(h[id << 1], h[id << 1 | 1]);
}
void build(int id, int l, int r) {
  f[id] = g[id] = h[id] = inf;
  if (l == r)
    return;
  int mid = l + r >> 1;
  build(id << 1, l, mid);
  build(id << 1 | 1, mid + 1, r);
}
void pushdown(int id) {
  if (tag[id])
    add(id << 1, tag[id]), add(id << 1 | 1, tag[id]);
  tag[id] = 0;
}
void modify(int id, int l, int r, int pos, LL val) {
  if (l == r) {
    if (val < f[id])
      f[id] = val, g[id] = val - l, h[id] = val + l;
    return;
  }
  pushdown(id);
  int mid = l + r >> 1;
  if (pos <= mid)
    modify(id << 1, l, mid, pos, val);
  else
    modify(id << 1 | 1, mid + 1, r, pos, val);
  pushup(id);
}
LL query(int id, int l, int r, int ql, int qr, int f) {
  if (ql <= l && r <= qr) {
    return f ? h[id] : g[id];
  }
  pushdown(id);
  int mid = l + r >> 1;
  LL tmp = inf;
  if (ql <= mid)
    tmp = min(tmp, query(id << 1, l, mid, ql, qr, f));
  if (qr > mid)
    tmp = min(tmp, query(id << 1 | 1, mid + 1, r, ql, qr, f));
  return tmp;
}
int main() {
  n = read();
  q = read();
  x = read();
  pos[0] = y = read();
  for (int i = 1; i <= q; i++)
    pos[i] = read();
  build(1, 1, n);
  modify(1, 1, n, x, 0);
  for (int i = 1; i <= q; i++) {
    LL tmp = min(query(1, 1, n, 1, pos[i], 0) + pos[i],
                 query(1, 1, n, pos[i] + 1, n, 1) - pos[i]);
    add(1, abs(pos[i] - pos[i - 1]));
    modify(1, 1, n, pos[i - 1], tmp);
  }
  printf("%lld\n", f[1]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define maxn 200005
using namespace std;
typedef long long LL;
int read() {
  char c = getchar();
  int f = 1, sum = 0;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum * f;
}
const LL inf = 1e18;
int n, q, x, y;
int pos[maxn];
LL f[maxn << 2], g[maxn << 2], h[maxn << 2], tag[maxn << 2];
void add(int x, LL v) {
  if (f[x] != inf)
    f[x] += v, g[x] += v, h[x] += v, tag[x] += v;
}
void pushup(int id) {
  f[id] = min(f[id << 1], f[id << 1 | 1]);
  g[id] = min(g[id << 1], g[id << 1 | 1]);
  h[id] = min(h[id << 1], h[id << 1 | 1]);
}
void build(int id, int l, int r) {
  f[id] = g[id] = h[id] = inf;
  if (l == r)
    return;
  int mid = l + r >> 1;
  build(id << 1, l, mid);
  build(id << 1 | 1, mid + 1, r);
}
void pushdown(int id) {
  if (tag[id])
    add(id << 1, tag[id]), add(id << 1 | 1, tag[id]);
  tag[id] = 0;
}
void modify(int id, int l, int r, int pos, LL val) {
  if (l == r) {
    if (val < f[id])
      f[id] = val, g[id] = val - l, h[id] = val + l;
    return;
  }
  pushdown(id);
  int mid = l + r >> 1;
  if (pos <= mid)
    modify(id << 1, l, mid, pos, val);
  else
    modify(id << 1 | 1, mid + 1, r, pos, val);
  pushup(id);
}
LL query(int id, int l, int r, int ql, int qr, int f) {
  if (ql <= l && r <= qr) {
    return f ? h[id] : g[id];
  }
  pushdown(id);
  int mid = l + r >> 1;
  LL tmp = inf;
  if (ql <= mid)
    tmp = min(tmp, query(id << 1, l, mid, ql, qr, f));
  if (qr > mid)
    tmp = min(tmp, query(id << 1 | 1, mid + 1, r, ql, qr, f));
  return tmp;
}
int main() {
  n = read();
  q = read();
  x = read();
  pos[0] = y = read();
  for (int i = 1; i <= q; i++)
    pos[i] = read();
  build(1, 1, n);
  modify(1, 1, n, x, 0);
  for (int i = 1; i <= q; i++) {
    LL tmp = min(query(1, 1, n, 1, pos[i], 0) + pos[i],
                 query(1, 1, n, pos[i] + 1, n, 1) - pos[i]);
    add(1, abs(pos[i] - pos[i - 1]));
    modify(1, 1, n, pos[i - 1], tmp);
  }
  printf("%lld\n", f[1]);
  return 0;
}