Poj_1151 Atlanta (line segment tree)

Source: Internet
Author: User
/* The scanning line problem of the Line Segment tree can be used as a template for such problems. I did this after I asked my senior brother...

Create a line segment tree by Y axis. */

Struct node {
Int L, R, C; // C indicates that the data is overwritten. If the value is not 0, the data is overwritten.
Double sum, Len; // Len indicates the length of the current segment, and sum indicates the valid length. Is the length of the area that can be included in the calculation.
} Node [n <2];

Struct line {
Double X; // record X coordinates
Double Y1;
Double Y2; // Y1, Y2 is used to record the two endpoints of a line segment of the parallel Y axis,
Int flag; // indicates the left side or the right side.
} L [N];

// My code:

# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
# Define L (t) T <1
# Define r (t) T <1 | 1

Using namespace STD;

Const int n= 210;

Struct node {
Int L, R, C;
Double sum, Len;
} Node [n <2];

Struct line {
Double X;
Double Y1;
Double Y2;
Int flag;
} L [N];

Double Y [N];

Bool CMP (line A, line B ){
Return A. x <B. X;
}

Void creat (int t, int L, int R ){
Node [T]. L = L;
Node [T]. r = R;
Node [T]. c = 0;
Node [T]. Sum = 0;
Node [T]. Len = Y [R]-y [l];
If (L + 1 = r) return;
Int mid = (L + r)> 1;
Creat (L (t), L, mid );
Creat (r (t), mid, R );
}

Void updata_sum (INT t ){
If (node [T]. c> 0) node [T]. Sum = node [T]. Len;
Else if (node [T]. R! = Node [T]. L + 1) node [T]. Sum = node [L (t)]. Sum + node [r (t)]. sum;
Else node [T]. Sum = 0;
}

Void updata (int t, int L, int R, int c ){
If (node [T]. L = L & node [T]. r = r ){
If (c) node [T]. c ++;
Else node [T]. c --;
Updata_sum (t );
Return;
}
Int mid = (node [T]. L + node [T]. R)> 1;
If (L> = mid) updata (r (t), L, R, C );
Else if (r <= mid) updata (L (t), L, R, C );
Else {
Updata (L (t), L, mid, C );
Updata (r (t), mid, R, C );
}
Updata_sum (t );
}

Int find (double key, int N ){
Int L = 1, R = N, mid;
While (L <= r ){
Mid = (L + r)> 1;
If (Y [Mid] = Key) return mid;
Else if (Y [Mid]> key) r = mid-1;
Else l = Mid + 1;
}
Return 0;
}

Int main (){
// Freopen ("data. In", "r", stdin );

Int t, n, m, I, CAS = 0;
Double x1, x2, Y1, Y2, ans;
While (scanf ("% d", & T), t ){
M = 1;
While (t --){
Scanf ("% lf", & X1, & Y1, & X2, & Y2 );
Y [m] = Y1; L [M]. X = x1; L [M]. y1 = Y1; L [M]. y2 = Y2; L [M]. flag = 1; m ++;
Y [m] = Y2; L [M]. X = x2; L [M]. y1 = Y1; L [M]. y2 = Y2; L [M]. flag = 0; m ++;
}
Sort (Y + 1, Y + M );
Sort (L + 1, L + M, CMP );
M --; n = 2;
For (I = 1; I <m; I ++)
If (Y [I]! = Y [I + 1]) y [n ++] = Y [I + 1];
N --; creat (1, 1, n );
For (ANS = 0, I = 1; I <m; I ++ ){
If (L [I]. Flag) updata (1, find (L [I]. Y1, n), find (L [I]. Y2, n), 1 );
Else updata (1, find (L [I]. Y1, n), find (L [I]. Y2, n), 0 );
Ans + = node [1]. Sum * (L [I + 1]. X-L [I]. X );
}
Printf ("Test Case # % d \ ntotal received area: %. 2lf \ n", ++ cas, 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.