Problem p03736 (C++)

Buggy submission (status: Runtime Error):
// by xiqiao
#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
typedef long long ll;
int n, Q;
const ll inf = 1ll << 60;
int abss(int x) {
  if (x > 0)
    return x;
  return -x;
}
struct node {
  ll mx, mn1, mn2;
  ll flag;
} a[maxn * 4];
void update(int x) {
  a[x].mx = min(a[x * 2].mx, a[x * 2 + 1].mx);
  a[x].mn1 = min(a[x * 2].mn1, a[x * 2 + 1].mn1);
  a[x].mn2 = min(a[x * 2].mn2, a[x * 2 + 1].mn2);
}
void build(int x, int l, int r) {
  if (l == r) {
    a[x].mx = a[x].mn1 = a[x].mn2 = inf;
    return;
  }
  int mid = (l + r) / 2;
  build(x * 2, l, mid);
  build(x * 2 + 1, mid + 1, r);
  update(x);
}
void down(int x) {
  if (a[x].flag) {
    ll t = a[x].flag;
    a[x].flag = 0;
    a[x * 2].flag += t;
    a[x * 2].mx += t;
    a[x * 2].mn1 += t;
    a[x * 2].mn2 += t;
    a[x * 2 + 1].flag += t;
    a[x * 2 + 1].mx += t;
    a[x * 2 + 1].mn1 += t;
    a[x * 2 + 1].mn2 += t;
  }
}

void modify(int x, int l, int r, int wh, ll val) {
  if (l == r) {
    if (a[x].mx <= val)
      return;
    a[x].mx = val;
    a[x].mn1 = val - wh;
    a[x].mn2 = val + wh;
    return;
  }
  down(x);
  int mid = (l + r) / 2;
  if (mid >= wh)
    modify(x * 2, l, mid, wh, val);
  else
    modify(x * 2 + 1, mid + 1, r, wh, val);
  update(x);
}
ll find1(int x, int l, int r, int tx, int ty) {
  if (l >= tx && r <= ty) {
    return a[x].mn1;
  }
  down(x);
  int mid = (l + r) / 2;
  ll ret = inf;
  if (mid >= tx)
    ret = min(ret, find1(x * 2, l, mid, tx, ty));
  if (mid < ty)
    ret = min(ret, find1(x * 2 + 1, mid + 1, r, tx, ty));
  return ret;
}
ll find2(int x, int l, int r, int tx, int ty) {
  if (l >= tx && r <= ty)
    return a[x].mn2;
  down(x);
  int mid = (l + r) / 2;
  ll ret = inf;
  if (mid >= tx)
    ret = min(ret, find2(x * 2, l, mid, tx, ty));
  if (mid < ty)
    ret = min(ret, find2(x * 2 + 1, mid + 1, r, tx, ty));
  return ret;
}
ll query(int x, int l, int r, int wh) {
  if (l == r)
    return a[x].mx;
  int mid = (l + r) / 2;
  down(x);
  if (mid >= wh)
    return query(x * 2, l, mid, wh);
  else
    return query(x * 2 + 1, mid + 1, r, wh);
}
void print() {
  puts("");
  for (int i = 1; i <= n; i++) {
    printf("%lld ", query(1, 1, n, i));
  }
  puts("");
}
int main() {
  scanf("%d%d", &n, &Q);
  int last, B;
  scanf("%d%d", &last, &B);
  build(1, 1, n);
  modify(1, 1, n, B, 0);
  while (Q--) {
    int x;
    scanf("%d", &x);
    ll u = find1(1, 1, n, 1, x) + x; // cout<<find1(1,1,n,1,x)<<endl;
    ll v = find2(1, 1, n, x + 1, n) -
           x; // cout<<find1(1,1,n,1,x)<<" "<<find1(1,1,n,1,x)+x<<" "<<u<<"
              // "<<v<<endl;
    a[1].flag += abss(x - last);
    a[1].mx += abss(x - last);
    a[1].mn1 += abss(x - last);
    a[1].mn2 += abss(x - last);
    modify(1, 1, n, last, min(u, v));
    last = x;
    //	print();
  }
  printf("%lld\n", a[1].mx);
  return 0;
}

