# 【NOI2018模拟4.3】paint

1<=n<=100000

### 题解：

Code：

#include<cstdio>
#include<cstring>
#include<algorithm>
#define fo(i, x, y) for(int i = x; i <= y; i ++)
using namespace std;

const int N = 2e5 + 5;

int w, h, n, ans, num[N];
struct node {
int x, y;
node (int _x = 0, int _y = 0) {x = _x, y = _y;}
} a[N], b[N];

int pl, pr, px;
struct tree {
int mx, z;
} t[N * 4];
void down(int i) {
if(t[i].z) {
t[i + i].z += t[i].z; t[i + i].mx += t[i].z;
t[i + i + 1].z += t[i].z; t[i + i + 1].mx += t[i].z;
t[i].z = 0;
}
}
void add(int i, int x, int y) {
if(y < pl || x > pr) return;
if(x >= pl && y <= pr) {t[i].z += px; t[i].mx += px;return;}
int m = x + y >> 1; down(i);
add(i + i, x, m); add(i + i + 1, m + 1, y);
t[i].mx = max(t[i + i].mx, t[i + i + 1].mx);

}
void cc(int x, int y, int c) {
pl = x, pr = y, px = c; add(1, 1, n + 2);
}

int z1[N], z2[N], d1, d2, z;

bool cmp(node a, node b) {return a.x < b.x;}

int b1[N], b2[N];

void GG() {
memset(b1, 0, sizeof b1);
memset(b2, 0, sizeof b2);
z = h / 2;
a[n + 1] = node(0, z + 1); a[n + 2] = node(0, z); a[n + 3] = node(w, h);
sort(a + 1, a + n + 4, cmp);
memset(t, 0, sizeof t);
int tot = 0;
fo(i, 1, n + 3) {
if(i == 1 || a[i].x != a[i - 1].x)
++ tot, cc(tot, tot, h - a[i].x);
num[i] = tot;
}
fo(i, 1, n + 3)
if(a[i].y > z) {
if(!b1[num[i]]) b1[num[i]] = i; else {
if(a[i].y < a[b1[num[i]]].y)
b1[num[i]] = i;
}
} else {
if(!b2[num[i]]) b2[num[i]] = i; else {
if(a[i].y > a[b2[num[i]]].y)
b2[num[i]] = i;
}
}
d1 = d2 = 1;
fo(i, 1, n + 3) {
if(!a[i].x && a[i].y == z) z1[1] = i;
if(!a[i].x && a[i].y == z + 1) z2[1] = i;
}
fo(i, 1, n + 3) {
if(!a[i].x && (a[i].y == z || a[i].y == z + 1))
continue;
if(a[i].y > z) {
if(i != b1[num[i]]) continue;
} else {
if(i != b2[num[i]]) continue;
}
ans = max(ans, t[1].mx + a[i].x);
if(a[i].y > z) {
cc(num[z1[d1]], num[i] - 1, a[i].y - h);
while(a[z1[d1]].y > a[i].y) {
cc(num[z1[d1 - 1]], num[z1[d1]] - 1, a[i].y - a[z1[d1]].y);
d1 --;
}
z1[++ d1] = i;
} else {
cc(num[z2[d2]], num[i] - 1, -a[i].y);
while(a[z2[d2]].y < a[i].y) {
cc(num[z2[d2 - 1]], num[z2[d2]] - 1, a[z2[d2]].y - a[i].y );
d2 --;
}
z2[++ d2] = i;
}
}
return;
}

int main() {
freopen("paint.in", "r", stdin);
freopen("paint.out", "w", stdout);
scanf("%d %d %d", &w, &h, &n);
fo(i, 1, n) scanf("%d %d", &a[i].x, &a[i].y);
fo(i, 1, n) b[i] = a[i];
GG();
swap(w, h); fo(i, 1, n) a[i] = node(b[i].y, b[i].x);
GG();
printf("%d", ans * 2);
}