SGU 319 Kalevich Strikes Back (line segment tree + scanning line)

Source: Internet
Author: User

I am just getting started with the line tree.
By scanning the line, the line segment tree records the coverage of the interval, then it can determine the inclusion relationship of the rectangle, and a relational tree can be established through the relationship.
Then traverse the tree.
It will not be recorded at the beginning, if the inserted edge weight is negative, that is, the top of the rectangle. Which edge should be covered in this way... 233333
We thought we needed to record the historical coverage information of the entire range. In fact, we only needed to overwrite the parent node through the previous relationship.
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cmath>
# Include <algorithm>
# Include <cstring>
# Include <vector>
# Define LL long
# Define lson step <1
# Define rson step <1 | 1
Using namespace std;
Const int n= 60005;
Struct Frames {
Int x1, y1, x2, y2;
Frames (){}
Frames (int _ x1, int _ y1, int _ x2, int _ y2 ){
X1 = _ x1, y1 = _ y1, x2 = _ x2, y2 = _ y2;
}
} Frames [N];
Struct Scanning_line {
Int y, x1, x2, id;
Scanning_line (){}
Scanning_line (int _ y, int _ x1, int _ x2, int _ I ){
Y = _ y, x1 = _ x1, x2 = _ x2, id = _ I;
}
Bool operator <(const Scanning_line s) const {
Return y <s. y;
}
} Line [N <1];
Struct Segment_tree {
Int left, right;
Int cover;
} L [N <3];
Int n, w, h, x [N <1], tot = 0;
Vector <int> edge [N];
Int pre [N];
Void bulid (int step, int l, int r ){
L [step]. left = l;
L [step]. right = r;
L [step]. cover = 0;
If (l = r) return;
Int m = (l + r)> 1;
Bulid (lson, l, m );
Bulid (rson, m + 1, r );
}
Void push_down (int step ){
If (L [step]. cover! =-1 ){
L [lson]. cover = L [rson]. cover = L [step]. cover;
L [step]. cover =-1;
}
}
Int query (int step, int l, int r ){
If (L [step]. cover! =-1)
Return L [step]. cover;
Int m = (L [step]. left + L [step]. right)> 1;
Push_down (step );
If (r <= m) return query (lson, l, r );
Else if (l> m) return query (rson, l, r );
Else return query (lson, l, m );
}
Void update (int step, int l, int r, int id ){
If (L [step]. left = l & L [step]. right = r ){
If (id <0 ){
L [step]. cover = pre [abs (id)];
}
Else {
L [step]. cover = id;
}
Return;
}
Push_down (step );
Int m = (L [step]. left + L [step]. right)> 1;
If (r <= m) update (lson, l, r, id );
Else if (l> m) update (rson, l, r, id );
Else {
Update (lson, l, m, id );
Update (rson, m + 1, r, id );
}
}
LL area [N];
Void dfs (int u ){
For (int I = 0; I <edge [u]. size (); I ++ ){
Int v = edge [u] [I];
Area [u]-= area [v];
Dfs (v );
}
}
Int main (){
Scanf ("% d", & n, & w, & h );
Frames [0] = Frames (0, 0, w, h );
Area [0] = (LL) w * h;
For (int I = 0; I <n; I ++ ){
Int x1, x2, y1, y2;
Scanf ("% d", & x1, & y1, & x2, & y2 );
If (x1> x2) swap (x1, x2 );
If (y1> y2) swap (y1, y2 );
Line [I * 2] = Scanning_line (y1, x1, x2, I + 1 );
Line [I * 2 + 1] = Scanning_line (y2, x1, x2,-(I + 1 ));
X [tot ++] = x1;
X [tot ++] = x2;
Frames [I + 1] = Frames (x1, y1, x2, y2 );
Area [I + 1] = (LL) (x2-x1) * (y2-y1 );
}
X [tot ++] = 0; x [tot ++] = w;
Sort (x, x + tot );
Tot = unique (x, x + tot)-x;
Bulid (1, 0, tot-1 );
Sort (line, line + 2 * n );
For (int I = 0; I <2 * n; I ++ ){
Line [I]. x1 = lower_bound (x, x + tot, line [I]. x1)-x;
Line [I]. x2 = lower_bound (x, x + tot, line [I]. x2)-x;
If (line [I]. id> 0 ){
Pre [line [I]. id] = query (1, line [I]. x1, line [I]. x2 );
Edge [pre [line [I]. id]. push_back (line [I]. id );
}
Update (1, line [I]. x1, line [I]. x2, line [I]. id );
}
Dfs (0 );
Sort (area, area + n + 1 );
For (int I = 0; I <= n; I ++)
Printf ("% lld % c", area [I], I = n? '\ N ':'');
Return 0;
}

# Include <iostream>
# Include <cstdio>
# Include <cmath>
# Include <algorithm>
# Include <cstring>
# Include <vector>
# Define LL long
# Define lson step <1
# Define rson step <1 | 1
Using namespace std;
Const int n= 60005;
Struct Frames {
Int x1, y1, x2, y2;
Frames (){}
Frames (int _ x1, int _ y1, int _ x2, int _ y2 ){
X1 = _ x1, y1 = _ y1, x2 = _ x2, y2 = _ y2;
}
} Frames [N];
Struct Scanning_line {
Int y, x1, x2, id;
Scanning_line (){}
Scanning_line (int _ y, int _ x1, int _ x2, int _ I ){
Y = _ y, x1 = _ x1, x2 = _ x2, id = _ I;
}
Bool operator <(const Scanning_line s) const {
Return y <s. y;
}
} Line [N <1];
Struct Segment_tree {
Int left, right;
Int cover;
} L [N <3];
Int n, w, h, x [N <1], tot = 0;
Vector <int> edge [N];
Int pre [N];
Void bulid (int step, int l, int r ){
L [step]. left = l;
L [step]. right = r;
L [step]. cover = 0;
If (l = r) return;
Int m = (l + r)> 1;
Bulid (lson, l, m );
Bulid (rson, m + 1, r );
}
Void push_down (int step ){
If (L [step]. cover! =-1 ){
L [lson]. cover = L [rson]. cover = L [step]. cover;
L [step]. cover =-1;
}
}
Int query (int step, int l, int r ){
If (L [step]. cover! =-1)
Return L [step]. cover;
Int m = (L [step]. left + L [step]. right)> 1;
Push_down (step );
If (r <= m) return query (lson, l, r );
Else if (l> m) return query (rson, l, r );
Else return query (lson, l, m );
}
Void update (int step, int l, int r, int id ){
If (L [step]. left = l & L [step]. right = r ){
If (id <0 ){
L [step]. cover = pre [abs (id)];
}
Else {
L [step]. cover = id;
}
Return;
}
Push_down (step );
Int m = (L [step]. left + L [step]. right)> 1;
If (r <= m) update (lson, l, r, id );
Else if (l> m) update (rson, l, r, id );
Else {
Update (lson, l, m, id );
Update (rson, m + 1, r, id );
}
}
LL area [N];
Void dfs (int u ){
For (int I = 0; I <edge [u]. size (); I ++ ){
Int v = edge [u] [I];
Area [u]-= area [v];
Dfs (v );
}
}
Int main (){
Scanf ("% d", & n, & w, & h );
Frames [0] = Frames (0, 0, w, h );
Area [0] = (LL) w * h;
For (int I = 0; I <n; I ++ ){
Int x1, x2, y1, y2;
Scanf ("% d", & x1, & y1, & x2, & y2 );
If (x1> x2) swap (x1, x2 );
If (y1> y2) swap (y1, y2 );
Line [I * 2] = Scanning_line (y1, x1, x2, I + 1 );
Line [I * 2 + 1] = Scanning_line (y2, x1, x2,-(I + 1 ));
X [tot ++] = x1;
X [tot ++] = x2;
Frames [I + 1] = Frames (x1, y1, x2, y2 );
Area [I + 1] = (LL) (x2-x1) * (y2-y1 );
}
X [tot ++] = 0; x [tot ++] = w;
Sort (x, x + tot );
Tot = unique (x, x + tot)-x;
Bulid (1, 0, tot-1 );
Sort (line, line + 2 * n );
For (int I = 0; I <2 * n; I ++ ){
Line [I]. x1 = lower_bound (x, x + tot, line [I]. x1)-x;
Line [I]. x2 = lower_bound (x, x + tot, line [I]. x2)-x;
If (line [I]. id> 0 ){
Pre [line [I]. id] = query (1, line [I]. x1, line [I]. x2 );
Edge [pre [line [I]. id]. push_back (line [I]. id );
}
Update (1, line [I]. x1, line [I]. x2, line [I]. id );
}
Dfs (0 );
Sort (area, area + n + 1 );
For (int I = 0; I <= n; I ++)
Printf ("% lld % c", area [I], I = n? '\ N ':'');
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.