HDU covers an area of 1255 square meters

Source: Internet
Author: User
/* When you do this, you should refer to others. Program Now, let's take a look back and take a good look at the ideas of this question first. This question needs to be discretization. Second, calculates the area of a rectangle. When a line segment is covered twice or more times, the area is calculated once. The specific drawing shows the third and most important part. The number of line segments is once, I was confused when I didn't know the essence of the number of line segments during the update of more. Now let's come back and think about it, once more is the first type of information, indicating the nature of the current interval. When all are returned recursively, the recurrence relationship updates once more, and more is introduced by once */# include <stdio. h> # include <stdlib. h> # include <math. h> # define EPS 1e-8 # define L (x) <1) # define R (x) <1 | 1) const int maxn = 2222; struct line {Double X, Y1, Y2; int flag ;}; bool equal (doub Le A, double B) {If (FABS (a-B) <= EPS) return true; return false;} int doublecmp (const void * a, const void * B) {return * (double *) A-* (double *) B> 0? 1:-1;} int CMP (const void * a, const void * B) {line * L1 = (line *) A; line * L2 = (line *) B; if (equal (L1-> X, L2-> X) {return L1-> flag-L2-> flag;} return (L1-> X) -(L2-> X)> 0? 1:-1; // pay attention to the size of floating point numbers} struct segtree {int L, R; int cov; // Number of overwrites double once, more; // overwrite the length once and overwrite the length twice}; line [2 * maxn]; segtree [maxn * 4]; // Space Double pointy [2 * maxn]; void bulid (INT left, int right, int t) {tree [T]. L = left; tree [T]. R = right; tree [T]. cov = 0; tree [T]. once = tree [T]. more = 0.0; tree [L (t)]. cov = 0; tree [L (t)]. once = tree [L (t)]. more = 0.0; tree [r (t)]. cov = 0; tree [r (t)]. once = tree [r (t)]. mor E = 0.0; If (right-left = 1) return; int mid = (left + right)> 1; bulid (left, mid, L (t )); bulid (MID, right, r (t);} int bsearch (int l, int R, double temp) {While (L <= r) {int mid = (L + r)> 1; if (equal (pointy [Mid], temp) {return mid;} else if (pointy [Mid]> temp) {r = mid-1;} else if (pointy [Mid] <temp) {L = Mid + 1;} return-1;} void RR (INT t, int L, int R) {// we will discuss if (tree [T]. cov> 1) {// overwrite multiple trees [T]. once = 0; tree [T]. more = pointy [R]-Pointy [l];} else if (tree [T]. cov = 1) {// If the length is overwritten once, the length is the sum of the length of the son overwrite once and twice. If the length is overwritten once, the length is the total length minus the length of the overwrite many times because it is impossible not to be replaced. covered Area tree [T]. more = tree [L (t)]. more + tree [r (t)]. more + tree [L (t)]. once + tree [r (t)]. once; tree [T]. once = pointy [R]-Pointy [l]-tree [T]. more;} else {// = 0 is not overwritten if (R-l = 1) {// leaf tree [T]. once = tree [T]. more = 0;} else {// update your own once moretree [t ]. Once = tree [L (t)]. once + tree [r (t)]. once; tree [T]. more = tree [L (t)]. more + tree [r (t)]. more ;}} void Update (int l, int R, int T, int flag) {/* This recursion is not my style. Haha, It's really copy. */If (L> = tree [T]. r | r <= tree [T]. l) // return is not in this interval; If (L <= tree [T]. L & R> = tree [T]. r) {// contains this Interval Tree [T]. cov + = flag; RR (T, tree [T]. l, tree [T]. r); // if the range is overwritten, RR once more then return directly; // important} Update (L, R, L (t), flag); Update (L, r, R (t), Flag); RR (T, tree [T]. l, tree [T]. r); // The RR function updates the node's once more} int main () {int t, n, I, j; double x1, x2, Y1, Y2; scanf ("% d", & T); While (t --) {scanf ("% d", & N); j = 1; for (I = 1; I <= N; I ++) {scanf ("% lf", & X1, & Y1, & X2, & Y2 ); line [J]. X = x1; line [J]. y1 = Y1; line [J]. y2 = Y2; line [J]. flag = 1; pointy [J] = Y1; j ++; line [J]. X = x2; line [J]. y1 = Y1; line [J]. y2 = Y2; line [J]. flag =-1; pointy [J] = Y2; j ++;} qsort (pointy + 1, 2 * n, sizeof (pointy [0]), doublecmp); qsort (LINE + 1, 2 * n, sizeof (line [0]), CMP ); // discretization int size = 1; for (I = 2; I <= 2 * n; I ++) {If (! Equal (pointy [I], pointy [I-1]) {pointy [++ size] = pointy [I] ;}} bulid (1, size, 1 ); double ans = 0.0; for (I = 1; I <= 2 * n; I ++) {if (I! = 1) {ans + = (line [I]. x-line [I-1]. x) * tree [1]. more;} int Y1 = bsearch (1, size, line [I]. y1); int y2 = bsearch (1, size, line [I]. y2); Update (Y1, Y2, 1, line [I]. flag);} printf ("%. 2lf \ n ", ANS);} return 0 ;}

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.