Language: C++

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define N 300010
using namespace std;

int W, H, n;
int x[N], y[N];
/*
struct node {
	int mx;
}tr[N << 2];

void update(int x) {
	tr[x].mx = max(tr[x << 1].mx, tr[x << 1 | 1].mx);
}

void build(int a, int b, int x) {
	tr[x].mx = 0;
	if (a < b) {
		int mid = a + b >> 1;
		build(a, mid, x << 1);
		build(mid + 1, b, x << 1 | 1);
	}
}

void modify(int x, int a, int b, int pos, int ad) {
	if (a == b) tr[x].mx = ad;
	int mid = a + b >> 1;
	if (pos <= mid) modify(x << 1, a, mid, pos, ad);
	else modify(x << 1 | 1, mid + 1, b, pos, ad);
	update(x);
}

int query(int x, int a, int b, int pos) {
	if (a == b) return tr[x].mx;
	int mid = l + r >> 1;
	int res = 0;
	if (l <= mid) res = max(res, query(x << 1, a, mid, l, r));
	if (r > mid) res = max(res, query(x << 1 | 1, mid + 1, b, l, r));
	update(x);
	return res;
}
*/

int data[N];

int ans = 0;
double halfW, halfH;

bool cmp(const int a, const int b) {
	return y[a] < y[b];
}

int r[N];
int top[2];
int s[2][N];
void solve() {
	for (int i = 1; i <= n; ++i) r[i] = i;
	sort(r + 1, r + n + 1, cmp);
	int min1 = W, max2 = 0;
//cout << endl;
	for (int i = 1; i <= n; ++i) {
		bool b = x[r[i]] > halfW;
//cout << x[r[i]] << ' ' << y[r[i]] << endl;
		ans = max(ans, (min1 - max2) * 2 + y[r[i]] * 2);
//cout << "ans: " << ans << endl;
		if (b) min1 = min(min1, x[r[i]]);
		else max2 = max(max2, x[r[i]]);
		while (top[b] && (x[r[i]] < x[r[s[b][top[b]]]]) == b && x[r[i]] != x[r[s[b][top[b]]]]) {
			if (top[b ^ 1]) {
				int p = *lower_bound(s[b ^ 1] + 1, s[b ^ 1] + top[b ^ 1] + 1, s[b][top[b]]);
				if (p <= top[b ^ 1]) data[s[b][top[b]]] = abs(x[r[s[b][top[b]]]] - x[r[p]]) - y[r[s[b][top[b]]]];
				else data[s[b][top[b]]] = (b ? x[r[s[b][top[b]]]] : (W - x[r[s[b][top[b]]]])) - y[r[s[b][top[b]]]];
			}
			else {
				data[s[b][top[b]]] = (b ? x[r[s[b][top[b]]]] : W - x[r[s[b][top[b]]]]) - y[r[s[b][top[b]]]];
			}
			ans = max(ans, data[top[b]] * 2 + 2 * y[r[i]]);
			data[s[b][top[b]--]] = 0;
		}
		s[b][++top[b]] = i;
	}
	
	ans = max(ans, (min1 - max2) * 2 + H * 2);
	int p = 0, q = 1, t[2] = {top[p], top[q]};
	while (t[p] || t[q]) {
		if (!t[p] || t[q] && s[p][t[p]] < s[q][t[q]]) swap(p, q);
		if (t[q] < top[q]) {
			int pos = s[q][t[q] + 1];
			data[s[p][t[p]]] = abs(x[r[s[p][t[p]]]] - x[r[pos]]) - y[r[s[p][t[p]]]];
		}
		else {
			data[s[p][t[p]]] = (p ? x[r[s[p][t[p]]]] : W - x[r[s[p][t[p]]]]) - y[r[s[p][t[p]]]];
		}
		ans = max(ans, data[s[p][t[p]]] * 2 + H * 2);
//cout << "ans: " << ans << ' ' << p << ' ' << x[r[s[p][t[p]]]] << ' ' << s[p][t[p]] << ' ' << data[s[p][t[p]]] << endl;
		t[p]--;
	}
}

int main() {
	scanf("%d%d%d", &W, &H, &n);
	halfW = (double)W / 2; halfH = (double)H / 2;
	for (int i = 1; i <= n; ++i) {
		scanf("%d%d", &x[i], &y[i]);
	}
	
	memset(data, 0, sizeof(data));
	solve();
	
	swap(W, H);
	swap(halfW, halfH);
	for (int i = 1; i <= n; ++i) {
		swap(x[i], y[i]);
	}
	memset(data, 0, sizeof(data));
	solve();
	
	printf("%d\n", max(ans, 2 * max(W, H) + 2);
}

Compiler output:
a.cc: In function 'void solve()':
a.cc:75:54: error: reference to 'data' is ambiguous
   75 |                                 if (p <= top[b ^ 1]) data[s[b][top[b]]] = abs(x[r[s[b][top[b]]]] - x[r[p]]) - y[r[s[b][top[b]]]];
      |                                                      ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:2:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:48:5: note:                 'int data [300010]'
   48 | int data[N];
      |     ^~~~
a.cc:76:38: error: reference to 'data' is ambiguous
   76 |                                 else data[s[b][top[b]]] = (b ? x[r[s[b][top[b]]]] : (W - x[r[s[b][top[b]]]])) - y[r[s[b][top[b]]]];
      |                                      ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:48:5: note:                 'int data [300010]'
   48 | int data[N];
      |     ^~~~
a.cc:79:33: error: reference to 'data' is ambiguous
   79 |                                 data[s[b][top[b]]] = (b ? x[r[s[b][top[b]]]] : W - x[r[s[b][top[b]]]]) - y[r[s[b][top[b]]]];
      |                                 ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:48:5: note:                 'int data [300010]'
   48 | int data[N];
      |     ^~~~
a.cc:81:40: error: reference to 'data' is ambiguous
   81 |                         ans = max(ans, data[top[b]] * 2 + 2 * y[r[i]]);
      |                                        ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:48:5: note:                 'int data [300010]'
   48 | int data[N];
      |     ^~~~
a.cc:82:25: error: reference to 'data' is ambiguous
   82 |                         data[s[b][top[b]--]] = 0;
      |                         ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:48:5: note:                 'int data [300010]'
   48 | int data[N];
      |     ^~~~
a.cc:93:25: error: reference to 'data' is ambiguous
   93 |                         data[s[p][t[p]]] = abs(x[r[s[p][t[p]]]] - x[r[pos]]) - y[r[s[p][t[p]]]];
      |                         ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:48:5: note:                 'int data [300010]'
   48 | int data[N];
      |     ^~~~
a.cc:96:25: error: reference to 'data' is ambiguous
   96 |                         data[s[p][t[p]]] = (p ? x[r[s[p][t[p]]]] : W - x[r[s[p][t[p]]]]) - y[r[s[p][t[p]]]];
      |                         ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:48:5: note:                 'int data [300010]'
   48 | int data[N];
      |     ^~~~
a.cc:98:32: error: reference to 'data' is ambiguous
   98 |                 ans = max(ans, data[s[p][t[p]]] * 2 + H * 2);
      |                                ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:48:5: note:                 'int data [300010]'
   48 | int data[N];
      |     ^~~~
a.cc: In function