Problem Description
Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) that specify the opposite corners of a rectangle. All coordinates will be integers in the range 0 to 100. For example, the line
5 8 7 10
specifies the rectangle who’s corners are(5,8),(7,8),(7,10),(5,10).
If drawn on graph paper, that rectangle would cover four squares. Your job is to count the number of unit(i.e.,1*1) squares that are covered by any one of the rectangles given as input. Any square covered by more than one rectangle should only be counted once.
Input
The input format is a series of lines, each containing 4 integers. Four -1’s are used to separate problems, and four -2’s are used to end the last problem. Otherwise, the numbers are the x-ycoordinates of two points that are opposite corners of a rectangle.
Output
Your output should be the number of squares covered by each set of rectangles. Each number should be printed on a separate line.
Sample Input
5 8 7 10
6 9 7 8
6 8 8 11
-1 -1 -1 -1
0 0 100 100
50 75 12 90
39 42 57 73
-2 -2 -2 -2
Sample Output
8
10000
#include "stdio.h"
#include "algorithm"
using namespace std;
const int maxn = 110;
int n,pos;
int y[maxn*2];
struct node
{
int x,y1,y2;
int f;
}Line[maxn*2];
struct Node
{
int y1,y2,len,inlen;
int ld,rd,c;
}tree[maxn*4];
bool cmp( node a,node b )
{
return a.x < b.x;
}
void swap( int &a,int &b )
{
int temp = a;
a = b;
b = temp;
}
void buildtree( int ld,int rd,int t )
{
tree[t].c = 0;
tree[t].y1 = y[ld]; tree[t].y2 = y[rd];
tree[t].ld = ld; tree[t].rd = rd;
tree[t].len = 0; tree[t].inlen = 0;
if( ld + 1 == rd )
return;
int mid = ( ld+rd )>>1;
buildtree( ld,mid,t<<1 );
buildtree( mid,rd,t<<1|1 );
}
void calen( int t ) //求线段树被覆盖1次长度
{
if( tree[t].c > 0 ) // t区间f是否被覆盖
{
tree[t].len = tree[t].y2 - tree[t].y1;
return;
}
if( tree[t].ld +1 == tree[t].rd ) tree[t].len = 0;
else
tree[t].len = tree[t<<1].len + tree[t<<1|1].len; // 回带len的值
}
void incalen(int t) //求交集时用到 线段被覆盖两次或两次以上
{
if(tree[t].c >= 2)
{
tree[t].inlen = tree[t].y2 - tree[t].y1;
return;
}
if( tree[t].ld+1 == tree[t].rd ) tree[t].inlen = 0;
else if( tree[t].c == 1 )
{
tree[t].inlen = tree[t<<1].len + tree[t<<1|1].len;
}
else tree[t].inlen = tree[t<<1].inlen + tree[t<<1|1].inlen;
}
void updata( int t,node e )
{
if( e.y1 == tree[t].y1 && e.y2 == tree[t].y2 )
{
tree[t].c += e.f;
calen(t);
//incalen(t); //求交集时用到
return;
}
if( e.y2 <= tree[t<<1].y2 )
updata( t<<1,e );
else if( e.y1 >= tree[t<<1|1].y1 ) //e在tree[t]的右半边
updata( t<<1|1,e );
else
{
node temp = e;
temp.y2 = tree[t<<1].y2;
updata( t<<1,temp );
temp = e;
temp.y1 = tree[t<<1|1].y1;
updata( t<<1|1,temp );
}
calen(t);
//incalen(t); //求交集时用到
}
int main()
{
//freopen( "data.txt","r",stdin );
int x1,y1,x2,y2;
while( true )
{
pos = 0;
while( scanf("%d%d%d%d",&x1,&y1,&x2,&y2) == 4 && x1 != -1 && x1 != -2 )
{
if( x1 > x2 ) //把坐标1作为左上角 2作为右下角
swap( x1,x2 );
if( y1 > y2 )
swap( y1,y2 );
pos++;
Line[pos].x = x1; Line[pos].f = 1;
Line[pos].y1 = y1; Line[pos].y2 = y2;
y[pos] = y1;
pos++;
Line[pos].x = x2; Line[pos].f = -1;
Line[pos].y1 = y1; Line[pos].y2 = y2;
y[pos] = y2;
}
sort( y+1,y+pos+1 );
sort( Line+1,Line+pos+1,cmp );
buildtree( 1,pos,1 );
int ans = 0;
for( int i = 1; i <= pos; i ++ )
{
ans += tree[1].len * ( Line[i].x - Line[i-1].x );
updata( 1,Line[i] );
}
printf("%d\n",ans);
if( x1 == -2 )
break;
}
return 0;
}