POJ2528-Mayor's posters

Source: Internet
Author: User

 

Reprinted please indicate the source: Thank you http://blog.csdn.net/lyy289065406/article/details/6799170

 

General question:

There is a wall, which is divided into 1 QW portions. The width of one portion is a unit width. Now there are n posters on the wall. The width of each poster is arbitrary, but it must be an integer multiple of the unit width and <= 1qw. If there is an intersection between the post poster and the post poster, the post poster will cover all or part of the Post poster. Now, I want to show the position (left and right) of each poster. How many posters can I see after I have posted n posters? (PS: You can see a part of it .)

 

Solution:

Water question, range compression ing (discretization) + line segment tree

 

 

First, create a model:

Given a number axis with a length of 1 QW, and then dyed in some intervals on the number axis, the I-th pair of intervals is I, and N times in total. The interval of each dyeing is given, and the number of colors that can be viewed at the end is displayed.

 

General idea:

If the I-th is colored in the interval [ai, BI], each cell of [ai, BI] is colored as I. The color after Dyeing overwrites the color of the first dyeing. Due to the dyeing n times, define a tag array tagcol. check from the first grid of the number axis until the last check. If the color is used, record it to tagcol, finally, count the numbers of different colors in tagcol.

The data size is too large, so it must be TLE.

 

We should use the line segment tree to solve the problem. This is just an entry-level question for the line segment tree. If you do not know the line segment tree, you can find some relevant materials and take a look at the comments in my code, there is a way to solve this problem. I recommend that you go to the PPT of the "ACM campus team of Zhejiang University" about the line segment tree. There are also examples of the Line Segment tree from entry to mastery, which also involve introduction of discretization, baidu search is available.

Then I would like to add some knowledge about the line tree here. There is a lot of misleading information about the line tree on the Internet.

 

1. The line segment tree is a binary tree and must be a balanced binary tree, but not necessarily a Complete Binary Tree.

2. If the range [a, B] is set to mid = (a + B)/2, the left subtree is [a, mid]. the right subtree is [Mid + 1, B]. When a = B, the interval is the leaf of the Line Segment tree, and no further division is required.

3. Although the line segment tree is not a complete binary tree, it can be constructed and stored in the form of a Complete Binary Tree, but the last layer may contain "Empty leaves" between some leaves ", ignore this. Similarly, empty leaves are numbered sequentially. When we determine a = B when traversing a line segment tree, we think that the leaves will never be traversed.

4. The purpose of using a Complete Binary Tree to store a line segment tree is to improve the efficiency of inserting line segments and searching. Using the p * 2, p * 2 + 1 Index to retrieve the left and right subtree of P is much faster than the pointer.

5. The essence of a line segment tree is that you do not need to search any longer. Use the root information of the subtree as much as possible to obtain the information of the entire tree. If you have to search for leaves each time when inserting a line segment or retrieving feature values, it is easier to directly create a common tree.

 

However, this question is not ac because the root system of a [1, 1 QW] Line Segment tree is very large, tle and MLE are fixed. Therefore, discretization is required.

In layman's terms, discretization is the compression interval, which maps the original long interval to the new short interval, but the coverage relationship before and after the compression remains unchanged. For example:

There is a number axis from 1 to 10 (the length is 9). Given four intervals [2, 4] [3, 6] [8, 10] [6, 9], the overwrite relationship means that the latter overwrites the former, the color of each interval is 1 2 3 4 in sequence.

Now we extract the eight endpoints of the four intervals, 2 4 3 6 8 6 9

Delete the same endpoint. If the same endpoint is 6, the remaining two are 4 3 6 8 10 9.

Sort them in ascending order. The values are 2, 3, 4, 6, 8, 9, and 10.

Then create a ing

2 3 4 6 8 9 10

When there are too many other users

1 2 3 4 5 6 7

The new four intervals are [1, 3] [2, 4] [5, 7] [4, 6], and the overwrite relationship is not changed. The new number axis is 1 to 7, that is, the length of the original number axis is compressed from 9 to 6. Obviously, the line segment tree of [] is more space-saving than the line segment tree, the search speed is faster, but the results are consistent.

 

Note that the same endpoint must be removed before sorting. This reduces the number of elements involved in sorting and saves time.

 

Attachment: the maximum number of posters is 10000, that is, the maximum length of the new number axis mapped by its endpoint is 20000. Therefore, when creating a discrete array DIS with a length of 1 QW, the unsigned short type can be used, and its ing value can be up to 20000, which saves space overhead.

 

Source correction

Alberta Collegiate Programming Contest 2003.10.18

Http://ugweb.cs.ualberta.ca /~ ACPC/2003/

Problem g

 

 

