[BZOJ2687]交与并

[BZOJ2687]交与并

输入示例

4
1 6
4 8
2 7
3 5


输出示例

24


题解

#include <bits/stdc++.h>
using namespace std;
#define rep(i, s, t) for(int i = (s), mi = (t); i <= mi; i++)
#define dwn(i, s, t) for(int i = (s), mi = (t); i >= mi; i--)

int x = 0, f = 1; char c = getchar();
while(!isdigit(c)) { if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}

#define maxn 1000010
#define pii pair <int, int>
#define x first
#define y second
#define mp(x, y) make_pair(x, y)
#define LL long long

int n, N;
pii _lr[maxn], lr[maxn];

LL calc(pii a, pii b) { return (LL)(max(a.y, b.y) - min(a.x, b.x)) * (min(a.y, b.y) - max(a.x, b.x)); }
const bool cmp(const pii &a, const pii &b) { return a.x != b.x ? a.x < b.x : a.y > b.y; }
const bool wrap(const pii &a, const pii &b) { return (a.x <= b.x && b.y < a.y) || (a.x < b.x && b.y <= a.y); }

struct Info {
int l, r, p;
Info(int _1 = 0, int _2 = 0, int _3 = 0): l(_1), r(_2), p(_3) {}
} S[maxn];
int hd, top;
LL ans;

int main() {

sort(_lr + 1, _lr + n + 1, cmp);
lr[++N] = _lr[1];
rep(i, 2, n) if(_lr[i].y > lr[N].y) lr[++N] = _lr[i]; else ans = max(ans, calc(lr[N], _lr[i]));
S[hd = top = 1] = Info(2, N, 1);
rep(i, 2, N) {
while(hd < top && i > S[hd].r) hd++;
ans = max(ans, calc(lr[S[hd].p], lr[i]));
while(top > hd && calc(lr[i], lr[S[top].l]) >= calc(lr[S[top].p], lr[S[top].l])) top--;
int l = i + 1, r = N;
while(l < r) {
int mid = l + r >> 1;
if(calc(lr[S[top].p], lr[mid]) > calc(lr[i], lr[mid])) l = mid + 1; else r = mid;
}
if(calc(lr[S[top].p], lr[l]) > calc(lr[i], lr[l])) continue;
S[top+1] = Info(l, N, i);
S[top].r = l - 1; ++top;
}

printf("%lld\n", ans);

return 0;
}

posted @ 2018-07-15 17:46  xjr01  阅读(1199)  评论(3编辑  收藏  举报