Counting Squares
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1885 Accepted Submission(s): 946
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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mem(a,x) memset(a,x,sizeof(a))
#define ls i<<1
#define rs i<<1|1
#define m(i) ((q[i].l + q[i].r)>>1)
using namespace std;
typedef long long ll;
const int N = 40000;
long long x[2*N];//横坐标
struct Edge
{
int l,r;//这条线的左右端点的横坐标
int h;//这条线的纵坐标
int f;//这条线是矩形的上边还是下边
}e[N<<1];
bool cmp(Edge a,Edge b)
{
return a.h < b.h;
}
struct Node
{
int l,r;//横坐标的区间,是横坐标数组的下标
int s;//该节点被覆盖的情况(是否完全覆盖)
int len;//该区间被覆盖的总长度
}q[N*8];
void build(int i,int l,int r)
{
q[i].l = l,q[i].r = r;
q[i].s = 0;q[i].len = 0;
if (l == r) return;
int mid = m(i);
build(ls,l,mid);
build(rs,mid+1,r);
}
void pushup(int i)
{
if (q[i].s)
{
q[i].len = x[q[i].r+1] - x[q[i].l];
}
else if (q[i].l == q[i].r)
{
q[i].len = 0;
}
else
{
q[i].len = q[ls].len + q[rs].len;
}
}
void update(int i,int l,int r,int xx)//
{
if (q[i].l == l&&q[i].r == r)
{
q[i].s += xx;
pushup(i);
return;
}
int mid = m(i);
if (r <= mid) update(ls,l,r,xx);
else if (l > mid) update(rs,l,r,xx);
else
{
update(ls,l,mid,xx);
update(rs,mid+1,r,xx);
}
pushup(i);
}
int main()
{
while(1){
int x1,x2,y1,y2;
int tot = 0,flag=0;
while (scanf("%d %d %d %d",&x1,&y1,&x2,&y2)!=EOF)
{
if(x1==-2){
flag=1;
break;
}
if(x1==-1)break;
if(x1>x2)swap(x1,x2);
if(y1>y2)swap(y1,y2);
Edge &t1 = e[tot];Edge &t2 = e[1+tot];
t1.l = t2.l = x1,t1.r = t2.r = x2;
t1.h = y1;t1.f = 1;
t2.h = y2;t2.f = -1;
x[tot] = x1;x[tot+1] = x2;
tot += 2;
}
sort(e,e+tot,cmp);//边按高度从小到大排序(自下而上扫描)
sort(x,x+tot);
int k = 1;
for (int i = 1;i < tot;++i)
{
if (x[i] != x[i-1])
{
x[k++] = x[i];
}
}
build(1,0,k-1);
long long ans = 0;
for (int i = 0;i < tot;++i)
{
int l = lower_bound(x,x+k,e[i].l) - x;
int r = lower_bound(x,x+k,e[i].r) - x - 1;
update(1,l,r,e[i].f);
ans += (e[i+1].h - e[i].h)*q[1].len;
}
printf("%lld\n",ans);
if(flag)
break;
}
return 0;
}
看到大神用的hash方法代码超简单
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <conio.h>
// 计算图形的面积,由于放在了Hash专题,想啊想,用Hash法怎么去做
// 将每一个图形单元看做是一个Hash对象,即用数组模拟一个二维的空间
int hash[105][105];
int main()
{
int x1=1, y1, x2, y2,cnt= 0, flag;
while(scanf( "%d%d%d%d", &x1, &y1, &x2, &y2 ))
{
flag= cnt= 0;
memset( hash, 0, sizeof( hash ) );
while( ( x1!= -1&& y1!= -1&& x2!= -1&& y2!= -1 ) )
{
if( x1== -2&& y1== -2&& x2== -2&& y2== -2 )
{
flag= 1;
break;
}
if( x1> x2 )
x1^= x2^= x1^= x2;
if( y1> y2 )
y1^= y2^= y1^= y2;
for( int i= x1; i< x2; ++i )
{
for( int j= y1; j< y2; ++j )
hash[i][j]= 1;
}
scanf( "%d%d%d%d", &x1, &y1, &x2, &y2 );
}
for( int i= 0; i< 100; ++i )
{
for( int j= 0; j< 100; ++j )
if( hash[i][j] )
cnt++;
}
printf( "%d\n", cnt );
if( flag )
break;
}
return 0;
}