Counting Squares
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2198 Accepted Submission(s): 1102
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.
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 <stdio.h> #include <algorithm> #include <string.h> using namespace std; struct node { double l,r,h; int z; }a[210]; bool cmp(node a,node b) { return a.h<b.h; } double hh[210]; int mark[210*4],sum[210*4]; void upfather(int n,int L,int R) { if(mark[n])sum[n]=hh[R+1]-hh[L]; else if(L==R)sum[n]=0; else sum[n]=sum[n*2]+sum[n*2+1]; } void update(int l,int r,int d,int n,int L,int R) { if(l<=L&&R<=r) { mark[n]+=d; upfather(n,L,R); return ; } int mid=L+R>>1; if(l<=mid)update(l,r,d,n*2,L,mid); if(r>mid)update(l,r,d,n*2+1,mid+1,R); upfather(n,L,R); } int findx(double i,double *b,int n) { int l=0; int r=n-1; while(l<=r) { int mid=l+r>>1; if(b[mid]==i)return mid; else if(i<b[mid])r=mid-1; else l=mid+1; } return -1; } int main() { double x0=0,x1=0,y0=0,y1=0; while(x0!=-2) { int k=0; while(cin>>x0>>y0>>x1>>y1,x0>=0) { if(x0>x1)swap(x0,x1); if(y0>y1)swap(y0,y1); hh[k]=x0; a[k].l=x0; a[k].r=x1; a[k].h=y0; a[k++].z=1; hh[k]=x1; a[k].l=x0; a[k].r=x1; a[k].h=y1; a[k++].z=-1; } sort(hh,hh+k); int i; int z=1; for(i=1;i<k;++i) { if(hh[i]!=hh[i-1])hh[z++]=hh[i]; } sort(a,a+k,cmp); int ans=0; for(i=0;i<k;++i) { int L=findx(a[i].l,hh,z); int R=findx(a[i].r,hh,z)-1; update(L,R,a[i].z,1,0,z-1); ans+=sum[1]*(a[i+1].h-a[i].h); } cout<<ans<<endl; } return 0; }
代码二:
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int m[105][105]; int main() { int x1,y1,x2,y2; int cnt=0; memset(m,0,sizeof(m)); while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF) { if(x1==-1) { printf("%d\n",cnt); cnt=0; memset(m,0,sizeof(m)); continue; } if(x1==-2) { printf("%d\n",cnt); break; } if(x1>x2)swap(x1,x2); if(y1>y2)swap(y1,y2); int i,j; for(i=x1;i<x2;i++) { for(j=y1;j<y2;j++) { if(m[i][j]==0) { cnt++; m[i][j]=1; } } } } return 0; }