码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1255 离散化+扫描线覆盖的面积

时间:2014-07-29 17:37:02      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   java   color   使用   os   

覆盖的面积

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3571    Accepted Submission(s): 1753


Problem Description
给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.

bubuko.com,布布扣
 

 

Input
输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表平面上的一个矩形的左上角坐标和右下角坐标,矩形的上下边和X轴平行,左右边和Y轴平行.坐标的范围从0到100000.

注意:本题的输入数据较多,推荐使用scanf读入数据.
 

 

Output
对于每组测试数据,请计算出被这些矩形覆盖过至少两次的区域的面积.结果保留两位小数.
 

 

Sample Input
2
5
1 1 4 2
1 3 3 7
2 1.5 5 4.5
3.5 1.25 7.5 4
6 3 10 7
3
0 0 1 1
1 0 2 1
2 0 3 1
 

 

Sample Output
7.63
0.00
 
 
 
 
题目意思:
 
输入整数n,下面n行每行有两个坐标分别为矩形的左下角坐标和右上角坐标,求矩形重叠部分的面积。
 
思路:
扫描线扫重叠部分的即可。注意up函数。
 
 
代码:
 
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 using namespace std;
  6 #define N 2010
  7 #define ll root<<1
  8 #define rr root<<1|1
  9 #define mid (a[root].l+a[root].r)/2
 10 
 11 double x[N*2];
 12 
 13 struct Line{
 14     double l, r, h;
 15     int v;
 16     Line(double a=0,double b=0,double c=0,int d=0):l(a),r(b),h(c),v(d){}
 17 }line[N*4];
 18 
 19 struct node{
 20     int l, r;
 21     double sum1, sum2;
 22     int v;
 23 }a[N*4];
 24 
 25 bool cmp(Line a,Line b){
 26     return a.h<b.h;
 27 }
 28 
 29 void build(int l,int r,int root){
 30     a[root].l=l;
 31     a[root].r=r;
 32     a[root].v=0;
 33     a[root].sum1=a[root].sum2=0.0;
 34     if(l==r) return;
 35     build(l,mid,ll);
 36     build(mid+1,r,rr);
 37 }
 38 
 39 int find(double key,int k){
 40     int l=0, r=k;
 41     while(l<=r){
 42         int mm=(l+r)/2;
 43         if(x[mm]==key) return mm;
 44         else if(x[mm]<key){
 45             l=mm+1;
 46         }
 47         else{
 48             r=mm-1;
 49         }
 50     }
 51 }
 52 
 53 void up(int root){
 54     if(a[root].v>1){
 55         a[root].sum1=0.0;
 56         a[root].sum2=x[a[root].r+1]-x[a[root].l];
 57     }
 58     else if(a[root].v==1){
 59         if(a[root].l==a[root].r) {
 60             a[root].sum1=x[a[root].r+1]-x[a[root].l];
 61             a[root].sum2=0.0;
 62         }
 63         else{
 64             a[root].sum2=a[ll].sum1+a[ll].sum2+a[rr].sum1+a[rr].sum2;
 65             a[root].sum1=x[a[root].r+1]-x[a[root].l]-a[root].sum2;
 66         }
 67     }
 68     else{
 69         if(a[root].l==a[root].r){
 70             a[root].sum1=a[root].sum2=0.0;
 71         }
 72         else{
 73             a[root].sum1=a[ll].sum1+a[rr].sum1;
 74             a[root].sum2=a[ll].sum2+a[rr].sum2;
 75         }
 76     }
 77 }
 78 void update(int l,int r,int v,int root){
 79     if(a[root].l>=l&&a[root].r<=r){
 80         a[root].v+=v;
 81         up(root);
 82         return;
 83     }
 84     if(l<=mid) update(l,r,v,ll);
 85     if(r>mid) update(l,r,v,rr);
 86     up(root);
 87 }
 88 
 89 main()
 90 {
 91     int t;
 92     double x1, y1, x2, y2;
 93     int n, m;
 94     int i, j, k;
 95     cin>>t;
 96     while(t--){
 97         scanf("%d",&n);
 98         m=0;
 99         for(i=0;i<n;i++){
100             scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
101             x[m]=x1;
102             line[m++]=Line(x1,x2,y1,1);
103             x[m]=x2;
104             line[m++]=Line(x1,x2,y2,-1);
105         }
106         
107         sort(x,x+m);
108         sort(line,line+m,cmp);
109         k=unique(x,x+m)-x;
110         build(0,k-1,1);
111         double ans=0.0;
112         for(i=0;i<m-1;i++){
113             int l=find(line[i].l,k-1);
114             int r=find(line[i].r,k-1);
115             update(l,r-1,line[i].v,1);
116         //    printf("%.2lf\n",a[1].sum2);
117             ans+=(line[i+1].h-line[i].h)*a[1].sum2;
118         }
119         printf("%.2lf\n",ans);
120     }
121 }

 

HDU 1255 离散化+扫描线覆盖的面积,布布扣,bubuko.com

HDU 1255 离散化+扫描线覆盖的面积

标签:des   style   blog   http   java   color   使用   os   

原文地址:http://www.cnblogs.com/qq1012662902/p/3875789.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!