Problem p03950 (C++)

Buggy submission (status: Runtime Error):
/*
  mail: mleautomaton@foxmail.com
  author: MLEAutoMaton
  This Code is made by MLEAutoMaton
*/
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define ll long long
#define REP(a, b, c) for (int a = b; a <= c; a++)
#define re register
#define file(a)                                                                \
  freopen(a ".in", "r", stdin);                                                \
  freopen(a ".out", "w", stdout)
#define int ll
inline int gi() {
  int f = 1, sum = 0;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    sum = (sum << 3) + (sum << 1) + ch - '0';
    ch = getchar();
  }
  return f * sum;
}
const int N = 500010;
int w, h, n;
struct node {
  int x, y;
  bool operator<(const node &b) const {
    return x < b.x || (x == b.x && y < b.y);
  }
} p[N];
typedef pair<int, int> pii;
#define mp make_pair
pii a[N], b[N]; // stack
int mx[N], tag[N], ans;
void pushup(int o) { mx[o] = max(mx[o << 1], mx[o << 1 | 1]); }
void pushdown(int o) {
  if (!tag[o])
    return;
  tag[o << 1] += tag[o];
  tag[o << 1 | 1] += tag[o];
  mx[o << 1] += tag[o];
  mx[o << 1 | 1] += tag[o];
  tag[o] = 0;
}
void modify(int o, int l, int r, int posl, int posr, int v) {
  if (posl <= l && r <= posr) {
    tag[o] += v;
    mx[o] += v;
    return;
  }
  pushdown(o);
  int mid = (l + r) >> 1;
  if (posl <= mid)
    modify(o << 1, l, mid, posl, posr, v);
  if (mid < posr)
    modify(o << 1 | 1, mid + 1, r, posl, posr, v);
  pushup(o);
}
void work() {
  memset(mx, 0, sizeof(mx));
  memset(tag, 0, sizeof(tag));
  sort(p + 1, p + n + 1);
  int l = 0, r = 0;
  for (int i = 1; i < n; i++) {
    if (p[i].y <= h / 2) {
      int lst = i - 1;
      while (l && p[i].y > a[l].second) {
        modify(1, 1, n, a[l].first, lst, a[l].second - p[i].y);
        lst = a[l--].first - 1;
      }
      if (lst != i - 1)
        a[++l] = mp(lst + 1, p[i].y);
    } else {
      int lst = i - 1;
      while (l && p[i].y < b[r].second) {
        modify(1, 1, n, b[r].first, lst, p[i].y - b[r].second);
        lst = b[r--].first - 1;
      }
      if (lst != i - 1)
        b[++r] = mp(lst + 1, p[i].y);
    }
    a[++l] = mp(i, 0);
    b[++r] = mp(i, h);
    modify(1, 1, n, i, i, h - p[i].x);
    ans = max(ans, mx[1] + p[i + 1].x);
  }
}
signed main() {
  w = gi();
  h = gi();
  n = gi();
  for (int i = 1; i <= n; i++)
    p[i].x = gi(), p[i].y = gi();
  p[++n] = (node){0, 0};
  p[++n] = (node){w, h};
  work();
  for (int i = 1; i <= n; i++)
    swap(p[i].x, p[i].y);
  swap(h, w);
  work();
  printf("%lld\n", ans * 2);
  return 0;
}


Fixed submission:
/*
  mail: mleautomaton@foxmail.com
  author: MLEAutoMaton
  This Code is made by MLEAutoMaton
*/
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define ll long long
#define REP(a, b, c) for (int a = b; a <= c; a++)
#define re register
#define file(a)                                                                \
  freopen(a ".in", "r", stdin);                                                \
  freopen(a ".out", "w", stdout)
#define int ll
inline int gi() {
  int f = 1, sum = 0;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    sum = (sum << 3) + (sum << 1) + ch - '0';
    ch = getchar();
  }
  return f * sum;
}
const int N = 2000010;
int w, h, n;
struct node {
  int x, y;
  bool operator<(const node &b) const {
    return x < b.x || (x == b.x && y < b.y);
  }
} p[N];
typedef pair<int, int> pii;
#define mp make_pair
pii a[N], b[N]; // stack
int mx[N], tag[N], ans;
void pushup(int o) { mx[o] = max(mx[o << 1], mx[o << 1 | 1]); }
void pushdown(int o) {
  if (!tag[o])
    return;
  tag[o << 1] += tag[o];
  tag[o << 1 | 1] += tag[o];
  mx[o << 1] += tag[o];
  mx[o << 1 | 1] += tag[o];
  tag[o] = 0;
}
void modify(int o, int l, int r, int posl, int posr, int v) {
  if (posl <= l && r <= posr) {
    tag[o] += v;
    mx[o] += v;
    return;
  }
  pushdown(o);
  int mid = (l + r) >> 1;
  if (posl <= mid)
    modify(o << 1, l, mid, posl, posr, v);
  if (mid < posr)
    modify(o << 1 | 1, mid + 1, r, posl, posr, v);
  pushup(o);
}
void work() {
  memset(mx, 0, sizeof(mx));
  memset(tag, 0, sizeof(tag));
  sort(p + 1, p + n + 1);
  int l = 0, r = 0;
  for (int i = 1; i < n; i++) {
    if (p[i].y <= h / 2) {
      int lst = i - 1;
      while (l && p[i].y > a[l].second) {
        modify(1, 1, n, a[l].first, lst, a[l].second - p[i].y);
        lst = a[l--].first - 1;
      }
      if (lst != i - 1)
        a[++l] = mp(lst + 1, p[i].y);
    } else {
      int lst = i - 1;
      while (l && p[i].y < b[r].second) {
        modify(1, 1, n, b[r].first, lst, p[i].y - b[r].second);
        lst = b[r--].first - 1;
      }
      if (lst != i - 1)
        b[++r] = mp(lst + 1, p[i].y);
    }
    a[++l] = mp(i, 0);
    b[++r] = mp(i, h);
    modify(1, 1, n, i, i, h - p[i].x);
    ans = max(ans, mx[1] + p[i + 1].x);
  }
}
signed main() {
  w = gi();
  h = gi();
  n = gi();
  for (int i = 1; i <= n; i++)
    p[i].x = gi(), p[i].y = gi();
  p[++n] = (node){0, 0};
  p[++n] = (node){w, h};
  work();
  for (int i = 1; i <= n; i++)
    swap(p[i].x, p[i].y);
  swap(h, w);
  work();
  printf("%lld\n", ans * 2);
  return 0;
}