Problem p03950 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
struct point {
  int x, y;
} arr[100005], st1[100005], st2[100005];
int mx[400005], lazy[400005], n, w, h, ans;
void modify(int u, int l, int r, int L, int R, int x) {
  if (L <= l && r <= R) {
    mx[u] += x;
    lazy[u] += x;
    return;
  }
  int m = l + r >> 1;
  if (L <= m)
    modify(u << 1, l, m, L, R, x);
  if (m < R)
    modify(u << 1 | 1, m + 1, r, L, R, x);
  mx[u] = std::max(mx[u << 1], mx[u << 1 | 1]) + lazy[u];
}
void work() {
  std::sort(arr, arr + n,
            [&](const point &a, const point &b) { return a.x < b.x; });
  memset(mx, -0x3f, sizeof(mx));
  memset(lazy, 0, sizeof(lazy));
  int tp1 = 0, tp2 = 0;
  for (int i = 0; i < n - 1; i++) {
    auto *st = arr[i].y <= h / 2 ? st1 : st2;
    int nxt = i - 1, &tp = arr[i].y <= h / 2 ? tp1 : tp2;
    while (tp && ((st[tp - 1].y < arr[i].y) ^ (arr[i].y > h / 2))) {
      modify(1, 0, n - 1, st[tp - 1].x, nxt,
             (st[tp - 1].y - arr[i].y) * (arr[i].y <= h / 2 ? 1 : -1));
      nxt = st[--tp].x - 1;
    }
    if (nxt != i - 1)
      st[tp++] = {nxt + 1, arr[i].y};
    st1[tp1++] = {i, 0};
    st2[tp2++] = {i, h};
    modify(1, 0, n - 1, i, i, -mx[0] + h - arr[i].x);
    ans = std::max(ans, mx[1] + arr[i + 1].x);
  }
}
int main() {
  // freopen("ARC063-F.in", "r", stdin);
  scanf("%d%d%d", &w, &h, &n);
  for (int i = 0; i < n; i++)
    scanf("%d%d", &arr[i].x, &arr[i].y);
  arr[n++] = {0, 0};
  arr[n++] = {w, h};
  work();
  std::swap(w, h);
  for (int i = 0; i < n; i++)
    std::swap(arr[i].x, arr[i].y);
  work();
  printf("%d\n", ans << 1);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
struct point {
  int x, y;
} arr[300005], st1[300005], st2[300005];
int mx[1200005], lazy[1200005], n, w, h, ans;
void modify(int u, int l, int r, int L, int R, int x) {
  if (L <= l && r <= R) {
    mx[u] += x;
    lazy[u] += x;
    return;
  }
  int m = l + r >> 1;
  if (L <= m)
    modify(u << 1, l, m, L, R, x);
  if (m < R)
    modify(u << 1 | 1, m + 1, r, L, R, x);
  mx[u] = std::max(mx[u << 1], mx[u << 1 | 1]) + lazy[u];
}
void work() {
  std::sort(arr, arr + n,
            [&](const point &a, const point &b) { return a.x < b.x; });
  memset(mx, -0x3f, sizeof(mx));
  memset(lazy, 0, sizeof(lazy));
  int tp1 = 0, tp2 = 0;
  for (int i = 0; i < n - 1; i++) {
    auto *st = arr[i].y <= h / 2 ? st1 : st2;
    int nxt = i - 1, &tp = arr[i].y <= h / 2 ? tp1 : tp2;
    while (tp && ((st[tp - 1].y < arr[i].y) ^ (arr[i].y > h / 2))) {
      modify(1, 0, n - 1, st[tp - 1].x, nxt,
             (st[tp - 1].y - arr[i].y) * (arr[i].y <= h / 2 ? 1 : -1));
      nxt = st[--tp].x - 1;
    }
    if (nxt != i - 1)
      st[tp++] = {nxt + 1, arr[i].y};
    st1[tp1++] = {i, 0};
    st2[tp2++] = {i, h};
    modify(1, 0, n - 1, i, i, -mx[0] + h - arr[i].x);
    ans = std::max(ans, mx[1] + arr[i + 1].x);
  }
}
int main() {
  // freopen("ARC063-F.in", "r", stdin);
  scanf("%d%d%d", &w, &h, &n);
  for (int i = 0; i < n; i++)
    scanf("%d%d", &arr[i].x, &arr[i].y);
  arr[n++] = {0, 0};
  arr[n++] = {w, h};
  work();
  std::swap(w, h);
  for (int i = 0; i < n; i++)
    std::swap(arr[i].x, arr[i].y);
  work();
  printf("%d\n", ans << 1);
  return 0;
}