POJ 3168 排序+扫描

题意:
这里写图片描述

思路:
我们可以把每个矩形拆成四条线

与x轴平行的放在一起
与y轴平行的放在一起
排个序 判一判有没有交 有交 则说明不可扩张 统计一下 就可以了

处理的姿势很重要
姿势不对毁一生

//By SiriusRen
#include <cstdio>
#include <algorithm>
using namespace std;
#define N 55555 
int n,x1,y1,x2,y2,cnt,vis[N],ans,maxx;
struct Edgex{int x,y1,y2,id;bool operator < (const Edgex &a)const{if(x!=a.x)return x<a.x;return y2<a.y2;}}edgex[N];
struct Edgey{int y,x1,x2,id;bool operator < (const Edgey &a)const{if(y!=a.y)return y<a.y;return x1<a.x1;}}edgey[N];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2),vis[i]=1,
        edgex[++cnt].x=x1,edgex[cnt].y1=y2,edgex[cnt].y2=y1,edgex[cnt].id=i,
        edgey[cnt].y=y1,edgey[cnt].x1=x1,edgey[cnt].x2=x2,edgey[cnt].id=i,
        edgex[++cnt].x=x2,edgex[cnt].y1=y2,edgex[cnt].y2=y1,edgex[cnt].id=i,
        edgey[cnt].y=y2,edgey[cnt].x1=x1,edgey[cnt].x2=x2,edgey[cnt].id=i;
    sort(edgex+1,edgex+1+cnt),sort(edgey+1,edgey+1+cnt);
    for(int i=1,j=i;i<=cnt;i=j,maxx=edgex[i].y1){
        while(edgex[i].x==edgex[j].x)j++;
        for(int k=i+1;k<j;k++){
            if(edgex[k].y2<=maxx)vis[edgex[k].id]=vis[edgex[k-1].id]=0;
            maxx=max(maxx,edgex[k].y1);
        }
    }
    for(int i=1,j=i;i<=cnt;i=j,maxx=edgey[i].x2){
        while(edgey[i].y==edgey[j].y)j++;
        for(int k=i+1;k<j;k++){
            if(edgey[k].x1<=maxx)vis[edgey[k].id]=vis[edgey[k-1].id]=0;
            maxx=max(maxx,edgey[k].x2);
        }
    }
    for(int i=1;i<=n;i++)ans+=vis[i];
    printf("%d
",ans);
}

这里写图片描述

原文地址:https://www.cnblogs.com/SiriusRen/p/6532196.html