Problem p03736 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define reset(a, b) memset(a, b, sizeof a);
#define ALL(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << (x) << endl
#define sz(x) (int)(x.size())
#define lt(x) (int)(x.length())
#define MOD 1000000007
#define mx 100010

long long mini[2][mx * 4], n, q, a[mx], lazy[2][mx * 4];

void build(int t, int idx, int l, int r) {
  mini[t][idx] = 1e18;
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  build(t, 2 * idx, l, mid);
  build(t, 2 * idx + 1, mid + 1, r);
  mini[t][idx] = min(mini[t][idx * 2], mini[t][idx * 2 + 1]);
}

long long cari(int t, int idx, int l, int r, int fl, int fr) {
  if (fl > r || fr < l)
    return 1e18;
  if (fl <= l && r <= fr)
    return mini[t][idx];
  int mid = (l + r) >> 1;
  if (lazy[t][idx] != 0) {
    mini[t][idx * 2] += lazy[t][idx];
    mini[t][idx * 2 + 1] += lazy[t][idx];
    lazy[t][idx * 2] += lazy[t][idx];
    lazy[t][idx * 2 + 1] += lazy[t][idx];
    lazy[t][idx] = 0;
  }
  return min(cari(t, idx * 2, l, mid, fl, fr),
             cari(t, idx * 2 + 1, mid + 1, r, fl, fr));
}

void upd(int t, int idx, int l, int r, int in, long long val) {
  if (in > r || in < l)
    return;
  if (l == r) {
    mini[t][idx] = val;
    return;
  }
  int mid = (l + r) >> 1;
  if (lazy[t][idx] != 0) {
    mini[t][idx * 2] += lazy[t][idx];
    mini[t][idx * 2 + 1] += lazy[t][idx];
    lazy[t][idx * 2] += lazy[t][idx];
    lazy[t][idx * 2 + 1] += lazy[t][idx];
    lazy[t][idx] = 0;
  }
  upd(t, 2 * idx, l, mid, in, val);
  upd(t, 2 * idx + 1, mid + 1, r, in, val);
  mini[t][idx] = min(mini[t][idx * 2], mini[t][idx * 2 + 1]);
}

vector<pair<int, int>> ve;

void update(int idx, long long val) {
  int in =
      lower_bound(ALL(ve), make_pair((int)a[idx], (int)idx)) - ve.begin() + 1;
  upd(0, 1, 1, n, in, val - a[idx]);
  upd(1, 1, 1, n, in, val + a[idx]);
}

