Mayor ' s Posters

Source: Internet
Author: User
Tags integer numbers

Mayor ' s posters time limit:1000MS Memory Limit:65536KB 64bit IO Format: %I64D &%i64u

Submit Status Practice POJ 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
Because the data is too large, and the intermediate data is mostly useless, we just need to judge the process of post-pasting, people can see a few different posters (each is different, that is, to see whether the poster is completely covered, of course, in the process of poster, only behind can cover the front,
So we judge from the trip, whether the current poster is covered, if not, certainly will not be more than the poster he posted earlier cover, is can see, add one.
Because the data is too big, too much, too much useless,so we use line segment tree + discretization for processing
#include <iostream>#include<cstdio>#include<algorithm>using namespacestd;#defineN 210000//due to too many datastructnode{intL, R;} Val[n];structpoint{intL, R, Flag;} Tree[n*4];intx[n*2], h[n* +]; Why the H array exists so much because 1 <= i <= N, 1 <= l i <= ri <= 10000000.voidBuild (intRootintLintr)//achievement {TREE[ROOT].L=l; TREE[ROOT].R=R; Tree[root].flag=false; if(L = =R)return ; Build (Root*2, L, (l+r)/2); Build (Root*2+1, (L+R)/2+1, R);}voidUpdateintroot)//update {if(tree[root*2].flag && tree[root*2+1].flag) Tree[root].flag=1;}intQueryintRootintLintr)//See if you can see the return 1;else return0;{if(Tree[root].flag)//if obscured, return0;return 0; if(TREE[ROOT].L = =TREE[ROOT].R)//If the node is not obscured, and to the leaf node, return 1, this node flag is 1, that is obscured {tree[root].flag=1; return 1;  } update (root); Update the root nodeintMid = (TREE[ROOT].L+TREE[ROOT].R)/2; if(R <=mid)returnQuery2*Root, L, R); Else if(L >mid)returnQuery2*root+1, L, R); Else        returnQuery2*root, L, mid) +query (2*root+1, mid+1, R);}intMain () {intc, N; scanf ("%d", &c);  while(c--) {scanf ("%d", &N); intK =0;  for(inti =1; I <= N; i++) {scanf ("%d%d", &AMP;VAL[I].L, &VAL[I].R); X[k+ +] = val[i].l, x[k++] =VAL[I].R; x array where each poster is stored} sort (x, x+k); K= Unique (x, x+k)-x; Go to Heavy, sort Build (1,0, K-1); Achievements, a total of k-1 points, there are k-1 nodes for(inti =0; I < K; i++) H[x[i]]=i; The values of the various nodes are discretized, that is, the middle of some have not all, only left the poster left and right two endpoints of the value of the H array, as the l,r value of the nodeintAns =0;  for(inti = n; i >0; i--)            if(Query (1, H[VAL[I].L], H[VAL[I].R])) Query ans++; printf ("%d\n", ans); }    return 0;}

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.