Nanyang sci-tech Question 9: posters (discretization + line segment tree)

Source: Internet
Author: User
Tags integer numbers
Posters time limit: 1000 MS | memory limit: 65535 kb difficulty: 6
Description
The citizens of bytetown, AB, cocould not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. the city councel has finally decided to build an electoral wall for placing the posters and introduce the following rules:
• Every candidate can place exactly one poster on the wall.
• All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in bytetown ).
• The Wall is divided into segments and the width of each segment is one byte.
• Each poster must completely cover a contiguous Number of wall segments.

They have built a wall 10000000 bytes long (such that there is enough place for all candidates ). when the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. moreover, the candidates started placing their posters on wall segments already occupied by other posters. everyone in bytetown was curous whose posters will be visible (entirely or in part) on the last day before elections.
Your task is to find the number of visible posters when all the posters are placed given the information about posters 'size, their place and order of placement on the electoral wall.
 
Input
The first line of input contains a number C giving the number of cases that follow. the first line of data for a single case contains number 1 <=n <= 10000. the subsequent n lines describe the posters in the order in which they were placed. the I-th line among the n lines contains two integer numbers Li and RI which are the number of the wall segment occupied by the Left end and the right end of the I-th poster, respectively. we know that for each 1 <= I <= N, 1 <= LI <= RI <= 10000000. after the I-th poster is placed, it entirely covers all Wall segments numbered Li, Li + 1 ,..., ri.
Output
For each input data set print the number of visible posters after all the posters are placed.

The picture below has strates the case of the sample input.
Http://acm.pku.edu.cn/JudgeOnline/images/2528_1.jpg
Sample Input
151 42 68 103 47 10
Sample output
4
Source
Poj
Uploaded
Iphxer

 

  Discretization + line segment tree.

The topic of a line segment tree with high difficulty is processed using discretization. At Nanyang Institute of Technology (http://acm.nyist.net/JudgeOnline/problem.php? PID = 9) submitted successfully, but wa submitted on poj. I don't know why ...... I heard that there are some problems with the test data on poj. Do you still need to calculate the vertices on both sides of the endpoint ?? I don't know. If you know me, please tell me, thank you...

  Question:

Give you some posters in order. These posters may overlap with each other and you can find the number of posters that are not completely covered.

The two endpoints of the poster can contain a maximum of 10000 posters, but a maximum of 10000000.

  Ideas:

Because the interval is involved, we should first think of the Line Segment tree, but the width of the poster is too large. What should I do if I have a 1-poster? Sure MLE is too memory (how many new nodes are there ). So we need to useDiscretizationThe method may sound mysterious, but it is easy to understand. For example, I will give you two posters, [] and [], which are sorted by 1, 3, 6, and 10. They are discretization to 1, 2, 3, 4, that is, if the first interval is [1, 3] and the second interval is [2, 4], you can create a [] Line Segment tree instead of a [] Line Segment tree, saves space.

  Code:

1 # include <iostream> 2 # include <stdio. h> 3 # include <string. h> 4 # include <algorithm> 5 using namespace STD; 6 7 # define maxn 20000 8 bool tree [maxn * 4 + 1]; // whether the storage interval has poster 9 short hash [10000010]; // The destination position after discretization is 10 short X [maxn + 10]; // store the two poster endpoints 11 struct post {12 short L; 13 short R; 14} A [maxn + 10]; // The original array 15 int CNT; // record number of posters exposed outside 16 17 bool insert (int d, int L, int R, int L, int R) 18 {19 if (tree [d]) 20 return false; 21 if (L = L & R = r) {// locate the interval 22 tree [d] = true; 23 return true; 24} 25 26 int mid = (L + r)/2; 27 bool res; 28 If (mid> = r) {29 res = insert (d <1, l, mid, L, R); 30} 31 else if (mid <L) {32 res = insert (d <1 | 1, Mid + 1, R, l, r); 33} 34 else {35 bool F1 = insert (d <1, L, mid, L, mid ); 36 bool F2 = insert (d <1 | 1, Mid + 1, R, Mid + 1, R); 37 res = F1 | F2; 38} 39 if (tree [d <1] & tree [d <1 | 1]) 40 tree [d] = true; 41 return res; 42} 43 44 int main () 45 {46 int t; 47 scanf ("% d", & T); 48 While (t --) {49 memset (tree, 0, sizeof (tree); 50 memset (hash, 0, sizeof (hash); 51 memset (x, 0, sizeof (x); 52 int I, n, ncount = 0; 53 54 // enter 55 scanf ("% d", & N); 56 for (I = 0; I <n; I ++) {57 scanf ("% d", & A [I]. l, & A [I]. r); 58 X [ncount ++] = A [I]. l; 59 X [ncount ++] = A [I]. r; 60} 61 62 sort (x, x + ncount); 63 ncount = unique (x, x + ncount)-X; // deduplicated 64 65 int key = 1; 66 for (I = 0; I <ncount; I ++) {// discretization processing 67 hash [x [I] = key; 68 if (I <nCount-1) {69 If (X [I + 1]-X [I] = 1) 70 key ++; 71 else 72 key = Key + 2; 73} 74} 75 76 CNT = 0; 77 for (I = n-1; I> = 0; I --) {78 If (insert (, key, hash [A [I]. l], hash [A [I]. r]) // Insert the poster after discretization into the line segment tree 79 CNT ++; 80} 81 printf ("% d \ n", CNT ); 82} 83 return 0; 84}

 

Freecode: www.cnblogs.com/yym2013

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.