int main() {
  cin >> n >> n >> a[1] >> a[2];
  build(0, 1, 1, n + 2);
  build(1, 1, 1, n + 2);
  for (int i = 1; i <= n; i++)
    cin >> a[i + 2];
  for (int i = 1; i <= n + 2; i++)
    ve.pb({a[i], i});
  n += 2;
  sort(ALL(ve));
  int satu = abs(a[2] - a[3]);
  int dua = abs(a[1] - a[3]);
  update(1, satu);
  update(2, dua);
  // debug(n);
  for (int i = 4; i <= n; i++) {
    int in =
        lower_bound(ALL(ve), make_pair((int)a[i], (int)i)) - ve.begin() + 1;
    long long sa = cari(0, 1, 1, n, 1, in) + a[i];
    long long du = cari(1, 1, 1, n, in, n) - a[i];
    long long tambah = abs(a[i] - a[i - 1]);
    lazy[0][1] += tambah;
    mini[0][1] += tambah;
    lazy[1][1] += tambah;
    mini[1][1] += tambah;
    update(i - 1, min(sa, du));
  }
  long long jaw = 1e18;
  for (int i = 1; i < n; i++) {
    int in =
        lower_bound(ALL(ve), make_pair((int)a[i], (int)i)) - ve.begin() + 1;
    jaw = min(jaw, cari(0, 1, 1, n, in, in) + a[i]);
    jaw = min(jaw, cari(1, 1, 1, n, in, in) - a[i]);
  }
  cout << jaw << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define reset(a, b) memset(a, b, sizeof a);
#define ALL(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << (x) << endl
#define sz(x) (int)(x.size())
#define lt(x) (int)(x.length())
#define MOD 1000000007
#define mx 300000

long long mini[2][mx * 4], n, q, a[mx], lazy[2][mx * 4];

void build(int t, int idx, int l, int r) {
  mini[t][idx] = 1e18;
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  build(t, 2 * idx, l, mid);
  build(t, 2 * idx + 1, mid + 1, r);
  mini[t][idx] = min(mini[t][idx * 2], mini[t][idx * 2 + 1]);
}

long long cari(int t, int idx, int l, int r, int fl, int fr) {
  if (fl > r || fr < l)
    return 1e18;
  if (fl <= l && r <= fr)
    return mini[t][idx];
  int mid = (l + r) >> 1;
  if (lazy[t][idx] != 0) {
    mini[t][idx * 2] += lazy[t][idx];
    mini[t][idx * 2 + 1] += lazy[t][idx];
    lazy[t][idx * 2] += lazy[t][idx];
    lazy[t][idx * 2 + 1] += lazy[t][idx];
    lazy[t][idx] = 0;
  }
  return min(cari(t, idx * 2, l, mid, fl, fr),
             cari(t, idx * 2 + 1, mid + 1, r, fl, fr));
}

void upd(int t, int idx, int l, int r, int in, long long val) {
  if (in > r || in < l)
    return;
  if (l == r) {
    mini[t][idx] = val;
    return;
  }
  int mid = (l + r) >> 1;
  if (lazy[t][idx] != 0) {
    mini[t][idx * 2] += lazy[t][idx];
    mini[t][idx * 2 + 1] += lazy[t][idx];
    lazy[t][idx * 2] += lazy[t][idx];
    lazy[t][idx * 2 + 1] += lazy[t][idx];
    lazy[t][idx] = 0;
  }
  upd(t, 2 * idx, l, mid, in, val);
  upd(t, 2 * idx + 1, mid + 1, r, in, val);
  mini[t][idx] = min(mini[t][idx * 2], mini[t][idx * 2 + 1]);
}

vector<pair<int, int>> ve;

void update(int idx, long long val) {
  int in =
      lower_bound(ALL(ve), make_pair((int)a[idx], (int)idx)) - ve.begin() + 1;
  upd(0, 1, 1, n, in, val - a[idx]);
  upd(1, 1, 1, n, in, val + a[idx]);
}

int main() {
  cin >> n >> n >> a[1] >> a[2];
  build(0, 1, 1, n + 2);
  build(1, 1, 1, n + 2);
  for (int i = 1; i <= n; i++)
    cin >> a[i + 2];
  for (int i = 1; i <= n + 2; i++)
    ve.pb({a[i], i});
  n += 2;
  sort(ALL(ve));
  int satu = abs(a[2] - a[3]);
  int dua = abs(a[1] - a[3]);
  update(1, satu);
  update(2, dua);
  // debug(n);
  for (int i = 4; i <= n; i++) {
    int in =
        lower_bound(ALL(ve), make_pair((int)a[i], (int)i)) - ve.begin() + 1;
    long long sa = cari(0, 1, 1, n, 1, in) + a[i];
    long long du = cari(1, 1, 1, n, in, n) - a[i];
    long long tambah = abs(a[i] - a[i - 1]);
    lazy[0][1] += tambah;
    mini[0][1] += tambah;
    lazy[1][1] += tambah;
    mini[1][1] += tambah;
    update(i - 1, min(sa, du));
  }
  long long jaw = 1e18;
  for (int i = 1; i < n; i++) {
    int in =
        lower_bound(ALL(ve), make_pair((int)a[i], (int)i)) - ve.begin() + 1;
    jaw = min(jaw, cari(0, 1, 1, n, in, in) + a[i]);
    jaw = min(jaw, cari(1, 1, 1, n, in, in) - a[i]);
  }
  cout << jaw << endl;
}