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

Atlantis HDU - 1542(线段树+扫描线)

时间:2019-08-17 16:28:41      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:java   struct   sts   play   width   tor   starting   memory   for   

技术图片

 

Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23452    Accepted Submission(s): 9278

Problem Description

There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.

Input

The input file consists of several test cases. Each test case starts with a line containing a single integer n (1<=n<=100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.

The input file is terminated by a line containing a single 0. Don’t process it.

Output

For each test case, your program should output one section. The first line of each section must be “Test case #k”, where k is the number of the test case (starting with 1). The second one must be “Total explored area: a”, where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.

Output a blank line after each test case.

Sample Input

2 10 10 20 20 15 15 25 25.5 0

Sample Output

Test case #1

Total explored area: 180.00

分析:模板题

代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 110;
 4 
 5 struct edge
 6 {
 7     double l, r;
 8     double h;
 9     int pos;
10     bool operator < (const edge &a)const
11     {
12         return h < a.h;
13     }
14 }e[maxn << 1];
15 
16 struct node
17 {
18     int l, r;
19     double sum;
20     int flag;
21 }t[maxn << 3];
22 int n;
23 double x[maxn << 1];
24 
25 void pushup(int tar)
26 {
27     if (t[tar].flag) t[tar].sum = x[t[tar].r + 1] - x[t[tar].l];
28     else if (t[tar].l == t[tar].r) t[tar].sum = 0;
29     else t[tar].sum = t[tar << 1].sum + t[tar << 1 | 1].sum;
30 }
31 
32 void build(int l, int r, int tar)
33 {
34     t[tar].l = l, t[tar].r = r, t[tar].sum = 0, t[tar].flag = 0;
35     if (l == r) return;
36     int mid = (l + r) >> 1;
37     build(l, mid, tar << 1);
38     build(mid + 1, r, tar << 1 | 1);
39 }
40 
41 void update(int l, int r, int v, int tar)
42 {
43     if (l == t[tar].l && r == t[tar].r)
44     {
45         t[tar].flag += v;
46         pushup(tar);
47         return;
48     }
49     int mid = (t[tar].l + t[tar].r) >> 1;
50     if (r <= mid) update(l, r, v, tar << 1);
51     else if (l > mid) update(l, r, v, tar << 1 | 1);
52     else update(l, mid, v, tar << 1), update(mid + 1, r, v, tar << 1 | 1);
53     pushup(tar);
54 }
55 
56 int main()
57 {
58     int num, cnt;
59     double x1, y1, x2, y2;
60     int cases = 0;
61 
62     while (cin >> n && n)
63     {
64         num = cnt = 0;
65         for (int i = 1; i <= n; i++)
66         {
67             scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
68             e[++num].l = x1, e[num].r = x2, e[num].h = y1, e[num].pos = -1;
69             e[++num].l = x1, e[num].r = x2, e[num].h = y2, e[num].pos = 1;
70             x[++cnt] = x1, x[++cnt] = x2;
71         }
72         sort(e + 1, e + 1 + num);
73         sort(x + 1, x + 1 + cnt);
74         int tot = unique(x + 1, x + 1 + cnt) - (x + 1);
75         //cout << tot << endl;
76         build(1, tot, 1);
77         double ans = 0;
78         for (int i = 1; i <= num; i++)
79         {
80             int l = lower_bound(x + 1, x + 1 + tot, e[i].l) - x;
81             int r = lower_bound(x + 1, x + 1 + tot, e[i].r) - x - 1;
82             //cout << l << " " << r << endl;
83             update(l, r, e[i].pos, 1);
84             ans += (e[i + 1].h - e[i].h) * t[1].sum;
85         }
86         printf("Test case #%d\nTotal explored area: %.2f\n\n", ++cases, ans);
87     }
88 }

 

Atlantis HDU - 1542(线段树+扫描线)

标签:java   struct   sts   play   width   tor   starting   memory   for   

原文地址:https://www.cnblogs.com/liuwenhan/p/11369048.html

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