离散化计算重叠矩形的周长。
称平行于x轴的边为横边,我们以横边为例,某一矩形中y坐标比较小的横边我们称为始边,另一边我们称为终边。用一条扫描线从下往上扫描,当扫到一条始边的时候,如果这条始边的正下方出现过k条始边和k条终边,那么这条始边肯定是没被覆盖住的,统计结果;当扫到一条终边的时候,如果这条始边的正下方出现过k条始边和k-1条终边,同理,统计结果。
注意扫描到的边要拆成单位长度的小边分别分析。
Executing...
Test 1: TEST OK [0.005 secs, 4156 KB]
Test 2: TEST OK [0.008 secs, 4156 KB]
Test 3: TEST OK [0.016 secs, 4156 KB]
Test 4: TEST OK [0.008 secs, 4156 KB]
Test 5: TEST OK [0.016 secs, 4156 KB]
Test 6: TEST OK [0.008 secs, 4156 KB]
Test 7: TEST OK [0.043 secs, 4156 KB]
Test 8: TEST OK [0.014 secs, 4156 KB]
Test 9: TEST OK [0.019 secs, 4156 KB]
Test 10: TEST OK [0.008 secs, 4156 KB]
Test 11: TEST OK [0.103 secs, 4156 KB]
All tests OK.
Your program ('picture') produced all correct answers! This is your submission #4 for this problem. Congratulations!
#include <iostream>
#include <memory.h>
#include <stdio.h>
#include <algorithm>
using namespace std; class CEdge
{
public:
int y;
int x1,x2;
bool isBegin;
CEdge(int y0=,int x10=,int x20=,bool flag=false):y(y0),x1(x10),x2(x20),isBegin(flag){}
bool operator <(const CEdge &e2)const
{
return y<e2.y || y==e2.y && isBegin;
}
}; int cnt[]={};
int n; int solve(CEdge edges[])
{
int ans=;
memset(cnt,,sizeof cnt); sort(edges,edges+*n);
for(int i=;i<*n;i++)
{
CEdge e=edges[i];
for(int j=e.x1;j<e.x2;j++)
{
if(e.isBegin && cnt[j]== || !e.isBegin && cnt[j]==)
ans++; if(e.isBegin)
cnt[j]++;
else
cnt[j]--;
}
}
return ans;
} CEdge eh[],ev[]; int main()
{
freopen("picture.in","r",stdin);
freopen("picture.out","w",stdout);
cin>>n;
for(int i=;i<n;i++)
{
int x1,y1,x2,y2;
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
x1+=;
x2+=;
y1+=;
y2+=; // 加入数组 横边
eh[*i]=CEdge(y1,x1,x2,true);
eh[*i+]=CEdge(y2,x1,x2,false);
// 加入数组 竖边
ev[*i]=CEdge(x1,y1,y2,true);
ev[*i+]=CEdge(x2,y1,y2,false);
} printf("%d\n",solve(eh)+solve(ev));
return ;
}