POJ 2528 Mayor ' s posters interval discrete segment tree

Source: Internet
Author: User
Tags integer numbers

Click to open link
Mayor ' s Posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 45894 Accepted: 13290

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

Source

Alberta Collegiate Programming Contest 2003.10.18

give you n a pictorial to be affixed to the wall, the back of the post will be covered in the front affixed to the last to see how many pictorial. the maximum number of topics given is 10000000, so we need to disperse the interval and then solve the segment tree.
35616k94ms#include<stdio.h> #include <string.h> #include <algorithm> #define M 10007using    namespace Std;int id[m*2],_hash[10000007];int n,num;struct t{int left,right,mid,color; T () {color=0;}} tree[m<<4];struct p{int l,r;}    p[m*8];void Build (int l,int r,int i) {tree[i].left=l;tree[i].right=r;    Tree[i].mid= (l+r) >>1;tree[i].color=0;    if (l==r) return;    Build (l,tree[i].mid,i<<1); Build (tree[i].mid+1,r,i<<1|1);}    BOOL Query (int l,int r,int i) {bool flag;    if (Tree[i].color) return false;        if (tree[i].left==l&&tree[i].right==r) {tree[i].color=true;    return true;    } if (R<=tree[i].mid) flag=query (l,r,i<<1);    else if (l>tree[i].mid) flag=query (l,r,i<<1|1);        else {bool Flag1=query (l,tree[i].mid,i<<1);        BOOL Flag2=query (TREE[I].MID+1,R,I&LT;&LT;1|1); flag=flag1| |    Flag2; } if (Tree[i<<1].color&&tree[i<<1|1].color)//Feedback Original node tree[i].coLor=true; return flag;}    void Lisanhua () {sort (id,id+num);    Num=unique (id,id+num)-id; for (int i=0;i<num;i++) _hash[id[i]]=i;}    int main () {int t;    scanf ("%d", &t);        while (t--) {num=0;        scanf ("%d", &n);            for (int i=1;i<=n;i++) {scanf ("%d%d", &AMP;P[I].L,&AMP;P[I].R);            ID[NUM++]=P[I].L;        ID[NUM++]=P[I].R;        } Lisanhua ();        Build (0,num-1,1);        int count=0;        for (int i=n;i>=1;i--)//from the last pasted forward looking for {if (query (_hash[p[i].l],_hash[p[i].r],1)) count++;    } printf ("%d\n", count); } return 0;}


POJ 2528 Mayor ' s posters interval discrete 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.