Problem p03950 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN = 100005;
struct Node {
  int x, y;
  bool operator<(const Node &b) const { return x < b.x; }
} a[MAXN << 1], stk[2][MAXN << 1];
int mx[MAXN << 2], add[MAXN << 2], n, w, h, tot_1, tot_2, ql, qr, qval, ans;
inline void pushdown(int i) {
  int lc = i << 1, rc = lc | 1, tag = add[i];
  add[i] = 0;
  mx[lc] += tag, mx[rc] += tag;
  add[lc] += tag, add[rc] += tag;
}
inline void update(int i, int l, int r) {
  if (l >= ql && r <= qr) {
    mx[i] += qval;
    add[i] += qval;
    return;
  }
  if (add[i])
    pushdown(i);
  int lc = i << 1, rc = lc | 1, mid = (l + r) >> 1;
  if (qr <= mid)
    update(lc, l, mid);
  else if (ql > mid)
    update(rc, mid + 1, r);
  else
    update(lc, l, mid), update(rc, mid + 1, r);
  mx[i] = max(mx[lc], mx[rc]);
}
inline void solve() {
  sort(a + 1, a + n + 1);
  tot_1 = tot_2 = 0;
  memset(mx, 0, sizeof(mx));
  memset(add, 0, sizeof(add));
  for (int i = 1; i <= n; ++i) {
    if (a[i].y <= h / 2) {
      int last = i - 1;
      while (tot_1 && a[i].y > stk[0][tot_1].y) {
        ql = stk[0][tot_1].x, qr = last, qval = stk[0][tot_1].y - a[i].y;
        update(1, 1, n);
        last = stk[0][tot_1].x - 1;
        --tot_1;
      }
      if (last != i - 1)
        stk[0][++tot_1] = (Node){last + 1, a[i].y};
    } else {
      int last = i - 1;
      while (tot_2 && a[i].y < stk[1][tot_2].y) {
        ql = stk[1][tot_2].x, qr = last, qval = a[i].y - stk[1][tot_2].y;
        update(1, 1, n);
        last = stk[1][tot_2].x - 1;
        --tot_2;
      }
      if (last != i - 1)
        stk[1][++tot_2] = (Node){last + 1, a[i].y};
    }
    stk[0][++tot_1] = (Node){i, 0};
    stk[1][++tot_2] = (Node){i, h};
    ql = qr = i, qval = h - a[i].x;
    update(1, 1, n);
    ans = max(ans, mx[1] + a[i + 1].x);
  }
}
int main() {
  scanf("%d%d%d", &w, &h, &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", &a[i].x, &a[i].y);
  a[++n].x = 0, a[n].y = 0;
  a[++n].x = w, a[n].y = h;
  solve();
  swap(w, h);
  for (int i = 1; i <= n; ++i)
    swap(a[i].x, a[i].y);
  solve();
  cout << ans * 2 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN = 300005;
struct Node {
  int x, y;
  bool operator<(const Node &b) const { return x < b.x; }
} a[MAXN << 1], stk[2][MAXN << 1];
int mx[MAXN << 2], add[MAXN << 2], n, w, h, tot_1, tot_2, ql, qr, qval, ans;
inline void pushdown(int i) {
  int lc = i << 1, rc = lc | 1, tag = add[i];
  add[i] = 0;
  mx[lc] += tag, mx[rc] += tag;
  add[lc] += tag, add[rc] += tag;
}
inline void update(int i, int l, int r) {
  if (l >= ql && r <= qr) {
    mx[i] += qval;
    add[i] += qval;
    return;
  }
  if (add[i])
    pushdown(i);
  int lc = i << 1, rc = lc | 1, mid = (l + r) >> 1;
  if (qr <= mid)
    update(lc, l, mid);
  else if (ql > mid)
    update(rc, mid + 1, r);
  else
    update(lc, l, mid), update(rc, mid + 1, r);
  mx[i] = max(mx[lc], mx[rc]);
}
inline void solve() {
  sort(a + 1, a + n + 1);
  tot_1 = tot_2 = 0;
  memset(mx, 0, sizeof(mx));
  memset(add, 0, sizeof(add));
  for (int i = 1; i <= n; ++i) {
    if (a[i].y <= h / 2) {
      int last = i - 1;
      while (tot_1 && a[i].y > stk[0][tot_1].y) {
        ql = stk[0][tot_1].x, qr = last, qval = stk[0][tot_1].y - a[i].y;
        update(1, 1, n);
        last = stk[0][tot_1].x - 1;
        --tot_1;
      }
      if (last != i - 1)
        stk[0][++tot_1] = (Node){last + 1, a[i].y};
    } else {
      int last = i - 1;
      while (tot_2 && a[i].y < stk[1][tot_2].y) {
        ql = stk[1][tot_2].x, qr = last, qval = a[i].y - stk[1][tot_2].y;
        update(1, 1, n);
        last = stk[1][tot_2].x - 1;
        --tot_2;
      }
      if (last != i - 1)
        stk[1][++tot_2] = (Node){last + 1, a[i].y};
    }
    stk[0][++tot_1] = (Node){i, 0};
    stk[1][++tot_2] = (Node){i, h};
    ql = qr = i, qval = h - a[i].x;
    update(1, 1, n);
    ans = max(ans, mx[1] + a[i + 1].x);
  }
}
int main() {
  scanf("%d%d%d", &w, &h, &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", &a[i].x, &a[i].y);
  a[++n].x = 0, a[n].y = 0;
  a[++n].x = w, a[n].y = h;
  solve();
  swap(w, h);
  for (int i = 1; i <= n; ++i)
    swap(a[i].x, a[i].y);
  solve();
  cout << ans * 2 << endl;
  return 0;
}