1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| #include<bits/stdc++.h> namespace Pozhu{ using namespace std; typedef long long ll; #define N 300010 #define inf 0x3f3f3f3f int w,h,n; int ans = -inf; struct node{ int x,y; friend bool operator < (node a,node b) {return a.x < b.x;} }a[N];
struct Tree{ int maxx,lz; }t[N << 2]; #define ls(x) (x << 1) #define rs(x) (x << 1 | 1)
void push_up(int x); void push_down(int x); void add(int x,int l,int r,int ql,int qr,int k); int query(int x,int l,int r,int ql,int qr); void calc();
void main() { ios :: sync_with_stdio(false); cin.tie(0),cout.tie(0); cin >> w >> h >> n; for(int i = 1;i <= n;i++) { cin >> a[i].x >> a[i].y; } a[n+1].x = w,a[n+1].y = h; calc(); swap(w,h); for(int i = 1;i <= n+1;i++) swap(a[i].x,a[i].y); calc(); cout << (ans << 1); return; }
struct flo{ int pos,high; flo(int _p,int _h) : pos(_p),high(_h){} }; stack<flo> up,down;
inline void upins(int x,int h) { while(up.size() > 1 && up.top().high > h) { auto x = up.top();up.pop(); add(1,0,n,up.top().pos,x.pos-1,-x.high); } add(1,0,n,up.top().pos,x-1,h); up.emplace(x,h); }
inline void dwins(int x,int h) { while(down.size() > 1 && down.top().high < h) { auto x = down.top();down.pop(); add(1,0,n,down.top().pos,x.pos-1,x.high); } add(1,0,n,down.top().pos,x-1,-h); down.emplace(x,h); }
inline void calc() { memset(t,0,sizeof(t)); while(up.size()) up.pop(); while(down.size()) down.pop(); up.emplace(0,0); down.emplace(0,0); sort(a+1,a+n+1); for(int i = 1;i <= n+1;i++) { if((a[i-1].y << 1) < h) { upins(i-1,h); dwins(i-1,a[i-1].y); } else { upins(i-1,a[i-1].y); dwins(i-1,0); } add(1,0,n,i-1,i-1,-a[i-1].x); ans = max(ans,a[i].x - a[i-1].x + h); ans = max(ans,query(1,0,n,1,i-2) + a[i].x); } }
int query(int x,int l,int r,int ql,int qr) { if(l > r || ql > qr) return -inf; if(l >= ql && r <= qr) return t[x].maxx; int ans = -inf; push_down(x); int mid = (l + r) >> 1; if(mid >= ql) ans = max(ans,query(ls(x),l,mid,ql,qr)); if(mid < qr) ans = max(ans,query(rs(x),mid+1,r,ql,qr)); return ans; }
void add(int x,int l,int r,int ql,int qr,int k) { if(l > r || ql > qr) return; if(l >= ql && r <= qr) { t[x].maxx += k; t[x].lz += k; return; } int mid = (l + r) >> 1; push_down(x); if(mid >= ql) add(ls(x),l,mid,ql,qr,k); if(mid < qr) add(rs(x),mid+1,r,ql,qr,k); push_up(x); return; }
inline void push_down(int x) { if(!t[x].lz) return; t[ls(x)].maxx += t[x].lz; t[rs(x)].maxx += t[x].lz; t[ls(x)].lz += t[x].lz; t[rs(x)].lz += t[x].lz; t[x].lz = 0; return; }
inline void push_up(int x) { t[x].maxx = max(t[ls(x)].maxx,t[rs(x)].maxx); }
}
signed main() { Pozhu :: main(); return 0; }
|