hdu 1542 Atlantis (扫描线方法 o(n^2))

#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define INF 1000000100

const int maxn = 210;

double ty[maxn],kk;

int flag;
struct Line{
double x,y1,y2;
int side;
Line(){}
void get(double xx=0,double yy1=0,double yy2=0,int sidee=0){
        x=xx; y1=yy1; y2=yy2; side=sidee;
    }
bool operator<(const Line& rhs)const{
return x < rhs.x;
}
}line[maxn];
struct Warm_house{
double x;
double y1,y2;
int flag;
 Warm_house(){}
void get(double yy1=0,double yy2=0,int f=0){
    y1=yy1; y2=yy2; flag=f;
}
}warm_house[maxn];
int main()
{
   int n,kase=1;
   double x1,y1,x2,y2;
   while(scanf("%d",&n)==1&&n){
      int id=1;
      for(int i=1;i<=n;i++){
        scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
        ty[id]=y1;
        line[id].get(x1,y1,y2,1);
        id++;
        ty[id]=y2;
        line[id].get(x2,y1,y2,-1);
        id++;
      }

      sort(ty+1,ty+id);
      sort(line+1,line+id);

      for(int i=1;i<id-1;i++){
         warm_house[i].get(ty[i],ty[i+1],0);
      }

      double ans=0;
      for(int i=1;i<id;i++){
           double gy1=line[i].y1,gy2=line[i].y2;
           double x=line[i].x;
           int fff=line[i].side;

           for(int j=1;j<id-1;j++){
                Warm_house& temp = warm_house[j];
                if(gy1<=temp.y1&&temp.y2<=gy2){
                    if(temp.flag>0){
                      ans+=(x-temp.x)*(temp.y2-temp.y1);
                      temp.x=x;
                      temp.flag+=fff;
                    }
                    else {
                        temp.flag+=fff;
                        temp.x=x;
                    }
                }
           }
      }

      printf("Test case #%d\nTotal explored area: ",kase++);
      printf("%.2lf\n\n",ans);
   }
return 0;
}


你可能感兴趣的:(Algorithm,C++,算法,HDU)