Problem p03950 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define x first
#define y second

typedef std::pair<int, int> point;
const int N = 600005;

int totx, toty, vx[N], vy[N], ans;
int w, h, n;

point p[N];

int tag[N], max[N];

void pushup(int o) { max[o] = std::max(max[o << 1], max[o << 1 | 1]) + tag[o]; }
void build(int o = 1, int l = 0, int r = toty - 1) {
  tag[o] = 0;
  if (l == r) {
    max[o] = -vy[l] + w;
    return;
  }
  int mid = l + r >> 1;
  build(o << 1, l, mid), build(o << 1 | 1, mid + 1, r), pushup(o);
}
void modify(int L, int R, int v, int o = 1, int l = 0, int r = toty - 1) {
  if (L > R)
    return;
  if (L <= l && R >= r) {
    tag[o] += v, max[o] += v;
    return;
  }
  int mid = l + r >> 1;
  if (L <= mid)
    modify(L, R, v, o << 1, l, mid);
  if (R > mid)
    modify(L, R, v, o << 1 | 1, mid + 1, r);
  pushup(o);
}

void solve() {
  int mid = std::upper_bound(vx, vx + totx, w >> 1) - vx - 1;
  build();
  std::sort(p + 1, p + n + 1, [&](point a, point b) {
    return a.y != b.y ? a.y < b.y : std::abs(a.x - mid) > std::abs(b.x - mid);
  });
  std::stack<int> L, R;
  p[0] = point(mid, 0), L.emplace(0), R.emplace(0);
  for (int y = 0, i = 0; y < toty; ++y) {
    ans = std::max(ans, vy[y] + max[1]);
    for (; i <= n && p[i].y == y; ++i)
      if (p[i].x <= mid) {
        modify(p[L.top()].y, y - 1, -vx[p[i].x]);
        while (p[L.top()].x < p[i].x) {
          point lst = p[L.top()];
          L.pop();
          modify(p[L.top()].y, lst.y - 1, vx[lst.x] - vx[p[i].x]);
        }
        L.emplace(i);
      } else {
        modify(p[R.top()].y, y - 1, vx[p[i].x] - w);
        while (p[R.top()].x > p[i].x) {
          point lst = p[R.top()];
          R.pop();
          modify(p[R.top()].y, lst.y - 1, vx[p[i].x] - vx[lst.x]);
        }
        R.emplace(i);
      }
  }
}

int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> w >> h >> n, ans = std::max(w, h) + 1;
  vx[0] = vy[0] = 0;
  for (int i = 1; i <= n; ++i) {
    std::cin >> p[i].x >> p[i].y;
    if (p[i].x == 0 || p[i].x == w || p[i].y == 0 || p[i].y == h) {
      --i, --n;
      continue;
    }
    vx[i] = p[i].x, vy[i] = p[i].y;
  }
  vx[n + 1] = w, vy[n + 1] = h;
  totx = toty = n + 2;
  totx = (std::sort(vx, vx + totx), std::unique(vx, vx + totx) - vx);
  toty = (std::sort(vy, vy + toty), std::unique(vy, vy + toty) - vy);
  for (int i = 1; i <= n; ++i)
    p[i].x = std::lower_bound(vx, vx + totx, p[i].x) - vx,
    p[i].y = std::lower_bound(vy, vy + toty, p[i].y) - vy;
  solve();
  std::swap(w, h), std::swap(totx, toty), std::swap(vx, vy);
  for (int i = 1; i <= n; ++i)
    std::swap(p[i].x, p[i].y);
  solve();
  std::cout << ans * 2 << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define x first
#define y second

typedef std::pair<int, int> point;
const int N = 600005;

int totx, toty, vx[N], vy[N], ans;
int w, h, n;

point p[N];

int tag[N << 2], max[N << 2];

void pushup(int o) { max[o] = std::max(max[o << 1], max[o << 1 | 1]) + tag[o]; }
void build(int o = 1, int l = 0, int r = toty - 1) {
  tag[o] = 0;
  if (l == r) {
    max[o] = -vy[l] + w;
    return;
  }
  int mid = l + r >> 1;
  build(o << 1, l, mid), build(o << 1 | 1, mid + 1, r), pushup(o);
}
void modify(int L, int R, int v, int o = 1, int l = 0, int r = toty - 1) {
  if (L > R)
    return;
  if (L <= l && R >= r) {
    tag[o] += v, max[o] += v;
    return;
  }
  int mid = l + r >> 1;
  if (L <= mid)
    modify(L, R, v, o << 1, l, mid);
  if (R > mid)
    modify(L, R, v, o << 1 | 1, mid + 1, r);
  pushup(o);
}

void solve() {
  int mid = std::upper_bound(vx, vx + totx, w >> 1) - vx - 1;
  build();
  std::sort(p + 1, p + n + 1, [&](point a, point b) {
    return a.y != b.y ? a.y < b.y : std::abs(a.x - mid) > std::abs(b.x - mid);
  });
  std::stack<int> L, R;
  p[0] = point(mid, 0), L.emplace(0), R.emplace(0);
  for (int y = 0, i = 0; y < toty; ++y) {
    ans = std::max(ans, vy[y] + max[1]);
    for (; i <= n && p[i].y == y; ++i)
      if (p[i].x <= mid) {
        modify(p[L.top()].y, y - 1, -vx[p[i].x]);
        while (p[L.top()].x < p[i].x) {
          point lst = p[L.top()];
          L.pop();
          modify(p[L.top()].y, lst.y - 1, vx[lst.x] - vx[p[i].x]);
        }
        L.emplace(i);
      } else {
        modify(p[R.top()].y, y - 1, vx[p[i].x] - w);
        while (p[R.top()].x > p[i].x) {
          point lst = p[R.top()];
          R.pop();
          modify(p[R.top()].y, lst.y - 1, vx[p[i].x] - vx[lst.x]);
        }
        R.emplace(i);
      }
  }
}

int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> w >> h >> n, ans = std::max(w, h) + 1;
  vx[0] = vy[0] = 0;
  for (int i = 1; i <= n; ++i) {
    std::cin >> p[i].x >> p[i].y;
    if (p[i].x == 0 || p[i].x == w || p[i].y == 0 || p[i].y == h) {
      --i, --n;
      continue;
    }
    vx[i] = p[i].x, vy[i] = p[i].y;
  }
  vx[n + 1] = w, vy[n + 1] = h;
  totx = toty = n + 2;
  totx = (std::sort(vx, vx + totx), std::unique(vx, vx + totx) - vx);
  toty = (std::sort(vy, vy + toty), std::unique(vy, vy + toty) - vy);
  for (int i = 1; i <= n; ++i)
    p[i].x = std::lower_bound(vx, vx + totx, p[i].x) - vx,
    p[i].y = std::lower_bound(vy, vy + toty, p[i].y) - vy;
  solve();
  std::swap(w, h), std::swap(totx, toty), std::swap(vx, vy);
  for (int i = 1; i <= n; ++i)
    std::swap(p[i].x, p[i].y);
  solve();
  std::cout << ans * 2 << '\n';
  return 0;
}