Problem p03736 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define inf 1000000000000000000ll
#define N 200009
#define M 300009
using namespace std;

int n, m, a[N], pos[N];
ll tg;
struct node {
  ll l, r;
} mn[M];
void build(int k, int l, int r) {
  mn[k].l = mn[k].r = inf;
  if (l == r) {
    pos[l] = k;
    return;
  }
  int mid = l + r >> 1;
  build(k << 1, l, mid);
  build(k << 1 | 1, mid + 1, r);
}
void ins(int x, ll l, ll r) {
  int i = pos[x];
  mn[i].l = min(mn[i].l, l);
  mn[i].r = min(mn[i].r, r);
  for (i >>= 1; i; i >>= 1)
    mn[i] = (node){min(mn[i << 1].l, mn[i << 1 | 1].l),
                   min(mn[i << 1].r, mn[i << 1 | 1].r)};
}
node qry(int k, int l, int r, int x, int y) {
  if (x <= l && r <= y)
    return mn[k];
  int mid = l + r >> 1;
  node ans = (node){inf, inf}, tmp;
  if (x <= mid) {
    tmp = qry(k << 1, l, mid, x, y);
    ans.l = min(ans.l, tmp.l);
    ans.r = min(ans.r, tmp.r);
  }
  if (y > mid) {
    tmp = qry(k << 1 | 1, mid + 1, r, x, y);
    ans.l = min(ans.l, tmp.l);
    ans.r = min(ans.r, tmp.r);
  }
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  int i, x, y;
  ll tmp;
  scanf("%d%d", &x, &y);
  a[0] = x;
  build(1, 1, n);
  ins(y, -y, y);
  for (i = 1; i <= m; i++) {
    scanf("%d", &a[i]);
    tmp = min(qry(1, 1, n, 1, a[i]).l + a[i], qry(1, 1, n, a[i], n).r - a[i]) +
          tg;
    tg += abs(a[i] - a[i - 1]);
    ins(a[i - 1], tmp - a[i - 1] - tg, tmp + a[i - 1] - tg);
  }
  ll ans = inf;
  for (i = 1; i <= n; i++)
    ans = min(ans, mn[pos[i]].l + i);
  printf("%lld\n", ans + tg);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define inf 1000000000000000000ll
#define N 200009
#define M 600009
using namespace std;

int n, m, a[N], pos[N];
ll tg;
struct node {
  ll l, r;
} mn[M];
void build(int k, int l, int r) {
  mn[k].l = mn[k].r = inf;
  if (l == r) {
    pos[l] = k;
    return;
  }
  int mid = l + r >> 1;
  build(k << 1, l, mid);
  build(k << 1 | 1, mid + 1, r);
}
void ins(int x, ll l, ll r) {
  int i = pos[x];
  mn[i].l = min(mn[i].l, l);
  mn[i].r = min(mn[i].r, r);
  for (i >>= 1; i; i >>= 1)
    mn[i] = (node){min(mn[i << 1].l, mn[i << 1 | 1].l),
                   min(mn[i << 1].r, mn[i << 1 | 1].r)};
}
node qry(int k, int l, int r, int x, int y) {
  if (x <= l && r <= y)
    return mn[k];
  int mid = l + r >> 1;
  node ans = (node){inf, inf}, tmp;
  if (x <= mid) {
    tmp = qry(k << 1, l, mid, x, y);
    ans.l = min(ans.l, tmp.l);
    ans.r = min(ans.r, tmp.r);
  }
  if (y > mid) {
    tmp = qry(k << 1 | 1, mid + 1, r, x, y);
    ans.l = min(ans.l, tmp.l);
    ans.r = min(ans.r, tmp.r);
  }
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  int i, x, y;
  ll tmp;
  scanf("%d%d", &x, &y);
  a[0] = x;
  build(1, 1, n);
  ins(y, -y, y);
  for (i = 1; i <= m; i++) {
    scanf("%d", &a[i]);
    tmp = min(qry(1, 1, n, 1, a[i]).l + a[i], qry(1, 1, n, a[i], n).r - a[i]) +
          tg;
    tg += abs(a[i] - a[i - 1]);
    ins(a[i - 1], tmp - a[i - 1] - tg, tmp + a[i - 1] - tg);
  }
  ll ans = inf;
  for (i = 1; i <= n; i++)
    ans = min(ans, mn[pos[i]].l + i);
  printf("%lld\n", ans + tg);
  return 0;
}