http://www.elijahqi.win/archives/554
同poj1177 只不过这次只用了离散化+扫描线
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 11000
using namespace std;
int n;
struct node{
int w,xl,xr,flag;
};
node ax[MAXN],ay[MAXN];
int mapx[2*MAXN],mapy[2*MAXN],lx[2*MAXN],ly[2*MAXN];
bool cmp(node ax1,node ax2){
return ax1.w==ax2.w?ax1.flag>ax2.flag:ax1.w<ax2.w;
}
int main(){
freopen("hdu1828.in","r",stdin);
freopen("hdu1828.out","w",stdout);
while (scanf("%d",&n)>0){
for (int i=1;i<=n;++i){
int x1,x2,y1,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1+=MAXN;x2+=MAXN;y1+=MAXN;y2+=MAXN;
if (x1>x2) swap(x1,x2);
if (y1>y2) swap(y1,y2);
ax[2*i-1].w=y1;ax[2*i-1].xl=x1;ax[2*i-1].xr=x2;ax[2*i-1].flag=1;
ax[2*i].w=y2;ax[2*i].xl=x1;ax[2*i].xr=x2;ax[2*i].flag=-1;
ay[2*i-1].w=x1;ay[2*i-1].xl=y1;ay[2*i-1].xr=y2;ay[2*i-1].flag=1;
ay[2*i].w=x2;ay[2*i].xl=y1;ay[2*i].xr=y2;ay[2*i].flag=-1;
mapx[x1]=1;mapx[x2]=1;
mapy[y1]=1;mapy[y2]=1;
}
sort(ax+1,ax+2*n+1,cmp);
sort(ay+1,ay+2*n+1,cmp);
//for (int i=1;i<=2*n;++i) printf("%d %d %d\n",ax[i].w,ax[i].xl,ax[i].xr);
int num=0,num1=0;
for (int i=0;i<=20000;++i){
if (mapx[i]==1) ++num,mapx[i]=num,lx[num]=i;
if (mapy[i]==1) ++num1,mapy[i]=num1,ly[num1]=i;
}
for (int i=1;i<=2*n;++i){
ax[i].xl=mapx[ax[i].xl];ax[i].xr=mapx[ax[i].xr];
ay[i].xl=mapy[ay[i].xl];ay[i].xr=mapy[ay[i].xr];
}
//for (int i=1;i<=2*n;++i) printf("%d %d %d\n",ax[i].w,ax[i].xl,ax[i].xr);
int tmp[2*MAXN],perimeter=0;
memset(tmp,0,sizeof (tmp));
for (int i=1;i<=2*n;++i){
if (ax[i].flag==1) for (int j=ax[i].xl;j<ax[i].xr;++j) tmp[j]++;
if (ax[i].flag==-1)
for (int j=ax[i].xl;j<ax[i].xr;++j){
tmp[j]--;
if (tmp[j]==0) perimeter+=2*(lx[j+1]-lx[j]);
}
}
memset(tmp,0,sizeof(tmp));
for (int i=1;i<=2*n;++i){
if (ay[i].flag==1) for (int j=ay[i].xl;j<ay[i].xr;++j) tmp[j]++;
if (ay[i].flag==-1)
for (int j=ay[i].xl;j<ay[i].xr;++j){
tmp[j]--;
if (tmp[j]==0) perimeter+=2*(ly[j+1]-ly[j]);
}
}
printf("%d",perimeter);
}
return 0;
}