Problem p03950 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
#define N 201002
struct quji {
  int l, r, mid, mx, laz;
} c[N << 2];
struct node {
  int x, y;
} a[N];
struct qj {
  int st, va;
} s1[N], s2[N];
int w, h, n, ans, tot1, tot2;
bool cmp(node n1, node n2) { return n1.y < n2.y; }
void bt(int ind, int l, int r) {
  c[ind].l = l;
  c[ind].r = r;
  c[ind].mid = (l + r) >> 1;
  c[ind].laz = 0;
  if (l == r) {
    c[ind].mx = -a[l].y;
    return;
  }
  bt(ind << 1, l, c[ind].mid);
  bt(ind << 1 | 1, c[ind].mid + 1, r);
  c[ind].mx = max(c[ind << 1].mx, c[ind << 1 | 1].mx);
}
void pushdown(int ind) {
  c[ind << 1].laz += c[ind].laz;
  c[ind << 1].mx += c[ind].laz;
  c[ind << 1 | 1].laz += c[ind].laz;
  c[ind << 1 | 1].mx += c[ind].laz;
  c[ind].laz = 0;
}
void chng(int ind, int l, int r, int x) {
  if (l > r)
    return;
  if (l == 0)
    return;
  if (l <= c[ind].l && c[ind].r <= r) {
    c[ind].laz += x;
    c[ind].mx += x;
    return;
  }
  pushdown(ind);
  if (l <= c[ind].mid)
    chng(ind << 1, l, r, x);
  if (r > c[ind].mid)
    chng(ind << 1 | 1, l, r, x);
  c[ind].mx = max(c[ind << 1].mx, c[ind << 1 | 1].mx);
}
int ask(int ind, int l, int r) {
  if (l > r)
    return -1e9;
  if (l <= c[ind].l && c[ind].r <= r)
    return c[ind].mx;
  int ret = -1e9;
  pushdown(ind);
  if (l <= c[ind].mid)
    ret = max(ret, ask(ind << 1, l, r));
  if (r > c[ind].mid)
    ret = max(ret, ask(ind << 1 | 1, l, r));
  return ret;
}
void solve() {
  sort(a + 1, a + 1 + n, cmp);
  int ls = 0, rs = w;
  for (int i = n; i; i--) {
    ans = max(ans, h - a[i].y + rs - ls);
    if (a[i].x <= w / 2)
      ls = max(ls, a[i].x);
    else
      rs = min(rs, a[i].x);
  }
  ans = max(ans, h + rs - ls);
  tot1 = tot2 = 0;
  bt(1, 1, n);
  ls = 0, rs = w;
  bool f1 = false, f2 = false;
  for (int i = 1; i <= n; i++) {
    /*ls=0,rs=w;
    for(int j=i-1;j;j--)
    {
            ans=max(ans,a[i].y-a[j].y+rs-ls);
            if(a[j].x<=w/2)ls=max(ls,a[j].x);
            else rs=min(rs,a[j].x);
    }
    ans=max(ans,a[i].y+rs-ls);*/
    ans = max(ans, rs - ls + a[i].y);
    int tv = ask(1, 1, i - 1);
    ans = max(ans, tv + a[i].y);
    chng(1, i, i, w);
    if (a[i].x <= w / 2) {
      ls = max(ls, a[i].x);
      int la = i - 1;
      while (tot1 && a[i].x > s1[tot1].va) {
        chng(1, s1[tot1].st, la, s1[tot1].va - a[i].x);
        la = s1[tot1].st - 1;
        tot1--;
      }
      if (la < i - 1)
        s1[++tot1].st = la + 1, s1[tot1].va = a[i].x;
    } else {
      rs = min(rs, a[i].x);
      int la = i - 1;
      while (tot2 && a[i].x < s2[tot2].va) {
        chng(1, s2[tot2].st, la, a[i].x - s2[tot2].va);
        la = s2[tot2].st - 1;
        tot2--;
      }
      if (la < i - 1)
        s2[++tot2].st = la + 1, s2[tot2].va = a[i].x;
    }
    s1[++tot1].st = i, s1[tot1].va = 0;
    s2[++tot2].st = i, s2[tot2].va = w;
  }
  // puts("fin");
  // printf("%d\n",ans);
}
int main() {
  scanf("%d%d%d", &w, &h, &n);
  if (!n) {
    printf("%d\n", 2 * (w + h));
    return 0;
  }
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i].x, &a[i].y);
  solve();
  for (int i = 1; i <= n; i++)
    swap(a[i].x, a[i].y);
  swap(w, h);
  solve();
  printf("%d\n", ans * 2);
  return 0;
}
/*
10 10 4
1 6
4 1
6 9
9 4
*/

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
#define N 301002
struct quji {
  int l, r, mid, mx, laz;
} c[N << 2];
struct node {
  int x, y;
} a[N];
struct qj {
  int st, va;
} s1[N], s2[N];
int w, h, n, ans, tot1, tot2;
bool cmp(node n1, node n2) { return n1.y < n2.y; }
void bt(int ind, int l, int r) {
  c[ind].l = l;
  c[ind].r = r;
  c[ind].mid = (l + r) >> 1;
  c[ind].laz = 0;
  if (l == r) {
    c[ind].mx = -a[l].y;
    return;
  }
  bt(ind << 1, l, c[ind].mid);
  bt(ind << 1 | 1, c[ind].mid + 1, r);
  c[ind].mx = max(c[ind << 1].mx, c[ind << 1 | 1].mx);
}
void pushdown(int ind) {
  c[ind << 1].laz += c[ind].laz;
  c[ind << 1].mx += c[ind].laz;
  c[ind << 1 | 1].laz += c[ind].laz;
  c[ind << 1 | 1].mx += c[ind].laz;
  c[ind].laz = 0;
}
void chng(int ind, int l, int r, int x) {
  if (l > r)
    return;
  if (l == 0)
    return;
  if (l <= c[ind].l && c[ind].r <= r) {
    c[ind].laz += x;
    c[ind].mx += x;
    return;
  }
  pushdown(ind);
  if (l <= c[ind].mid)
    chng(ind << 1, l, r, x);
  if (r > c[ind].mid)
    chng(ind << 1 | 1, l, r, x);
  c[ind].mx = max(c[ind << 1].mx, c[ind << 1 | 1].mx);
}
int ask(int ind, int l, int r) {
  if (l > r)
    return -1e9;
  if (l <= c[ind].l && c[ind].r <= r)
    return c[ind].mx;
  int ret = -1e9;
  pushdown(ind);
  if (l <= c[ind].mid)
    ret = max(ret, ask(ind << 1, l, r));
  if (r > c[ind].mid)
    ret = max(ret, ask(ind << 1 | 1, l, r));
  return ret;
}
void solve() {
  sort(a + 1, a + 1 + n, cmp);
  int ls = 0, rs = w;
  for (int i = n; i; i--) {
    ans = max(ans, h - a[i].y + rs - ls);
    if (a[i].x <= w / 2)
      ls = max(ls, a[i].x);
    else
      rs = min(rs, a[i].x);
  }
  ans = max(ans, h + rs - ls);
  tot1 = tot2 = 0;
  bt(1, 1, n);
  ls = 0, rs = w;
  bool f1 = false, f2 = false;
  for (int i = 1; i <= n; i++) {
    /*ls=0,rs=w;
    for(int j=i-1;j;j--)
    {
            ans=max(ans,a[i].y-a[j].y+rs-ls);
            if(a[j].x<=w/2)ls=max(ls,a[j].x);
            else rs=min(rs,a[j].x);
    }
    ans=max(ans,a[i].y+rs-ls);*/
    ans = max(ans, rs - ls + a[i].y);
    int tv = ask(1, 1, i - 1);
    ans = max(ans, tv + a[i].y);
    chng(1, i, i, w);
    if (a[i].x <= w / 2) {
      ls = max(ls, a[i].x);
      int la = i - 1;
      while (tot1 && a[i].x > s1[tot1].va) {
        chng(1, s1[tot1].st, la, s1[tot1].va - a[i].x);
        la = s1[tot1].st - 1;
        tot1--;
      }
      if (la < i - 1)
        s1[++tot1].st = la + 1, s1[tot1].va = a[i].x;
    } else {
      rs = min(rs, a[i].x);
      int la = i - 1;
      while (tot2 && a[i].x < s2[tot2].va) {
        chng(1, s2[tot2].st, la, a[i].x - s2[tot2].va);
        la = s2[tot2].st - 1;
        tot2--;
      }
      if (la < i - 1)
        s2[++tot2].st = la + 1, s2[tot2].va = a[i].x;
    }
    s1[++tot1].st = i, s1[tot1].va = 0;
    s2[++tot2].st = i, s2[tot2].va = w;
  }
  // puts("fin");
  // printf("%d\n",ans);
}
int main() {
  scanf("%d%d%d", &w, &h, &n);
  if (!n) {
    printf("%d\n", 2 * (w + h));
    return 0;
  }
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i].x, &a[i].y);
  solve();
  for (int i = 1; i <= n; i++)
    swap(a[i].x, a[i].y);
  swap(w, h);
  solve();
  printf("%d\n", ans * 2);
  return 0;
}
/*
10 10 4
1 6
4 1
6 9
9 4
*/