Description
There is a rectangle in the xy-plane, with its lower left corner at (0,0) and its upper right corner at (W,H). Each of its sides is parallel to the x-axis or y-axis. Initially, the whole region within the rectangle is painted white.
Snuke plotted N points into the rectangle. The coordinate of the i-th (1≤i≤N) point was (xi,yi).
Then, for each 1≤i≤N, he will paint one of the following four regions black:
- the region satisfying x<xi within the rectangle
- the region satisfying x>xi within the rectangle
- the region satisfying y<yi within the rectangle
- the region satisfying y>yi within the rectangle
Find the longest possible perimeter of the white region of a rectangular shape within the rectangle after he finishes painting.
题意:给定一个$W\times H$的二维平面,初始均为白色,有$n$个关键点$(x_{i},y_{i})$,对于每一个关键点选择一个方向,并将该方向上的所有网格涂成黑色。易得操作后白色部分一定是一个矩形,请最大化矩形周长。
分析:
观察可以得到一个性质,答案矩形一定会经过直线$x=\frac{W}{2}$或$y=\frac{H}{2}$。两种情况可以用相同的方式处理出答案。
将坐标离散化后,枚举矩形的上下边界,可以直接计算出矩形的左右边界。考虑用线段树进行优化。左右各开一个单调栈,在维护单调栈时在线段树上进行区间加减即可。(其实画图比较方便理解。
#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
#define lc(x) x<<1
#define rc(x) x<<1|1
using namespace std;
const int N=3e5+;
int w,h,n,ans,L,R;
int mx[N*],tag[N*];
struct node{int x,y;node(int _x=,int _y=):x(_x),y(_y){};}p[N],a[N],b[N];
int read()
{
int x=,f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
void modify(int x,int l,int r,int v)
{
if(L<=l&&r<=R){mx[x]+=v;tag[x]+=v;return;}
int mid=(l+r)>>;
if(L<=mid)modify(lc(x),l,mid,v);
if(R>mid)modify(rc(x),mid+,r,v);
mx[x]=max(mx[lc(x)],mx[rc(x)])+tag[x];
}
bool cmp(node a,node b){return a.x<b.x||(a.x==b.x&&a.y<b.y);}
void work()
{
memset(mx,,sizeof(mx));
memset(tag,,sizeof(tag));
sort(p+,p+n+,cmp);
int l=,r=;
for(int i=;i<=n-;i++)
{
if(p[i].y<=h/)
{
int nxt=i-;
while(l&&a[l].y<p[i].y)
{
L=a[l].x;R=nxt;nxt=a[l].x-;
modify(,,n,a[l].y-p[i].y);l--;
}
if(nxt!=i-)a[++l]=node(nxt+,p[i].y);
}
else
{
int nxt=i-;
while(r&&b[r].y>p[i].y)
{
L=b[r].x;R=nxt;nxt=b[r].x-;
modify(,,n,p[i].y-b[r].y);r--;
}
if(nxt!=i-)b[++r]=node(nxt+,p[i].y);
}
a[++l]=node(i,);b[++r]=node(i,h);
L=i;R=i;modify(,,n,h-p[i].x);
ans=max(ans,mx[]+p[i+].x);
}
}
int main()
{
w=read();h=read();n=read();
for(int i=;i<=n;i++)p[i].x=read(),p[i].y=read();
p[++n]=node(,);p[++n]=node(w,h);work();
for(int i=;i<=n;i++)swap(p[i].x,p[i].y);
swap(w,h);work();
printf("%d",ans*);
return ;
}