Fixed submission:
// by xiqiao
#include <bits/stdc++.h>
#define maxn 200005
using namespace std;
typedef long long ll;
int n, Q;
const ll inf = 1ll << 60;
int abss(int x) {
  if (x > 0)
    return x;
  return -x;
}
struct node {
  ll mx, mn1, mn2;
  ll flag;
} a[maxn * 4];
void update(int x) {
  a[x].mx = min(a[x * 2].mx, a[x * 2 + 1].mx);
  a[x].mn1 = min(a[x * 2].mn1, a[x * 2 + 1].mn1);
  a[x].mn2 = min(a[x * 2].mn2, a[x * 2 + 1].mn2);
}
void build(int x, int l, int r) {
  if (l == r) {
    a[x].mx = a[x].mn1 = a[x].mn2 = inf;
    return;
  }
  int mid = (l + r) / 2;
  build(x * 2, l, mid);
  build(x * 2 + 1, mid + 1, r);
  update(x);
}
void down(int x) {
  if (a[x].flag) {
    ll t = a[x].flag;
    a[x].flag = 0;
    a[x * 2].flag += t;
    a[x * 2].mx += t;
    a[x * 2].mn1 += t;
    a[x * 2].mn2 += t;
    a[x * 2 + 1].flag += t;
    a[x * 2 + 1].mx += t;
    a[x * 2 + 1].mn1 += t;
    a[x * 2 + 1].mn2 += t;
  }
}

void modify(int x, int l, int r, int wh, ll val) {
  if (l == r) {
    if (a[x].mx <= val)
      return;
    a[x].mx = val;
    a[x].mn1 = val - wh;
    a[x].mn2 = val + wh;
    return;
  }
  down(x);
  int mid = (l + r) / 2;
  if (mid >= wh)
    modify(x * 2, l, mid, wh, val);
  else
    modify(x * 2 + 1, mid + 1, r, wh, val);
  update(x);
}
ll find1(int x, int l, int r, int tx, int ty) {
  if (l >= tx && r <= ty) {
    return a[x].mn1;
  }
  down(x);
  int mid = (l + r) / 2;
  ll ret = inf;
  if (mid >= tx)
    ret = min(ret, find1(x * 2, l, mid, tx, ty));
  if (mid < ty)
    ret = min(ret, find1(x * 2 + 1, mid + 1, r, tx, ty));
  return ret;
}
ll find2(int x, int l, int r, int tx, int ty) {
  if (l >= tx && r <= ty)
    return a[x].mn2;
  down(x);
  int mid = (l + r) / 2;
  ll ret = inf;
  if (mid >= tx)
    ret = min(ret, find2(x * 2, l, mid, tx, ty));
  if (mid < ty)
    ret = min(ret, find2(x * 2 + 1, mid + 1, r, tx, ty));
  return ret;
}
ll query(int x, int l, int r, int wh) {
  if (l == r)
    return a[x].mx;
  int mid = (l + r) / 2;
  down(x);
  if (mid >= wh)
    return query(x * 2, l, mid, wh);
  else
    return query(x * 2 + 1, mid + 1, r, wh);
}
void print() {
  puts("");
  for (int i = 1; i <= n; i++) {
    printf("%lld ", query(1, 1, n, i));
  }
  puts("");
}
int main() {
  scanf("%d%d", &n, &Q);
  int last, B;
  scanf("%d%d", &last, &B);
  build(1, 1, n);
  modify(1, 1, n, B, 0);
  while (Q--) {
    int x;
    scanf("%d", &x);
    ll u = find1(1, 1, n, 1, x) + x; // cout<<find1(1,1,n,1,x)<<endl;
    ll v = find2(1, 1, n, x + 1, n) -
           x; // cout<<find1(1,1,n,1,x)<<" "<<find1(1,1,n,1,x)+x<<" "<<u<<"
              // "<<v<<endl;
    a[1].flag += abss(x - last);
    a[1].mx += abss(x - last);
    a[1].mn1 += abss(x - last);
    a[1].mn2 += abss(x - last);
    modify(1, 1, n, last, min(u, v));
    last = x;
    //	print();
  }
  printf("%lld\n", a[1].mx);
  return 0;
}