D-mayor ' s Posters (segment tree + discretization) POJ 2528

Source: Internet
Author: User
Tags integer numbers


D-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

1
5
1 4
2 6
8 10
3 4

7 10

Submit wa,g++ in C + + to AC

#include <bits/stdc++.h> #include <cstdio> #include <algorithm>using namespace Std;const int maxn= 20011;const int inf=999999999; #define Lson (rt<<1), L,m#define Rson (rt<<1|1), M+1,r#define M ((L+R) >>    1) #define for (i,n) for (int i=0;i< (n); i++) Template<class t>inline T Read (t&x) {char C;    while ((C=getchar ()) <=32);    BOOL Ok=false;    if (c== '-') Ok=true,c=getchar ();    for (x=0; c>32; C=getchar ()) x=x*10+c-' 0 ';    if (OK) x=-x; return x;}    Template<class t> inline void Read_ (t&x,t&y) {read (x); Read (y);}    Template<class t> inline void Write (T x) {if (x<0) Putchar ('-'), x=-x;    if (x<10) putchar (x+ ' 0 '); else write (X/10), Putchar (x%10+ ' 0 ');}    Template<class t>inline void Writeln (T x) {write (x); Putchar (' \ n ');} -------IO template------typedef long LONG ll;struct poster{int left,right;} Pos[maxn];int x[maxn<<1];int hash[10000011];struct node{bool bcovered;} p[maxn<<3];///is initially visible, i.e. falsevoid build (int rt,int l,int R) {p[rt].bcovered=false;    if (l==r) return;    Build (Lson); Build (Rson);} Insert a poster, true is this poster partially or completely visible, false is not seen bool update (int rt,int l,int r,int x,int y) {//printf ("%d%d%d%d%d\n", Rt,l,r,x,    y);    if (p[rt].bcovered) return false;    if (l==x&&y==r) {return p[rt].bcovered=true;    } bool ans;    if (y<=m) ans=update (lson,x,y);    else if (x>m) ans=update (rson,x,y); else {ans=update (lson,x,m) |update (rson,m+1,y);//bitwise OR, GUARANTEED | Both sides are calculated, not by | |, because | | is a short-circuit operation, if the left is true, the right side will not be calculated} p[rt].bcovered=p[rt<<1].bcovered&&p[rt<<1|1].bcovered;// Back to update father's status return ans;   int main () {//#ifndef Online_judge//Freopen ("In.txt", "R", stdin);    #endif//Online_judge int T;    int n,m,i,j,k,t;    Read (T);        while (t--) {read (n);        int cnt=0;            for (i,n) {read_ (pos[i].left,pos[i].right);            X[cnt++]=pos[i].left; X[cnt++]=pos[I].right;        } sort (x,x+cnt);        Cnt=unique (X,X+CNT)-X;        int num=0;            for (I,CNT)///discretization {hash[x[i]]=num;                if (i<cnt-1) {if (x[i+1]-x[i]==1)//processing non-adjacent discretization is still not adjacent, adjacent still adjacent num++;            else num+=2;        }} build (1,0,num); int ans=0;///the poster from the outside (as opposed to the real situation), guaranteeing that the later post has no effect on the previous for (i=n-1;i>=0;i--) if (Update (1,0,num,hash[pos[i].left],hash[        Pos[i].right]) ans++;    Writeln (ANS); } return 0;} /* discretization to note a little 1 101 36 10 should be 0 60 24 6 note the non-adjacent before discretization, is also not adjacent after discretization */


D-mayor ' s Posters (segment tree + discretization) POJ 2528

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.