Nsoj 4621 Posters (discretization + segment tree)

Source: Internet
Author: User
Tags integer numbers

Posters

Time limit: 1000ms memory limit: 128000kb64 bit integer: Java class Name: Previous question submit run result statistics Discussion Edition next topic 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.
Http://acm.pku.edu.cn/JudgeOnline/images/2528_1.jpg

Sample input
151 42 68 103 47 10
Sample output
4
SOURCE Nyoj

Test instructions

On a long wall to paste n Zhang Hai newspaper, the first Zhang Hai can be posted from Li to Ri, the poster is equal (can be understood as a one-dimensional model), according to the method given in order to paste, asked to finally see a few posters.

Analysis:

Starting point and end point range (1 <= i <= N, 1 <= li <= ri <= 10000000) However, we only used (1 <= n <= 10000), only used (1 /1000) * * Point, to save the entire state, open 10000000 of the shape of an array of decisive memory, the use of discrete ideas with 20000 of the shaping array can save the whole state.

If you follow the general method, you in a poster area corresponding to the value of the preservation state, the worst case of discretization is still 2*c*n2 decisive time-out, it will be used to optimize the line segment tree, segment tree used to two points of thought so the worst case is 2*c*nlogn,( The scope of C is not given, simply take into account, but C should not be too big).

1#include <iostream>2#include <algorithm>3#include <map>//discretization of Mappings4#include <Set>//automatic de-weight5 using namespacestd;6 #defineMAXN 100057Map <int,int>M;8 structnode{9     intLeft ;Ten     intRight ; One     intindex; ANode (): Left (0), Right (0), Index (0) {} -} TREE[MAXN *4], INPUT[MAXN]; -  the intPOINT[MAXN *2], sum; - Set<int>visible; -  - voidBuildtree (intRootintLeftintright) {//Create a line segment tree +Tree[root].left =Left ; -Tree[root].right =Right ; +Tree[root].index =-1; A     if(Left +1<Right ) { at         intMid = (left + right)/2; -Buildtree (Root *2, left, mid); -Buildtree (Root *2+1, Mid, right); -     } - } -  in voidUpdatetree (intKintAintBintindex) { -     if(Tree[k].left = = a && Tree[k].right = =b) { toTree[k].index =index; +         return; -     } the     if(Tree[k].index! =-1){ *tree[2* K].index = tree[2* k +1].index =Tree[k].index; $Tree[k].index =-1;Panax Notoginseng     } -     intMid = (Tree[k].left + tree[k].right)/2; the     if(b <=mid) +Updatetree (2*K, A, b, index); A     Else if(A >=mid) theUpdatetree (2* k +1, A, b, index); +     Else { -Updatetree (2* k, A, tree[2*k].right, index); $Updatetree (2* k +1, tree[2* k +1].left, b, index); $     } - } - voidSearchtree (intK) {//Calendar the     if(Tree[k].index! =-1) - Visible.insert (tree[k].index);Wuyi     Else if(Tree[k].right > Tree[k].left +1) { theSearchtree (2*k); -Searchtree (2* k +1); Wu     } - } About intMain () { $     intc, N; -CIN >>C; -      while(c--) { -CIN >>N; Asum =0; +          for(inti =0; I < n; i++) { theCIN >> Input[i].left >>Input[i].right; -input[i].left--; $point[sum++] =Input[i].left; thepoint[sum++] =Input[i].right; the         } theSort (point, point +sum); thesum = Unique (point, point + Sum) – point;//total sum of points after weight - m.clear (); in          for(intI=0; i<sum;i++){ theM[point[i]]=i;//Mapping the         } AboutBuildtree (1,0, Sum-1); the          for(inti =0; I < n; i++) { theUpdatetree (1, M[input[i].left], m[input[i].right], i); the         } + visible.clear (); -Searchtree (1); thecout << visible.size () <<Endl;Bayi     } the     return 0; the}

Nsoj 4621 Posters (discretization + segment tree)

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.