Question link: http://poj.org/problem? Id = 2528
Type: Line Segment tree + discretization.
Author: kuangbin.
For more information, see writed by kuangbin (blog www.cnblogs.com/kuangbin)
I had a hard time on this topic for a day, mainly discretization, and I was not very skilled at just getting in touch with the line tree.
Now I understand discretization.
The general idea Of discretization: for example, to give you a set of data 1 4 1000 100000, If you directly
Opening a line segment is obviously a waste, so we only need to map it:
1 1
4 2
1000 3
100000 4
Next, we only need to create a line segment tree for 1 2 3 4.
Interval of [1, 4]
Discretization is equivalent to first ing and then building.
Given some posters may overlap with each other, tell you the width (height) and order of each poster, and ask how many posters are not completely covered?
A maximum of 10000 posters can be created, but the wall contains 10000000 tiles. The posters will not fall in the middle of the tiles.
If you build a role directly, even if you don't use TLE, MLE will be used. That is, the unit interval is too long.
In fact, there are 10000 million posters with a maximum of 20000 points. Number of each interval, that is, discretization. Then create the number.
In fact, floating point numbers are also discretization.
There are a lot of things to be aware. In this case, the line segment tree is four times larger than the ordinary three times.
Details determine success or failure:
Program:
/*
HDU 2528 mayor's posters
Some posters may overlap with each other to tell you each poster.
The width (with the same height) and the order of overlapping. How many sheets are not completely covered?
A maximum of 10000 posters can be created, but the wall contains 10000000 tiles. The posters will not fall in the middle of the tiles.
If you build a role directly, even if you don't use TLE, MLE will be used. That is, the unit interval is too long.
In fact, there are 10000 million posters with a maximum of 20000 points. Number of each interval, that is, discretization. Then create the number.
In fact, floating point numbers are also discretization.
Writer: kuangbin
*/
# Include < Stdio. h >
# Include < Algorithm >
# Include < Math. h >
Using Namespace STD;
Const Int Maxn = 10010 ;
Struct Cpost
{
Int L, R;
} Posters [maxn];
Int X [maxn * 2 ];
Int Hash [ 10000005 ];
Struct Node
{
Int L, R;
Bool Bcovered; // Indicates whether it is completely overwritten.
} Segtree [maxn * 8 ]; // Here we must open four times the number of line segments ,??
Void Build ( Int I, Int L, Int R) // Create a line segment tree
{
Segtree [I]. L = L;
Segtree [I]. r = R;
Segtree [I]. bcovered = False ;
If (L = R) Return ;
Int Mid = (L + R) > 1 ;
Build (I < 1 , L, mid );
Build (I < 1 | 1 , Mid + 1 , R );
}
Bool Post ( Int I, Int L, Int R) // Post a good report and determine whether it is completely covered
{
If (Segtree [I]. bcovered) Return False ;
If (Segtree [I]. L = L && Segtree [I]. r = R)
{
Segtree [I]. bcovered = True ;
Return True ;
}
Bool Bresult;
Int Mid = (Segtree [I]. L + Segtree [I]. R) > 1 ;
If (R <= Mid) bresult = Post (I < 1 , L, R );
Else If (L > Mid)
Bresult = Post (I < 1 | 1 , L, R );
Else
{
Bool B1 = Post (I < 1 , L, mid );
Bool B2 = Post (I < 1 | 1 , Mid + 1 , R );
Bresult = B1 | B2; // It cannot be directly or up, because if it is true, it will not be done later.
}
// It is very important to report back to the original node. If both the left and right sons are completely covered, they will naturally be completely covered.
If (Segtree [I < 1 ]. Bcovered && Segtree [I < 1 | 1 ]. Bcovered)
Segtree [I]. bcovered = True ;
Return Bresult;
}
Int Main ()
{
Int T;
Int I, J, K;
Int N;
Scanf ( " % D " , & T );
While (T -- )
{
Scanf ( " % D " , & N );
Int Ncount = 0 ;
For (I = 0 ; I < N; I ++ )
{
Scanf ( " % D " , & Posters [I]. l, & Posters [I]. R );
X [ncount ++ ] = Posters [I]. L;
X [ncount ++ ] = Posters [I]. R;
}
Sort (x, x + Ncount ); // Sort first
Ncount = Unique (x, x + Ncount) - X; // Merge the same items
For (I = 0 ; I < Ncount; I ++ )
Hash [x [I] = I;
Build ( 1 , 0 , Ncount - 1 );
Int Res = 0 ;
For (I = N - 1 ; I > = 0 ; I -- ) // Start from above.
If (Post ( 1 , Hash [posters [I]. L], hash [posters [I]. R])
Res ++ ;
Printf ( " % D \ n " , Res );
}
Return 0 ;
}
After thinking for a day, I finally solved this problem and got a deeper understanding of the Line Segment tree.
It also understands the nature of discretization. Win and chase, and then make several line tree lines.
by 2011-8-15