Problem p03950 (C++)

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

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define Debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef long double LD;
typedef unsigned int uint;
typedef pair<int, int> pii;
typedef unsigned long long uLL;

template <typename T> inline void Read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}

template <typename T> inline bool CheckMax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <typename T> inline bool CheckMin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

const int N = 300005;

int n, w, h, ans, d[N], u[N], x[N], y[N], tmp[N], tag[N << 2], val[N << 2];
pii a[N];

inline void Build(int x, int l, int r) {
  val[x] = -tmp[l], tag[x] = 0;
  if (l == r) {
    return;
  }
  int mid = l + r >> 1;
  Build(x << 1, l, mid), Build(x << 1 | 1, mid + 1, r);
}

inline void Modify(int x, int l, int r, int ql, int qr, int v) {
  if (l == ql && r == qr) {
    val[x] += v, tag[x] += v;
    return;
  }
  int mid = l + r >> 1;
  if (qr <= mid) {
    Modify(x << 1, l, mid, ql, qr, v);
  } else if (ql > mid) {
    Modify(x << 1 | 1, mid + 1, r, ql, qr, v);
  } else {
    Modify(x << 1, l, mid, ql, mid, v),
        Modify(x << 1 | 1, mid + 1, r, mid + 1, qr, v);
  }
  val[x] = max(val[x << 1], val[x << 1 | 1]) + tag[x];
}

inline void Solve(int n) {
  int m = 0, mid = h >> 1, tpd = 0, tpu = 0;
  for (int i = 1; i <= n; ++i) {
    a[i] = mp(x[i], y[i]);
  }
  sort(a + 1, a + n + 1);
  if (a[n].X < w) {
    a[++n] = mp(w, 0);
  }
  for (int i = 1; i <= n; ++i) {
    tmp[++m] = a[i].X;
  }
  tmp[++m] = 0, sort(tmp + 1, tmp + m + 1),
  m = unique(tmp + 1, tmp + m + 1) - tmp - 1;
  for (int i = 1; i <= n; ++i) {
    a[i].X = lower_bound(tmp + 1, tmp + m + 1, a[i].X) - tmp;
  }
  a[0].X = 1, Build(1, 1, m);
  if (a[1].X > 1) {
    Modify(1, 1, m, 1, 1, h);
  }
  for (int i = 1; i <= n; ++i) {
    if (a[i].Y <= mid) {
      Modify(1, 1, m, a[i].X, a[i].X, h);
      CheckMax(ans, tmp[a[i].X] + val[1]);
      Modify(1, 1, m, a[d[tpd]].X, a[i].X - 1, -a[i].Y);
      for (; tpd && a[d[tpd]].Y <= a[i].Y; --tpd) {
        Modify(1, 1, m, a[d[tpd - 1]].X, a[d[tpd]].X - 1, a[d[tpd]].Y - a[i].Y);
      }
      d[++tpd] = i;
    } else {
      Modify(1, 1, m, a[i].X, a[i].X, h);
      CheckMax(ans, tmp[a[i].X] + val[1]);
      Modify(1, 1, m, a[u[tpu]].X, a[i].X - 1, a[i].Y - h);
      for (; tpu && a[u[tpu]].Y >= a[i].Y; --tpu) {
        Modify(1, 1, m, a[u[tpu - 1]].X, a[u[tpu]].X - 1, a[i].Y - a[u[tpu]].Y);
      }
      u[++tpu] = i;
    }
  }
}

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  Read(w), Read(h), Read(n);
  for (int i = 1; i <= n; ++i) {
    Read(x[i]), Read(y[i]);
  }
  Solve(n);
  for (int i = 1; i <= n; ++i) {
    swap(x[i], y[i]);
  }
  swap(w, h);
  Solve(n);
  printf("%d\n", ans << 1);
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}


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

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define Debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef long double LD;
typedef unsigned int uint;
typedef pair<int, int> pii;
typedef unsigned long long uLL;

template <typename T> inline void Read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}

template <typename T> inline bool CheckMax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <typename T> inline bool CheckMin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

const int N = 300005;

int n, w, h, ans, d[N], u[N], x[N], y[N], tmp[N], tag[N << 2], val[N << 2];
pii a[N];

inline void Build(int x, int l, int r) {
  val[x] = -tmp[l], tag[x] = 0;
  if (l == r) {
    return;
  }
  int mid = l + r >> 1;
  Build(x << 1, l, mid), Build(x << 1 | 1, mid + 1, r);
}

inline void Modify(int x, int l, int r, int ql, int qr, int v) {
  if (ql > qr) {
    return;
  }
  if (l == ql && r == qr) {
    val[x] += v, tag[x] += v;
    return;
  }
  int mid = l + r >> 1;
  if (qr <= mid) {
    Modify(x << 1, l, mid, ql, qr, v);
  } else if (ql > mid) {
    Modify(x << 1 | 1, mid + 1, r, ql, qr, v);
  } else {
    Modify(x << 1, l, mid, ql, mid, v),
        Modify(x << 1 | 1, mid + 1, r, mid + 1, qr, v);
  }
  val[x] = max(val[x << 1], val[x << 1 | 1]) + tag[x];
}

inline void Solve(int n) {
  int m = 0, mid = h >> 1, tpd = 0, tpu = 0;
  for (int i = 1; i <= n; ++i) {
    a[i] = mp(x[i], y[i]);
  }
  sort(a + 1, a + n + 1);
  if (a[n].X < w) {
    a[++n] = mp(w, 0);
  }
  for (int i = 1; i <= n; ++i) {
    tmp[++m] = a[i].X;
  }
  tmp[++m] = 0, sort(tmp + 1, tmp + m + 1),
  m = unique(tmp + 1, tmp + m + 1) - tmp - 1;
  for (int i = 1; i <= n; ++i) {
    a[i].X = lower_bound(tmp + 1, tmp + m + 1, a[i].X) - tmp;
  }
  a[0].X = 1, Build(1, 1, m);
  if (a[1].X > 1) {
    Modify(1, 1, m, 1, 1, h);
  }
  for (int i = 1; i <= n; ++i) {
    if (a[i].Y <= mid) {
      Modify(1, 1, m, a[i].X, a[i].X, h);
      CheckMax(ans, tmp[a[i].X] + val[1]);
      Modify(1, 1, m, a[d[tpd]].X, a[i].X - 1, -a[i].Y);
      for (; tpd && a[d[tpd]].Y <= a[i].Y; --tpd) {
        Modify(1, 1, m, a[d[tpd - 1]].X, a[d[tpd]].X - 1, a[d[tpd]].Y - a[i].Y);
      }
      d[++tpd] = i;
    } else {
      Modify(1, 1, m, a[i].X, a[i].X, h);
      CheckMax(ans, tmp[a[i].X] + val[1]);
      Modify(1, 1, m, a[u[tpu]].X, a[i].X - 1, a[i].Y - h);
      for (; tpu && a[u[tpu]].Y >= a[i].Y; --tpu) {
        Modify(1, 1, m, a[u[tpu - 1]].X, a[u[tpu]].X - 1, a[i].Y - a[u[tpu]].Y);
      }
      u[++tpu] = i;
    }
  }
}

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  Read(w), Read(h), Read(n);
  for (int i = 1; i <= n; ++i) {
    Read(x[i]), Read(y[i]);
  }
  Solve(n);
  for (int i = 1; i <= n; ++i) {
    swap(x[i], y[i]);
  }
  swap(w, h);
  Solve(n);
  printf("%d\n", ans << 1);
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}