PKU 1151 Atlanta (line segment tree + discretization + scanning line)

Source: Internet
Author: User

Not to mention it first, because it is not quite understandable ~~~~~~

# Include <iostream> <br/> # include <cstdlib> <br/> # define N 105 </P> <p> using namespace STD; </P> <p> struct ELEM {<br/> double X; <br/> double S; <br/> double T; <br/> bool flag; <br/>}; </P> <p> struct node {<br/> int L; <br/> int R; <br/> int cover; <br/> double m; <br/>}; </P> <p> ELEM cor [2 * n]; <br/> node tree [8 * n]; <br/> double index [2 * n]; <br/> int Len; <br/> int N; <br/> double res; </P> <p> int CMP (const void * a, const Void * B) <br/>{< br/> If (* (ELEM *) ). x> (* (ELEM *) B ). x) <br/> return 1; <br/> else if (* (ELEM *) ). X = (* (ELEM *) B ). x) <br/> return 0; <br/> else return-1; <br/>}</P> <p> int CMP1 (const void *, const void * B) <br/>{< br/> If (* (double *) A> * (double *) B) <br/> return 1; <br/> else if (* (double *) A = * (double *) B) <br/> return 0; <br/> else return-1; <br/>}</P> <p> void Init () <br/>{< br/> int I; <br/> int K; <Br/> double X1, Y1, X2, Y2; <br/> for (I = 0; I <n; I ++) {<br/> CIN> x1> Y1> X2> Y2; <br/> K = I <1; <br/> cor [K]. X = x1; Cor [K]. S = Y1; Cor [K]. T = Y2; <br/> cor [K]. flag = true; <br/> cor [k + 1]. X = x2; Cor [k + 1]. S = Y1; Cor [k + 1]. T = Y2; <br/> cor [k + 1]. flag = false; <br/> index [k] = Y1; index [k + 1] = Y2; <br/>}< br/> qsort (Cor, 2 * n, sizeof (Cor [0]), CMP); <br/> qsort (index, 2 * n, sizeof (index [0]), CMP1 ); <br/> Len = 0; <br /> For (I = 1; I <2 * n; I ++) {<br/> If (index [I]! = Index [I-1]) <br/> index [+ Len] = index [I]; <br/>}< br/> // for (I = 0; I <= Len; I ++) <br/> // cout <index [I] <''; <br/> // cout <Endl; <br/> return; <br/>}</P> <p> int find (Double X) <br/>{< br/> int base = 0; <br/> int Top = Len; <br/> int mid; // cout <x <''; <br/> while (base <= top) {<br/> mid = (base + TOP)/2; <br/> If (x> index [Mid]) <br/> base = Mid + 1; <br/> else if (x <index [Mid]) <br/> Top = mid-1; <br/> else return mid; <br/>}< br/> return-1; <br/>}</P> <p> void buid_tree (int K, int left, int right) <br/>{< br/> int mid; <br/> int L, R; <br/> tree [K]. L = left; tree [K]. R = right; <br/> tree [K]. cover = 0; tree [K]. m = 0; <br/> If (tree [K]. r-tree [K]. L = 1) <br/> return; <br/> mid = (tree [K]. L + tree [K]. r)/2; <br/> L = k <1; r = L + 1; <br/> buid_tree (L, left, mid ); <br/> buid_tree (R, mid, right); <br/> return; <br/>}</P> <p> void init_m (int K) <br/>{< br/> int L, R; <br/> If (tree [K]. cover> 0) {<br/> tree [K]. M = index [tree [K]. r]-index [tree [K]. l]; <br/> return; <br/>}< br/> L = k <1; r = L + 1; <br/> If (tree [K]. r-tree [K]. l> 1) <br/> tree [K]. M = tree [l]. m + tree [R]. m; <br/> else tree [K]. m = 0; <br/> return; <br/>}</P> <p> void tree_insert (int K, int left, int right) <br/>{< br/> int mid; <br/> int L, R; <br/> If (left <= tree [K]. L & right> = tree [K]. r) {<br/> tree [K]. cover ++; <br/> init_m (k); <br/> return; <br/>}< br/> mid = (tree [K]. L + tree [K]. r)/2; <br/> L = k <1; r = L + 1; <br/> If (left <mid) tree_insert (L, left, right); <br/> If (Right> mid) tree_insert (R, left, right); <br/> init_m (k ); // cout <tree [K]. m <''; <br/> return; <br/>}</P> <p> void tree_delete (int K, int left, int right) <br/>{< br/> int mid; <br/> int L, R; <br/> If (left <= tree [K]. L & right> = tree [K]. r) {<br/> tree [K]. cover --; <br/> init_m (k); <br/> return; <br/>}< br/> mid = (tree [K]. L + tree [K]. r)/2; <br/> L = k <1; r = L + 1; <br/> If (left <mid) tree_delete (L, left, right); <br/> If (Right> mid) tree_delete (R, left, right); <br/> init_m (k); <br/> return; <br/>}</P> <p> void slove () <br/>{< br/> int I; <br/> res = 0; <br/> for (I = 0; I <(2 * N-1); I ++) {<br/> If (Cor [I]. flag) <br/> tree_insert (1, find (Cor [I]. s), find (Cor [I]. t); <br/> else tree_delete (1, find (Cor [I]. s), find (Cor [I]. t); <br/> res + = tree [1]. M * (Cor [I + 1]. x-Cor [I]. x); <br/>}</P> <p> int main () <br/>{< br/> int walk = 0; <br/> while (CIN> N & N) {<br/> Init (); <br/> buid_tree (1, 0, Len ); <br/> slove (); <br/> printf ("Test Case # % d/N", ++ walk); <br/> printf ("total occupied ed area: %. 2lf/n ", Res); <br/>}< br/> return 0; <br/>}

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.