题目描述
YLOI村有一片荒地,上面竖着N个稻草人,村民们每年多次在稻草人们的周围举行祭典。
有一次,YLOI村的村长听到了稻草人们的启示,计划在荒地中开垦一片田地。和启示中的一样,田地需要满足以下条件:
1、田地的形状是边平行于坐标轴的长方形;
2、左下角和右上角各有一个稻草人;
3、田地的内部(不包括边界)没有稻草人。
给出每个稻草人的坐标,请你求出有多少遵从启示的田地的个数
分治
按x坐标分治。
每次按y坐标从上至下扫。
对于分界线左右端维护凸壳,单调栈即可维护。
拿左边的点为左下去看有多少可以组成矩形,显然可视范围最多到它凸壳前一个点的y坐标下,然后去右边二分看看包含多少凸壳上的点。
具体见代码。
#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
using namespace std;
typedef long long ll;
const int maxn=200000+10;
struct dong{
int x,y;
} a[maxn];
int b[maxn],d1[maxn],d2[maxn],id[maxn],c[maxn];
int i,j,k,l,t,n,m,tot,top;
ll ans;
int read(){
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9'){
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
bool cmp(dong a,dong b){
return a.x<b.x;
}
bool cmp2(int x,int y){
return a[x].y>a[y].y;
}
int binary(int x){
//if (!top) return 0;
int l=1,r=top+1,mid;
while (l<r){
mid=(l+r)/2;
if (a[d2[mid]].y<=x) r=mid;else l=mid+1;
}
return l;
}
void solve(int l,int r){
if (l==r){
id[l]=l;
return;
}
int i,j,k,t,mid=(l+r)/2;
solve(l,mid);
solve(mid+1,r);
j=l;k=mid+1;
fo(i,l,r){
if (j>mid) c[i]=id[k++];
else if (k>r) c[i]=id[j++];
else if (a[id[j]].y>a[id[k]].y) c[i]=id[j++];
else c[i]=id[k++];
}
fo(i,l,r) id[i]=c[i];
/*fo(i,l,r) id[i]=i;
sort(id+l,id+r+1,cmp2);*/
tot=top=0;
fo(i,l,r){
if (a[id[i]].x<=mid){
while (tot&&a[d1[tot]].x<a[id[i]].x) tot--;
if (tot) t=a[d1[tot]].y-1;else t=n;
k=binary(t);
ans+=(ll)(top-k+1);
d1[++tot]=id[i];
}
else{
while (top&&a[d2[top]].x>a[id[i]].x) top--;
d2[++top]=id[i];
}
}
}
int main(){
freopen("scarecrows.in","r",stdin);freopen("scarecrows.out","w",stdout);
n=read();
fo(i,1,n) a[i].x=read(),a[i].y=read();
fo(i,1,n) b[i]=a[i].x;
sort(b+1,b+n+1);
fo(i,1,n) a[i].x=lower_bound(b+1,b+n+1,a[i].x)-b;
fo(i,1,n) b[i]=a[i].y;
sort(b+1,b+n+1);
fo(i,1,n) a[i].y=lower_bound(b+1,b+n+1,a[i].y)-b;
sort(a+1,a+n+1,cmp);
solve(1,n);
printf("%lld\n",ans);
}

本文介绍了一个关于寻找符合特定条件的矩形田地数量的问题。通过分治策略及单调栈维护凸壳的方式,实现了高效的算法解决方案。具体而言,算法依据稻草人的位置坐标,计算出所有可能形成的矩形田地数目。
654

被折叠的 条评论
为什么被折叠?



