HDU 1255 覆盖的面积

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1255

题目大意:给出多个矩形坐标,求他们的面积的并,即互相覆盖的面积。

解题思路:与Atlantis那个题目如出一辙,不过要在线段树节点中增加一个 mlen 变量表示区间内重叠数>1的线段长度,那么更新的时候:

若区间重叠数为2,则mlen 的长度是区间的长度

若区间重叠数为1,则mlen 的长度是子区间重叠数为1的长度和

否则,mlen的长度是子区间 重叠数为2的长度和

其余的就与Atlantis那个题目无异了

代码:

 1 const int maxn = 2010;
 2 struct node{
 3     int f;
 4     double rl, rr;
 5     double olen, mlen;
 6 };
 7 node tree[maxn * 4];
 8 struct Line{
 9     double x1, x2, y;
10     int flag;
11     Line() {}
12     Line(double a, double b, double c, int f) : x1(a), x2(b), y(c), flag(f) {}
13     bool operator < (const Line& t) const{
14         return y < t.y;
15     }
16 };
17 Line lines[maxn];
18 double xs[maxn];
19 int n;
20 
21 void build(int l, int r, int k){
22     tree[k].f = 0; tree[k].olen = tree[k].mlen = 0;
23     tree[k].rl = xs[l]; tree[k].rr = xs[r];
24     if(l == r - 1) return;
25     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
26     build(l, mid, lc);
27     build(mid, r, rc);
28 }
29 void cacu(int l, int r, int k){
30     if(tree[k].f >= 2){
31         tree[k].olen = tree[k].mlen = tree[k].rr - tree[k].rl;
32     } 
33     else if(tree[k].f == 1){
34         tree[k].olen = tree[k].rr - tree[k].rl;
35         if(l + 1 == r) tree[k].mlen = 0;
36         else tree[k].mlen = tree[k << 1].olen + tree[k << 1 | 1].olen;
37     }
38     else{
39         if(l + 1 == r) tree[k].olen = tree[k].mlen = 0;
40         else{
41             tree[k].olen = tree[k << 1].olen + tree[k << 1 | 1].olen;
42             tree[k].mlen = tree[k << 1].mlen + tree[k << 1 | 1].mlen;
43         }
44     }
45 }
46 void update(Line ul, int l, int r, int k){
47     if(ul.x1 <= tree[k].rl && ul.x2 >= tree[k].rr){
48         tree[k].f += ul.flag;
49         cacu(l, r, k);
50         return;
51     }
52     if(ul.x1 > tree[k].rr || ul.x2 < tree[k].rl) return;
53     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
54     if(ul.x2 <= tree[lc].rr) update(ul, l, mid, lc);
55     else if(ul.x1 >= tree[rc].rl) update(ul, mid, r, rc);
56     else{
57         update(ul, l, mid, lc);
58         update(ul, mid, r, rc);
59     }
60     cacu(l, r, k);
61 }
62 
63 int main(){
64     int T;
65     scanf("%d", &T);
66     for(int t = 1; t <= T; t++){
67         scanf("%d", &n);
68         int cnt = 1;
69         for(int i = 1; i <= n; i++){
70             double x1, y1, x2, y2;
71             scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
72             lines[cnt] = Line(x1, x2, y1, 1);
73             xs[cnt] = x1;
74             cnt++;
75             lines[cnt] = Line(x1, x2, y2, -1);
76             xs[cnt] = x2;
77             cnt++;
78         }
79         sort(lines + 1, lines + cnt);
80         sort(xs + 1, xs + cnt);
81         cnt--;
82         build(1, cnt, 1);
83         update(lines[1], 1, cnt, 1);
84         double ans = 0;
85         for(int i = 2; i <= cnt; i++){
86             ans += (lines[i].y - lines[i - 1].y) * tree[1].mlen;
87             update(lines[i], 1, cnt, 1);
88         }
89         printf("%.2f\n", ans);
90     }
91 }

题目:

覆盖的面积

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


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

 

 

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
 

 

Author
Ignatius.L & weigang Lee

 

posted @ 2017-08-09 10:06  EricJeffrey  阅读(147)  评论(0编辑  收藏  举报