Problem p03950 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int w, h, n;
const int N = 400000;
int xi[N], yi[N], ord[N];
int ans;
int comp(int a, int b) { return xi[a] < xi[b]; }
int tagL[N], tagR[N], vaL[N], vaR[N], val[N], mxL[N], mxR[N], mnL[N], mnR[N];
int lc[N], rc[N], lb[N], rb[N], tot;
void upd(int t) {
  vaL[t] = max(vaL[lc[t]], vaL[rc[t]]);
  vaR[t] = max(vaR[lc[t]], vaR[rc[t]]);
  val[t] = max(val[lc[t]], val[rc[t]]);
  mnL[t] = min(mnL[lc[t]], mnL[rc[t]]);
  mnR[t] = min(mnR[lc[t]], mnR[rc[t]]);
  mxL[t] = max(mxL[lc[t]], mxL[rc[t]]);
  mxR[t] = max(mxR[lc[t]], mxR[rc[t]]);
}
void psdw(int t) {
  if (~tagL[t]) {
    vaL[lc[t]] = h - xi[ord[lb[lc[t]]]] - tagL[t];
    val[lc[t]] = vaR[lc[t]] - tagL[t];
    tagL[lc[t]] = mnL[lc[t]] = mxL[lc[t]] = tagL[t];
    vaL[rc[t]] = h - xi[ord[lb[rc[t]]]] - tagL[t];
    val[rc[t]] = vaR[rc[t]] - tagL[t];
    tagL[rc[t]] = mnL[rc[t]] = mxL[rc[t]] = tagL[t];
    tagL[t] = -1;
  }
  if (~tagR[t]) {
    vaR[lc[t]] = h - xi[ord[lb[lc[t]]]] - tagR[t];
    val[lc[t]] = vaL[lc[t]] - tagR[t];
    tagR[lc[t]] = mnR[lc[t]] = mxR[lc[t]] = tagR[t];
    vaR[rc[t]] = h - xi[ord[lb[rc[t]]]] - tagR[t];
    val[rc[t]] = vaL[rc[t]] - tagR[t];
    tagR[rc[t]] = mnR[rc[t]] = mxR[rc[t]] = tagR[t];
    tagR[t] = -1;
  }
}
void modify_L(int t, int l, int r, int v) {
  if (mnL[t] >= v)
    return;
  if (l <= lb[t] && rb[t] <= r && mxL[t] <= v) {
    vaL[t] = h - xi[ord[lb[t]]] - v;
    val[t] = vaR[t] - v;
    tagL[t] = mnL[t] = mxL[t] = v;
    return;
  }
  psdw(t);
  if (l <= rb[lc[t]])
    modify_L(lc[t], l, r, v);
  if (r >= lb[rc[t]])
    modify_L(rc[t], l, r, v);
  upd(t);
}
void modify_R(int t, int l, int r, int v) {
  if (mnR[t] >= v)
    return;
  if (l <= lb[t] && rb[t] <= r && mxR[t] <= v) {
    vaR[t] = h - xi[ord[lb[t]]] - v;
    val[t] = vaL[t] - v;
    tagR[t] = mnR[t] = mxR[t] = v;
    return;
  }
  psdw(t);
  if (l <= rb[lc[t]])
    modify_R(lc[t], l, r, v);
  if (r >= lb[rc[t]])
    modify_R(rc[t], l, r, v);
  upd(t);
}
int build(int l, int r) {
  int t = ++tot;
  lb[t] = l;
  rb[t] = r;
  tagL[t] = tagR[t] = -1;
  mnL[t] = mxL[t] = mnR[t] = mxR[t] = 0;
  if (l != r) {
    lc[t] = build(l, (l + r) / 2);
    rc[t] = build((l + r) / 2 + 1, r);
    upd(t);
  } else {
    val[t] = vaL[t] = vaR[t] = h - xi[ord[l]];
  }
  return t;
}

