POJ 2528--mayor ' s posters —————— "segment tree interval substitution, finding different intervals of existence"

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

The main topic: in the publicity bar to paste the leaflet, the same width, the length is different. Paste in a certain order, give the starting and ending position of the flyer, where the position can not be ignored as a point (but a length), asked to see a number of leaflets at the end.

Problem-solving ideas: Due to the extent of the data given, so we have to discretization to reduce the complexity, because here is not the "point" is a band length, so the general discretization will appear discrete distortion, here can be in the discrete time to increase the skill, that is, in the non-adjacent data to increase the separation point, highlighting discontinuity. Then the segment is replaced by the segment tree, and finally the different propagating individual numbers of the whole interval are obtained.

#include <stdio.h> #include <string.h> #include <algorithm>using namespace std; #define MID (L+r)/2# Define Lson rt*2,l,mid#define rson rt*2+1,mid+1,rconst int maxn=50000;int lm[maxn/2],rm[maxn/2];int col[maxn*4];int Hash[maxn];int a[maxn];int num=0;int discretization (int l,int r,int key) {//discretization while (l<=r) {int m= (L+R)/2        ;        if (Key==a[m]) {return m;        }else if (Key<a[m]) {r=m-1;        }else{l=m+1;        }}}void pushdown (int rt) {if (col[rt]!=-1) {COL[RT*2]=COL[RT];        COL[RT*2+1]=COL[RT];    Col[rt]=-1; }}void Update (int rt,int l,int r,int l_ran,int r_ran,int _col) {if (L_ran<=l&&r<=r_ran) {Col[rt]=_c        Ol    return;    } pushdown (RT);    if (l_ran<=mid) update (LSON,L_RAN,R_RAN,_COL); if (r_ran>mid) update (RSON,L_RAN,R_RAN,_COL);} void query (int rt,int l,int R) {if (col[rt]!=-1) {if (!            HASH[COL[RT]]) {num++; Hash[coL[rt]]=1;    } return;    } if (l==r) return;    Query (Lson); Query (Rson);}    void Debug () {for (int i=1;i<32;i++) {printf ("%d%d\n", i,col[i]);    }}int Main () {int t;    scanf ("%d", &t);        while (t--) {int n,nn=0,m;        scanf ("%d", &n);            for (int i=0;i<n;i++) {scanf ("%d%d", &lm[i],&rm[i]);            A[nn++]=lm[i];        A[nn++]=rm[i];        } sort (A,A+NN);        M=1;            for (int i=0;i<nn-1;i++) {//de-weight if (a[i]!=a[i+1]) {a[m++]=a[i+1];            }} for (int i=m-1;i>0;i--) {//Add separator point if (a[i]!=a[i-1]+1) {a[m++]=a[i-1]+1;        }} sort (a,a+m);        int TML,TMR;        memset (col,-1,sizeof (col));    for (int i=0;i<n;i++) {tml= discretization (0,m-1,lm[i]);    Discretization of Tmr= discretization (0,m-1,rm[i]);        Discrete update (1,0,M-1,TML,TMR,I);    }//debug ();    memset (hash,0,sizeof (Hash));        num=0;        Query (1,0,M-1);    printf ("%d\n", num); } return 0;}

  

POJ 2528--mayor ' s posters —————— "segment tree interval substitution, finding different intervals of existence"

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.