HDU 1542 Atlanta (matrix coverage area of Line Segment tree)

Source: Internet
Author: User


Question: Calculate the coverage area for N matrices.

Thought: I read others' answer reports

Given the coordinates in the lower left corner of a rectangle and the coordinates in the upper right corner are respectively: (x1, Y1) and (X2, Y2). For such a rectangle, we construct two line segments, one positioned in X1, in the Y coordinate, the range is [Y1, y2], and the value of a cover domain is 1. The other line segment is located in X2, And the range is [Y1, y2], given a cover value of-1. Based on this method, two line segments are constructed for each rectangle, and all line segments are sorted from left to right based on the located X.


# Include <iostream> # include <stdio. h> # include <string. h >#include <algorithm> using namespace STD; # define M 100 # define INF 0x3fffffff # define maxn 500000*2 struct seg {int flag; double up, down, X ;} line [M * 5]; int CMP (seg a, seg B) {return. x <B. x;} struct tree {double up, down, X; int cover; bool flag;} tree [M * m]; Double Y [M * 3]; // split y and build void build (int id, int L, int R) {tree [ID]. down = Y [l]; tree [ID]. up = Y [R]; tree [ID]. flag = false; tree [ID]. cover = 0; tree [ID]. X =-1; if (L + 1 = r) {tree [ID]. flag = true; return;} int mid = (L + r)/2; build (ID * 2, L, mid); Build (ID * 2 + 1, mid, r);} double insert (int id, double X, double L, Double R, int flag) // flag indicates left or right {If (tree [ID]. down> = r | tree [ID]. up <= L) return 0; If (tree [ID]. flag) {If (tree [ID]. cover> 0) // Recursion to the leaf node {double temp_x = tree [ID]. x; double ans = (x-temp_x) * (tree [ID]. up-tree [ID]. down); tree [ID]. X = x; // locate the last xtree [ID]. cover + = flag; return ans;} else {tree [ID]. cover + = flag; tree [ID]. X = x; return 0 ;}} double ans1, ans2; ans1 = insert (ID * 2, X, L, R, flag ); ans2 = insert (ID * 2 + 1, x, L, R, flag); Return ans1 + ans2;} int main () {int T, CA = 1; while (scanf ("% d", & T) {double x1, x2, Y1, Y2; int K = 0; For (INT I = 0; I <t; I ++) {scanf ("% lf", & X1, & Y1, & X2, & Y2); k ++; Y [k] = Y1; line [K]. down = Y1; line [K]. up = Y2; line [K]. X = x1; line [K]. flag = 1; // 1 indicates K ++ on the left; y [k] = Y2; line [K]. X = x2; line [K]. down = Y1; line [K]. up = Y2; line [K]. flag =-1; //-1 indicates the right} Sort (Y + 1, Y + k + 1); sort (LINE + 1, line + 1 + k, CMP ); build (1, 1, k); double ans = 0; For (INT I = 1; I <= K; I ++) ans + = insert (1, line [I]. x, line [I]. down, line [I]. up, line [I]. flag); printf ("Test Case # % d \ ntotal received area: %. 2f \ n ", CA ++, ANS);} return 0;}/* 210 10 20 2015 15 25 25.50 */


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.