// Memory time // 21276 K 547 MS # include <iostream> # include <algorithm> using namespace STD; Class linetree_node {public: int S, E; // interval endpoint int Col; // interval color linetree_node (): S (0), E (0), COL (0) {}}; class solve {public: solve (int n): n (n) {initial (); input (); creatlinetree (1, maxp, 1); solution ();}~ Solve () {for (INT I = 1; I <= N; I ++) Delete [] Reg [I]; Delete [] EP; Delete [] DIS; delete [] tagcol; Delete [] lt;} void initial (void); // initialize and apply for the bucket void input (void); // enter void creatlinetree (INT sp, int TP, int P); // construct the [Sp, TP] Line Segment tree void solution (void); // insert range, statistical color void insert (int A, int B, int P, int color); // [a, B]: insert the interval [a, B] into the line segment tree // P: the position of the current line segment tree // color: dyeing void DFS (int p) in the current interval; // traverses the line segment tree and calculates the number of different colors in the line segment tree. Protected: int N; // number of posters (number of intervals) int maxp ;// The maximum endpoint of the record (Compressed), used to build the linetree_node of the Line Segment tree in the range [1, maxp] * lt; // line segment tree int ** reg; // store input intervals sequentially (the width of each poster) int * EP; // store two endpoints unsigned short * Dis for each interval sequentially; // map the endpoints, compression interval (discretization) bool * tagcol; // mark the int CNT color that can be seen; // counter, record the number of different colors that can be seen in the line segment tree}; void solve :: initial (void) {CNT = 0; Reg = new int * [n + 1]; for (INT I = 1; I <= N; I ++) reg [I] = new int [2]; Ep = new int [2 * n + 1]; Dis = new unsigned short [1e7 + 1]; memset (DIS, 0, sizeof (unsigned short) * (1e7 + 1); tagcol = new bool [n + 1]; memset (tagcol, false, sizeof (bool) * (n + 1); return;} void solve: input (void) {int p = 0; for (INT I = 1; I <= N; I ++) {scanf ("% d", & reg [I] [0], & reg [I] [1]);/* avoid duplicate mappings between interconnected endpoints to different values * // * to reduce the number of elements involved in sorting, mark * // * to save space, the DIS [] Array Used for discretization is temporarily used to mark the endpoint */If (DIS [Reg [I] [0] = 0) {EP [p ++] = reg [I] [0]; DIS [Reg [I] [0] = 1 ;} if (DIS [Reg [I] [1] = 0) {EP [p ++] = reg [I] [1]; dis [Reg [I] [1] = 1 ;}/ * discretization */sort (Ep, EP + p); // unsigned short hash = 0 in the interval endpoint sorting; fo R (Int J = 0; j <p; j ++) dis [EP [J] = ++ hash; // map the ordered endpoints to 1, 2 ,..., maxpmaxp = hash; Lt = new linetree_node [4 * maxp + 1]; return;} void solve: creatlinetree (INT sp, int TP, int p) {lt [p]. S = sp; lt [p]. E = TP; If (sp = TP) return;/* Note that the line segment tree is not necessarily a Complete Binary Tree * // * but for processing convenience, accelerate search efficiency * // * We can construct and store a line segment tree in the form of a complete binary tree */INT mid = (SP + TP)> 1; creatlinetree (SP, mid, p * 2); creatlinetree (Mid + 1, TP, p * 2 + 1); return;} void solve: solution (void) {for (INT I = 1; I <= N; I ++) inse RT (DIS [Reg [I] [0], DIS [Reg [I] [1], 1, I); // interval by interval (poster) dye DFS (1); printf ("% d \ n", CNT); return;} void solve: insert (int A, int B, int P, int color) {If (B <lt [p]. S | A> lt [p]. e) // [a, B] and [s, E] have no intersection return; // description [a, B] is not [s, e] the subtree contains, and you do not need to search down if (a <= lt [p]. S & B> = lt [p]. e) // [a, B] completely covers [s, E] {lt [p]. col = color; // [s, E] the sub-tree operation of [s, E] is not required for the moment (this is determined by the search mechanism of the line tree) return; // Therefore, directly return}/* If the code can be executed here, [a, B] Partially overwrites [s, E] */If (LT [p]. col> = 0) // [s, E] is colorless or monochrome {// due I do not know the number of [s, E] covered by [a, B]. Lt [p * 2]. col = lt [p * 2 + 1]. col = lt [p]. col; // Therefore, the child of [s, E] First inherits the monochrome of [s, E] lt [p]. col =-1; // [s, E] because it is partially overwritten, the color is colored.}/* if it can be executed here, it indicates [s, e] is the child of the multi-color * // * refined processing [s, E]. determine the color of which part of [s, E] is located */insert (A, B, p * 2, color); insert (A, B, P * 2 + 1, color); return;} void solve: DFS (INT P) {If (LT [p]. col = 0) // [s, E] is colorless, and the child must also be colorless. You do not need to search for return; If (LT [p]. col> 0) // [s, E] is a monochrome, you do not need to search down {// because its subintervals must be overwritten by [s, E], only [s, e] color if (! Tagcol [lt [p]. col]) {tagcol [lt [p]. col] = true; CNT ++;} return;} If (LT [p]. col =-1) // [s, E] Is multicolor, which indicates that it can be found in [s, e] see the color of the set {// search for the specific color of the sub-range DFS (p * 2); DFS (p * 2 + 1);} return;} int main (void) {int test; scanf ("% d", & Test); For (int t = 1; t <= test; t ++) {int N; scanf ("% d", & N); solve poj2528 (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.