POJ 2528 Mayor ' s posters

Source: Internet
Author: User
Tags integer numbers

Description

The citizens of Bytetown, AB, could not stand then the candidates in the mayoral election campaign has been placing their Electoral posters at all places at their whim. The city council have finally decided to build a electoral wall for placing the posters and introduce the following rules:
    • Every candidate can place exactly one poster on the wall.
    • All posters is of the same height equal to the height of the wall; The width of a poster can be any integer number of bytes (byte was the unit of length in Bytetown).
    • The wall is divided to segments and the width of each segment is one byte.
    • Each poster must completely cover a contiguous number of wall segments.

They has built a wall 10000000 bytes long (such that there are enough place for all candidates). When the electoral campaign is restarted, the candidates were placing their posters on the wall and their posters differe D widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown is curious whose posters would 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 was placed given the information about posters ' Si Ze, 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 the integer numbers Li and ri which is the number of the wall segment occupied b Y 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 are 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 is placed.

The picture below illustrates the case of the sample input.

Sample Input

151 42 68 103 47 10

Sample Output

4

Test Instructions: There is a wall, is divided into 1QW parts, a portion of the width of a unit width. Now put n Zhang Hai on the wall, the width of each poster is arbitrary, but must be an integer multiple of the width of the unit, and <=1QW. Post poster if the poster with the first affixed to the intersection, after the poster will be affixed to all or part of the poster first affixed. Now give the position of each poster (the left and right position), asked to post the N Zhang Hai report, how many Zhang Hai can you see? (PS: See part of See also.) )

The segment tree (coverage problem) and discretization are examined:

discretization of processing :

The popular point says that discretization is the compression interval, so that the original long interval is mapped to a new short interval, but the coverage relationship is not changed before and after the interval compression. As an example:

There is a 1 to 10 axis (length 9), given a 4 interval [2,4] [3,6] [8,10] [6,9], the covering relationship is the latter covering the former, each interval is dyed 1 2 3 4.

Now we take the 8 endpoints of these 4 intervals, 2 4 3 6 8 10 6 9

Then delete the same endpoint, where the same endpoint is 6, leaving 2 4 3 6 8 10 9

Sort it in ascending order, get 2 3 4 6 8 9 10

And then build the mapping

2 3 4 6 8 9 10

↓↓↓↓↓↓↓

1 2 3 4 5 6 7

The new 4 range is [1,3] [2,4] [5,7] [4,6], and the coverage relationship has not been changed. The new axis is 1 to 7, that is, the length of the original axis is compressed from 9 to 6, it is obvious that the construction [1,7] segment tree is more space-saving and faster to search than the segment tree that constructs [1,10], but the results of the solution are consistent.

Attached: The maximum number of poster count is 10000, that is, the new axis length of its endpoint mapping is up to 20000. Therefore, when a discrete array of length 1QW is established, the mapping value is up to 20000, which saves space overhead.

#include <stdio.h>#include<algorithm>#defineN 10010using namespacestd;structnode{intLeft, right, covered;//covered indicates whether the interval is overwritten}no[8*N];structpoint{intL, R;} Po[n];inthash[10000010], a[2*n];//A array holds the value of the endpoint before the discretization process, and the hash array holds the endpoint after the discretization processvoidBulid (intLeftintRightintroot) {    intmid; No[root].left=Left ; No[root].right=Right ; No[root].covered=0; if(left = right)return ; Mid= (left+right)/2; Bulid (left, Mid, Root*2); Bulid (Mid+1, right, root*2+1);}intQuery (intLeftintRightintroot) {    intMid, A1, A2; if(no[root].covered)return 0; if(No[root].left = = Left && No[root].right = =Right ) {no[root].covered=1; return 1; } //If the interval is found and is not overwritten, the interval can be posted on a poster, the covered assignment is 1, the next time no poster can be postedMid = (no[root].left+no[root].right)/2; intAns//cannot return a value directly    if(Right <= mid) ans = Query (left, right, root*2); Else if(Left > Mid) ans = Query (left, right, root*2+1); Else{A1= Query (left, Mid, root*2); A2= Query (mid+1, right, root*2+1); Ans= A1 |A2; }    if(no[root*2].covered && no[root*2+1].covered) no[root].covered=1; returnAns//the entire range needs to be updated whether it is overwritten}intMain () {intT, N, I, K, ans; scanf ("%d", &T);  while(t--) {k=1; Ans=0; scanf ("%d", &N);  for(i =1; I <= N; i++) {scanf ("%d%d", &po[i]. L, &Po[i].            R); A[k++] =Po[i].            L A[k++] =Po[i].        R } k--; Sort (A+1, a+k+1);//sort (small to large)K = Unique (A +1, a+k+1)-(A +1);//Go heavy         for(i =1; I <= K; i++) Hash[a[i]]= i;//discretization of processingBulid (1K1);  for(i = n; I >=1; i--)        {            if(Query (hash[po[i). L], Hash[po[i]. R],1)) Ans++; } //From the back to the next to determine whether each interval can also be affixed to the newspaper, because if you can post from the trip, no matter how to paste, cannot determine whether this interval is coveredprintf"%d\n", ans); }    return 0;}

POJ 2528 Mayor ' s posters

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.