poj 1151 Atlantis

http://poj.org/problem?id=1151

这道题就是给你一些矩形的左上角和右下角的坐标,这些矩形可能有重叠,求这些矩形覆盖的面积。先把x坐标和y坐标分别离散化。然后再求面积。

 1 #include <cstdio>

 2 #include <cstring>

 3 #include <algorithm>

 4 #define maxn 510

 5 using namespace std;

 6 

 7 int n;

 8 double x2,y2,x1,y1;

 9 bool flag[maxn][maxn];

10 double X[maxn],Y[maxn];

11 struct node

12 {

13     double x1,y1,x2,y2;

14 } p[maxn];

15 

16 int bsearch(double *a,int l,int r,double target)

17 {

18     int low=l,high=r;

19     while(low<=high)

20     {

21         int mid=(low+high)>>1;

22         if(a[mid]==target)

23         {

24             return mid;

25         }

26         if(a[mid]>target)

27             high=mid-1;

28         else

29             low=mid+1;

30     }

31 }

32 

33 int main()

34 {

35     int case1=0;

36     while(scanf("%d",&n)!=EOF)

37     {

38         memset(flag,false,sizeof(flag));

39         memset(X,0,sizeof(X));

40         memset(Y,0,sizeof(Y));

41         case1++;

42         if(n==0) break;

43         int t1=0,t2=0;

44         for(int i=0; i<n; i++)

45         {

46             scanf("%lf%lf%lf%lf",&p[i].x1,&p[i].y1,&p[i].x2,&p[i].y2);

47             X[t1++]=p[i].x1;X[t1++]=p[i].x2;

48             Y[t2++]=p[i].y1;Y[t2++]=p[i].y2;

49         }

50         sort(X,X+2*n);

51         sort(Y,Y+2*n);

52         for(int i=0; i<n; i++)

53         {

54             int xpos=bsearch(X,0,t1-1,p[i].x1);

55             int ypos=bsearch(Y,0,t2-1,p[i].y1);

56             int xpos1=bsearch(X,0,t1-1,p[i].x2);

57             int ypos1=bsearch(Y,0,t2-1,p[i].y2);

58             for(int i=xpos; i<xpos1; i++)

59             {

60                 for(int j=ypos; j<ypos1; j++)

61                 {

62                     flag[i][j]=true;

63                 }

64             }

65         }

66         double sum=0;

67         for(int i=0; i<t1; i++)

68         {

69             for(int j=0; j<t2; j++)

70             {

71                 if(flag[i][j])

72                     sum+=((X[i+1]-X[i])*(Y[j+1]-Y[j]));

73             }

74         }

75         printf("Test case #%d\n",case1);

76         printf("Total explored area: %.2lf\n",sum);

77         printf("\n");

78     }

79     return 0;

80 }
View Code

 

你可能感兴趣的:(ant)