POJ 2528 Mayor ' s posters

Source: Internet
Author: User
Tags integer numbers

Mayor ' s PostersTime limit:1000MS Memory Limit:65536KB 64bit IO Format:%i64d &%i64 U SubmitStatusPracticePOJ 2528

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 II integer numbers L 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 <= l i <= ri <= 10000000. After the i-th poster are placed, it entirely covers all wall segments numbered L I, l i+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

Main topic:
In general, people want to vote for elections or whatever, do not bother, in short, the post is posted posters, the back of the front of the poster can be covered, the poster will not be affixed to the middle of the square,
If a poster is not completely affixed even if you can see, ask at the end you can see a few posters (English slag, anyway I understand)

Input
1//t Sample 5//below 5 (n) Zhang Hai report and its range  1 <= n <= 100001 4//poster of the left and right border 1--100000002 68 103 47 10


4 can see 4 posters

Problem Solving Ideas:
Due to the way the poster is pasted, the outside is absolutely not covered by the inside, so it is more convenient to cover the poster backwards
Mainly because the poster boundary is too large to be discretized, if you think of each border of the poster as a node, the maximum required range becomes the 20000 node is 80000
But I will not be discretized the first time to read the courseware, imitate the writing, but listen to the seniors said seemingly not very good discretization or completely unqualified discretization
I use this discretization or to open a 100000000 one-dimensional array Although open, the problem can be solved, but seniors said if the range is hundreds of millions of, there is no way
But this problem can still be, the new discretization method tomorrow.

Here is the code
/*after all, I'm Wang Yuhao .*/#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<cstdlib>#include<cmath>#include<cctype>#defineMax (a) (a) > (b)? ( A):(B))#defineLS rt<<1#defineRS Rt<<1|1using namespacestd;Const intN =200010;intXx[n];//X[i] used to store the left and right side of the poster, arranged after the position of the node I;intPost[n * -];//Post[i] used to store the edge I should be arranged in the first position abovestructposter{intL, R;} P[n];//P[i] to store the left and right side of the Zhang Hai report.structnode{intL, R; BOOLcover; intMid () {return(L + R) >>1; }}a[n<<2];intcmpConst void*a,Const void*b) {    int*c, *D; C= (int*) A; D= (int*) b; return*c-*D;}voidBuild (intRtintLintR) {A[RT].L= L, A[RT].R = r, A[rt].cover =false; if(L = = r)return ; Build (LS, L, A[rt].    Mid ()); Build (RS, A[rt]. Mid ()+1, R);}voidUp (intRT) {    //if both LS and RS are overwritten, this area is overwritten    if(A[RT].L = = A[RT].R)return; if(A[ls].cover &&a[rs].cover) A[rt].cover=true;}BOOLSearch (intRtintLintR//If this interval is found to be overwritten, overwrite and return true, otherwise return false;{    BOOLans; if(A[rt].cover)return false; if(A[RT].L = = L && A[RT].R = =r) {if(A[rt].cover)return false; Else{a[rt].cover=true; return true; }    }    if(L >A[rt]. Mid ()) {ans=Search (RS, L, R); }    Else if(R <=A[rt]. Mid ()) {ans=Search (LS, L, R); }    Else {        BOOLANS1 =Search (LS, L, A[rt].        Mid ()); BOOLAns2 = Search (RS, A[rt]. Mid () +1, R); Ans= Ans1 | |Ans2; } up (RT);//update up every time you place a poster    returnans;}intMain () {intT, N; scanf ("%d", &T);  while(t--) {scanf ("%d", &N); intindex =0;  for(inti =0; I < n; i++) {scanf ("%d%d", &AMP;P[I].L, &P[I].R); Xx[index++] =P[I].L; Xx[index++] =P[I].R; } qsort (xx, index,sizeof(xx[0]), CMP);//sorts the size of each left and right side to determine the node location to which it belongs        intIndex2 =1;  for(inti =0; I < index; i++) {//the position of each edge is determined, the same value is a node, adjacent nodes are also adjacent, the non-adjacent nodes between the empty nodePost[xx[i]] =Index2; if(i = = index-1) Break;//judge the last one, end the loop, because each time the loop is saved is the state of the next node, in time to stop the array from crossing            if(Xx[i +1]-xx[i] = =1) index2++; if(Xx[i +1]-Xx[i] >1) Index2 + =2; } Build (1,1, INDEX2);//Create a line segment tree        intAns =0;//Save the answer         for(inti = n-1; I >=0; i--) {            if(Search (1, POST[P[I].L], POST[P[I].R])) ans++;//each time a poster, judge will not be covered, will not answer +1} printf ("%d\n", ans); }}/*151 103*/

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.