Hangzhou Electric HDU ACM 1225 Atlantis (the most basic of line-segment tree scanning lines discretization)

Source: Internet
Author: User
Tags cmath

ACM First scan line problem, in fact, the algorithm principle is very good understanding, but the specific implementation or encountered many problems, I try to refer to the online two parts to solve the problem of segment tree representation interval, the first is the real value of each node, such as the update of the 1 to 4 interval after discretization, We update the line tree with 1 ~ 3, so that a single node can also represent a minimum unit interval. The second is to change the usual strategy at the time of the build, 1 ~ 10 for the total interval, two children for 1 ~ 5, 5 to 10.
Core Difficulty: When we find a need to update the interval, the first should update the cover value, and then determine whether it is overwritten, or to remove the overwrite, if it is just overwritten, then the sum of this node is the node true interval value. If it is just lifting the overlay, then it is necessary to see if his child is covered, and if so, the two child should be added to the father sum.
when backtracking to determine whether each sub-root node is overwritten, if overridden then use the current parent node's value to continue to participate in recursion, if the parent node is not overwritten should update the child value, and then continue to participate in recursion. You will find that if you look down from the root node, you can see that the interval represented by the first layer of covered nodes is exactly the sum value of the total interval.
Atlantis Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 8948 Accepted Submission (s): 3833


Problem Descriptionthere is 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 the write a program that calculates this quantity.

Inputthe input file consists of several test cases. Each test case is 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 the 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) is 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.
Outputfor Each test case, the your program should the output one section. The first line of all sections must be ' test Case #k ', where k is the number of the 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 the Union of all Rec Tangles in this test case), printed exact to both digits to the right of the decimal point.

Output a blank line after each test case.

Sample Input
210 10 20 2015 15 25 25.50

Sample Output

Sourcemid-central European Regional Contest 2000

Version 1:
/*=============================================================================## Author:liangshu-cbam # # QQ        : 756029571 # # School: Harbin Polytechnic University # # last modified:2015-08-11 12:30## filename:a.cpp## Description: # The people who is crazy enough to think they can change the world, is the ones who does! =============================================================================*/# #include <iostream> # include<sstream> #include <algorithm> #include <cstdio> #include <string.h> #include <c ctype> #include <string> #include <cmath> #include <vector> #include <stack> #includ    e<queue> #include <map> #include <set> using namespace std;const int INF = 2024;struct line {    Double S, e, Y, Mark;        Line (double s, double e, double y, double ma): s (s), E (e), Y (y), Mark (MA) {} bool friend operator < (line A, line B) {    return A.Y < B.y; }};struct tree{int cover; int lft,rht;double sum;} Tree[inf<<2];vector<double>x;vector<line>l;map<double, int>h;void create (int root, int left    , int right) {tree[root].lft = left;    Tree[root].rht = right;    tree[root].sum = 0;        if (left + 1! = right) {int mid = (left + right)/2;        Create (Root<<1, left, mid);    Create (root<<1|1, Mid, right); } return; void update (int root, int val, int left, int right) {if (Tree[root].lft >= left && Tree[root].rht <= right        ) {Tree[root].cover + = val;        if (tree[root].cover) {tree[root].sum = X[tree[root].rht]-X[tree[root].lft];return;            } else{tree[root].sum = Tree[root<<1].sum + tree[root<<1|1].sum;        return;    }} if (tree[root].lft = = Tree[root].rht) return;    int mid = (tree[root].lft + tree[root].rht)/2;    if (left < mid) {update (Root <<1, Val, left, right); } if (right >= mid) {Update (root<<1|1,val,left, right); } if (!tree[root].cover) tree[root].sum = Tree[root<<1].sum + tree[root<<1|1].sum;}    int main () {int n,cs = 1; while (~SCANF ("%d", &n) &&n) {h.clear (); L.clear ();        X.clear ();        Double x1, y1, x2, y2;            for (int i = 1; I <= n; i++) {scanf ("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);            L.push_back (Line (x1, x2, y1, 1));            L.push_back (Line (x1, x2, y2,-1)); X.push_back (x1);        X.push_back (x2);        } sort (X.begin (), X.end ());       Sort (L.begin (), L.end ());       X.erase (Unique (X.begin (), X.end ()), X.end ());       for (int i = 0; i < x.size (); i++) {H[x[i]] = i;       } Create (1, 0, X.size ()-1);       Double ans = 0;int i;               for (i = 0; i < l.size ()-1; i++) {update (1, L[i].mark, H[l[i].s], h[l[i].e]);       Ans + = (l[i + 1].y-l[i].y) * tree[1].sum; } update (1, L[i].Mark, H[l[i].s], h[l[i].e]);    printf ("Test case #%d\ntotal explored area:%.2lf\n\n", cs++, ans); }}

Version 2:

/*=============================================================================## Author:liangshu-cbam # # QQ        : 756029571 # # School: Harbin Polytechnic University # # last modified:2015-08-11 12:50## filename:a.cpp## Description: # The people who is crazy enough to think they can change the world, is the ones who does! =============================================================================*/# #include <iostream># include<sstream> #include <algorithm> #include <cstdio> #include <string.h> #include < cctype> #include <string> #include <cmath> #include <vector> #include <stack> #include < queue> #include <map> #include <set>using namespace std;const int INF = 2222;struct line{double s, E, Y, MA RK;} L[inf];d ouble sum[inf<<2];int cnt[inf<<2];vector<double>dict;bool cmp (line A, line B) {return A.Y &lt ; B.Y;} void Pushup (int rt, int l, int r) {if (Cnt[rt]) {sum [RT] = Dict[r + 1]-DICT[L]; } else Sum[rt] = sum[rt<<1] + sum[rt<<1|1];}    int bin (double k, int len) {int L = 0, r = len-1;        while (L <= r) {int mid = (L + r) >>1;        if (dict[mid] = = k) return mid;        else if (Dict[mid] > k) r = mid-1;    else L = mid + 1; } return-1;} void update (int rt, int l, int r, int l, int r, int val) {if (L <= l && R <= R) {Cnt[rt] + = VA        L        Pushup (RT, L, R);    return;    } int mid = (L + r) >>1;    if (l <= mid) Update (rt<<1, L, R, L, Mid, Val);    if (R > Mid) Update (RT&LT;&LT;1|1,L, R, Mid + 1, R, Val); Pushup (rt,l, R);}    int main () {int n,cs = 1;        while (scanf ("%d", &n)! = EOF && N) {memset (sum, 0, sizeof (sum));        int t = 0;            for (int i = 1; I <= n; i++) {double x1,y1,x2,y2;            scanf ("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);            Dict.push_back (x1); L[t].s = x1;            L[T].E = x2;            L[t].y = y1;            L[t++].mark = 1;            Dict.push_back (x2);            L[T].S = x1;            L[T].E = x2;            L[t].y = y2;        L[t++].mark =-1;        } sort (L, L + t, CMP);        Sort (dict. Begin (), Dict.end ());        Dict.erase (Unique (Dict.begin (), Dict.end ()), Dict.end ());        Double ans = 0;            for (int i = 0; i < T; i + +) {int L = Bin (L[i].s, dict.size ());            int r = Bin (l[i].e, Dict.size ())-1;            if (l <= r) Update (1, L, R, 0, Dict.size ()-1, L[i].mark);        Ans + = sum[1] * (L[i + 1].y-l[i].y);    } printf ("Test case #%d\ntotal explored area:%.2lf\n\n", cs++, ans); } return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hangzhou Electric HDU ACM 1225 Atlantis (the most basic of line-segment tree scanning lines discretization)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.