void solve() {
  for (int i = 1; i <= n; ++i)
    ord[i] = i;
  sort(ord + 1, ord + n + 1, comp);
  tot = 0;
  build(0, n);
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, xi[ord[i]] + val[1]);
    if (yi[ord[i]] <= h / 2)
      modify_L(1, 0, i - 1, yi[ord[i]]);
    else
      modify_R(1, 0, i - 1, h - yi[ord[i]]);
  }
  ans = max(ans, w + val[1]);
}
int main() {
  scanf("%d%d%d", &w, &h, &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", &xi[i], &yi[i]);
  solve();
  swap(w, h);
  for (int i = 1; i <= n; ++i)
    swap(xi[i], yi[i]);
  solve();
  printf("%d\n", ans * 2);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int w, h, n;
const int N = 800000;
int xi[N], yi[N], ord[N];
int ans;
int comp(int a, int b) { return xi[a] < xi[b]; }
int tagL[N], tagR[N], vaL[N], vaR[N], val[N], mxL[N], mxR[N], mnL[N], mnR[N];
int lc[N], rc[N], lb[N], rb[N], tot;
void upd(int t) {
  vaL[t] = max(vaL[lc[t]], vaL[rc[t]]);
  vaR[t] = max(vaR[lc[t]], vaR[rc[t]]);
  val[t] = max(val[lc[t]], val[rc[t]]);
  mnL[t] = min(mnL[lc[t]], mnL[rc[t]]);
  mnR[t] = min(mnR[lc[t]], mnR[rc[t]]);
  mxL[t] = max(mxL[lc[t]], mxL[rc[t]]);
  mxR[t] = max(mxR[lc[t]], mxR[rc[t]]);
}
void psdw(int t) {
  if (~tagL[t]) {
    vaL[lc[t]] = h - xi[ord[lb[lc[t]]]] - tagL[t];
    val[lc[t]] = vaR[lc[t]] - tagL[t];
    tagL[lc[t]] = mnL[lc[t]] = mxL[lc[t]] = tagL[t];
    vaL[rc[t]] = h - xi[ord[lb[rc[t]]]] - tagL[t];
    val[rc[t]] = vaR[rc[t]] - tagL[t];
    tagL[rc[t]] = mnL[rc[t]] = mxL[rc[t]] = tagL[t];
    tagL[t] = -1;
  }
  if (~tagR[t]) {
    vaR[lc[t]] = h - xi[ord[lb[lc[t]]]] - tagR[t];
    val[lc[t]] = vaL[lc[t]] - tagR[t];
    tagR[lc[t]] = mnR[lc[t]] = mxR[lc[t]] = tagR[t];
    vaR[rc[t]] = h - xi[ord[lb[rc[t]]]] - tagR[t];
    val[rc[t]] = vaL[rc[t]] - tagR[t];
    tagR[rc[t]] = mnR[rc[t]] = mxR[rc[t]] = tagR[t];
    tagR[t] = -1;
  }
}
void modify_L(int t, int l, int r, int v) {
  if (mnL[t] >= v)
    return;
  if (l <= lb[t] && rb[t] <= r && mxL[t] <= v) {
    vaL[t] = h - xi[ord[lb[t]]] - v;
    val[t] = vaR[t] - v;
    tagL[t] = mnL[t] = mxL[t] = v;
    return;
  }
  psdw(t);
  if (l <= rb[lc[t]])
    modify_L(lc[t], l, r, v);
  if (r >= lb[rc[t]])
    modify_L(rc[t], l, r, v);
  upd(t);
}
void modify_R(int t, int l, int r, int v) {
  if (mnR[t] >= v)
    return;
  if (l <= lb[t] && rb[t] <= r && mxR[t] <= v) {
    vaR[t] = h - xi[ord[lb[t]]] - v;
    val[t] = vaL[t] - v;
    tagR[t] = mnR[t] = mxR[t] = v;
    return;
  }
  psdw(t);
  if (l <= rb[lc[t]])
    modify_R(lc[t], l, r, v);
  if (r >= lb[rc[t]])
    modify_R(rc[t], l, r, v);
  upd(t);
}
int build(int l, int r) {
  int t = ++tot;
  lb[t] = l;
  rb[t] = r;
  tagL[t] = tagR[t] = -1;
  mnL[t] = mxL[t] = mnR[t] = mxR[t] = 0;
  if (l != r) {
    lc[t] = build(l, (l + r) / 2);
    rc[t] = build((l + r) / 2 + 1, r);
    upd(t);
  } else {
    val[t] = vaL[t] = vaR[t] = h - xi[ord[l]];
  }
  return t;
}

void solve() {
  for (int i = 1; i <= n; ++i)
    ord[i] = i;
  sort(ord + 1, ord + n + 1, comp);
  tot = 0;
  build(0, n);
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, xi[ord[i]] + val[1]);
    if (yi[ord[i]] <= h / 2)
      modify_L(1, 0, i - 1, yi[ord[i]]);
    else
      modify_R(1, 0, i - 1, h - yi[ord[i]]);
  }
  ans = max(ans, w + val[1]);
}
int main() {
  scanf("%d%d%d", &w, &h, &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", &xi[i], &yi[i]);
  solve();
  swap(w, h);
  for (int i = 1; i <= n; ++i)
    swap(xi[i], yi[i]);
  solve();
  printf("%d\n", ans * 2);
}