Problem p03950 (C++)

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

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template <typename A, typename B> inline char smax(A &a, const B &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename A, typename B> inline char smin(A &a, const B &b) {
  return b < a ? a = b, 1 : 0;
}

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> pii;

template <typename I> inline void read(I &x) {
  int f = 0, c;
  while (!isdigit(c = getchar()))
    c == '-' ? f = 1 : 0;
  x = c & 15;
  while (isdigit(c = getchar()))
    x = (x << 1) + (x << 3) + (c & 15);
  f ? x = -x : 0;
}

const int N = 2e5 + 7;
const int INF = 0x3f3f3f3f;

int w, h, n, dis;
int b[N];

struct Node {
  int x, y;
} a[N], p[N], q1[N], q2[N];

struct SGT {
#define lc o << 1
#define rc o << 1 | 1
  struct Node {
    int max, add;
  } t[N << 2];

  inline void build(int o, int L, int R) {
    t[o].add = 0, t[o].max = h;
    if (L == R)
      return;
    int M = (L + R) >> 1;
    build(lc, L, M), build(rc, M + 1, R);
  }
  inline void add(int o, int L, int R, int l, int r, int k) {
    if (l > r)
      return;
    if (l <= L && R <= r)
      return t[o].max += k, t[o].add += k, (void)0;
    int M = (L + R) >> 1;
    if (l <= M)
      add(lc, L, M, l, r, k);
    if (r > M)
      add(rc, M + 1, R, l, r, k);
    t[o].max = std::max(t[lc].max, t[rc].max) + t[o].add;
  }
} t;

inline int find(int x) { return std::lower_bound(b + 1, b + dis + 1, x) - b; }
inline void lsh() {
  std::sort(b + 1, b + n + 1);
  dis = 0;
  for (int i = 1; i <= n; ++i)
    if (i == i || b[i] != b[i - 1])
      b[++dis] = b[i];
  for (int i = 1; i <= n; ++i)
    p[i].x = find(p[i].x);
}
inline int solve() {
  for (int i = 1; i <= n; ++i)
    p[i] = a[i], b[i] = p[i].x;
  lsh();
  // for (int i = 1; i <= n; ++i) dbg("%d%c", p[i].x, " \n"[i == n]);
  std::sort(p + 1, p + n + 1, [](const auto &a, const auto &b) {
    return a.x < b.x || (a.x == b.x && a.y < b.y);
  });
  int hh = h / 2, tp1 = 0, tp2 = 0, ans = 0;
  t.build(1, 1, dis);
  for (int i = 1; i <= n; ++i) {
    t.add(1, 1, dis, 1, p[i].x, b[p[i].x] - b[p[i - 1].x]);
    smax(ans, t.t[1].max);
    if (p[i].y <= hh) {
      while (tp1 && p[i].y >= q1[tp1].y) {
        t.add(1, 1, dis, q1[tp1 - 1].x + 1, q1[tp1].x, q1[tp1].y);
        --tp1;
      }
      q1[++tp1] = p[i];
      t.add(1, 1, dis, q1[tp1 - 1].x + 1, q1[tp1].x, -p[i].y);
      // dbg("q1: ");
      // for (int i = 1; i <= tp1; ++i) dbg("(%d, %d) ", q1[i].x, q1[i].y);
      // dbg("\n");
    } else {
      while (tp2 && p[i].y <= q2[tp2].y) {
        t.add(1, 1, dis, q2[tp2 - 1].x + 1, q2[tp2].x, h - q2[tp2].y);
        --tp2;
      }
      q2[++tp2] = p[i];
      t.add(1, 1, dis, q2[tp2 - 1].x + 1, q2[tp2].x, p[i].y - h);
      // dbg("q2: ");
      // for (int i = 1; i <= tp2; ++i) dbg("(%d, %d) ", q2[i].x, q2[i].y);
      // dbg("\n");
    }
  }
  return ans << 1;
}

inline void work() {
  int ans = solve();
  std::swap(w, h);
  for (int i = 1; i <= n; ++i)
    std::swap(a[i].x, a[i].y);
  smax(ans, solve());
  printf("%d\n", ans);
}

inline void init() {
  read(w), read(h), read(n);
  for (int i = 1; i <= n; ++i)
    read(a[i].x), read(a[i].y);
  a[++n] = Node{0, 0};
  a[++n] = Node{w, h};
}

int main() {
#ifdef hzhkk
  freopen("hkk.in", "r", stdin);
#endif
  init();
  work();
  fclose(stdin), fclose(stdout);
  return 0;
}

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

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template <typename A, typename B> inline char smax(A &a, const B &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename A, typename B> inline char smin(A &a, const B &b) {
  return b < a ? a = b, 1 : 0;
}

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> pii;

template <typename I> inline void read(I &x) {
  int f = 0, c;
  while (!isdigit(c = getchar()))
    c == '-' ? f = 1 : 0;
  x = c & 15;
  while (isdigit(c = getchar()))
    x = (x << 1) + (x << 3) + (c & 15);
  f ? x = -x : 0;
}

const int N = 3e5 + 7;
const int INF = 0x3f3f3f3f;

int w, h, n, dis;
int b[N];

struct Node {
  int x, y;
} a[N], p[N], q1[N], q2[N];

struct SGT {
#define lc o << 1
#define rc o << 1 | 1
  struct Node {
    int max, add;
  } t[N << 2];

  inline void build(int o, int L, int R) {
    t[o].add = 0, t[o].max = h;
    if (L == R)
      return;
    int M = (L + R) >> 1;
    build(lc, L, M), build(rc, M + 1, R);
  }
  inline void add(int o, int L, int R, int l, int r, int k) {
    if (l > r)
      return;
    if (l <= L && R <= r)
      return t[o].max += k, t[o].add += k, (void)0;
    int M = (L + R) >> 1;
    if (l <= M)
      add(lc, L, M, l, r, k);
    if (r > M)
      add(rc, M + 1, R, l, r, k);
    t[o].max = std::max(t[lc].max, t[rc].max) + t[o].add;
  }
} t;

inline int find(int x) { return std::lower_bound(b + 1, b + dis + 1, x) - b; }
inline void lsh() {
  std::sort(b + 1, b + n + 1);
  dis = 0;
  for (int i = 1; i <= n; ++i)
    if (i == i || b[i] != b[i - 1])
      b[++dis] = b[i];
  for (int i = 1; i <= n; ++i)
    p[i].x = find(p[i].x);
}
inline int solve() {
  for (int i = 1; i <= n; ++i)
    p[i] = a[i], b[i] = p[i].x;
  lsh();
  // for (int i = 1; i <= n; ++i) dbg("%d%c", p[i].x, " \n"[i == n]);
  std::sort(p + 1, p + n + 1, [](const auto &a, const auto &b) {
    return a.x < b.x || (a.x == b.x && a.y < b.y);
  });
  int hh = h / 2, tp1 = 0, tp2 = 0, ans = 0;
  t.build(1, 1, dis);
  for (int i = 1; i <= n; ++i) {
    t.add(1, 1, dis, 1, p[i].x, b[p[i].x] - b[p[i - 1].x]);
    smax(ans, t.t[1].max);
    if (p[i].y <= hh) {
      while (tp1 && p[i].y >= q1[tp1].y) {
        t.add(1, 1, dis, q1[tp1 - 1].x + 1, q1[tp1].x, q1[tp1].y);
        --tp1;
      }
      q1[++tp1] = p[i];
      t.add(1, 1, dis, q1[tp1 - 1].x + 1, q1[tp1].x, -p[i].y);
      // dbg("q1: ");
      // for (int i = 1; i <= tp1; ++i) dbg("(%d, %d) ", q1[i].x, q1[i].y);
      // dbg("\n");
    } else {
      while (tp2 && p[i].y <= q2[tp2].y) {
        t.add(1, 1, dis, q2[tp2 - 1].x + 1, q2[tp2].x, h - q2[tp2].y);
        --tp2;
      }
      q2[++tp2] = p[i];
      t.add(1, 1, dis, q2[tp2 - 1].x + 1, q2[tp2].x, p[i].y - h);
      // dbg("q2: ");
      // for (int i = 1; i <= tp2; ++i) dbg("(%d, %d) ", q2[i].x, q2[i].y);
      // dbg("\n");
    }
  }
  return ans << 1;
}

inline void work() {
  int ans = solve();
  std::swap(w, h);
  for (int i = 1; i <= n; ++i)
    std::swap(a[i].x, a[i].y);
  smax(ans, solve());
  printf("%d\n", ans);
}

inline void init() {
  read(w), read(h), read(n);
  for (int i = 1; i <= n; ++i)
    read(a[i].x), read(a[i].y);
  a[++n] = Node{0, 0};
  a[++n] = Node{w, h};
}

int main() {
#ifdef hzhkk
  freopen("hkk.in", "r", stdin);
#endif
  init();
  work();
  fclose(stdin), fclose(stdout);
  return 0;
}