Poj2528mayor ' s Posters segment tree + discretization

Source: Internet
Author: User
Tags integer numbers

Mayor ' s posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 51098 Accepted: 14788

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


Test instructions
Given some posters that may overlap each other and tell you each poster
The width (the height is the same) and the stacking order, ask how many are not completely covered?
Exercises
First discretization of the N poster
And then the line tree is looking for the show.
The code was written down.
#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineMAXN 200051#defineMoD 10007#defineEPS 1e-9Const intinf=0x3f3f3f3f; inline ll read () {ll x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//******************************************************************structnode{intX,INDEX,JJ;} a[100001];BOOLCMP (node A,node b) {returna.x<b.x;}structss{intL,r,tag,v;} tr[100001];intn,aa[100001],bb[100001],hash[100001];voidPushdown (intk) {    if(tr[k].tag==0)return; if(TR[K].L==TR[K].R)return; Tr[k<<1].tag=Tr[k].tag; Tr[k<<1|1].tag=Tr[k].tag; Tr[k<<1].v=Tr[k].tag; TR[K].V=Tr[k].tag; Tr[k].tag=0;}voidBuildintKintSintt) {TR[K].L=s; TR[K].R=T; Tr[k].tag=0; if(t==R) {TR[K].V=0;return; }    intMid= (s+t) >>1; Build (k<<1, S,mid); Build (k<<1|1, mid+1, t);}voidUpdateintKintSintTintc) {    if(Tr[k].tag) {pushdown (k); }    if(s==tr[k].l&&tr[k].r==t) {Tr[k].tag=C; TR[K].V=C; return; }    intMid= (TR[K].L+TR[K].R) >>1; if(t<=mid) Update (k<<1, S,t,c); Else if(s>mid) Update (k<<1|1, S,t,c); Else{update (k<<1, S,mid,c); Update (k<<1|1, mid+1, T,c); }}voidAskintKintSintt) {    if(tr[k].l==s&&t==tr[k].r&&Tr[k].tag) {Hash[tr[k].tag]=1; return ; }    intMid= (TR[K].L+TR[K].R) >>1; if(T<=mid) Ask (k<<1, s,t); Else if(S>mid) Ask (k<<1|1, s,t); Else{Ask (k<<1, S,mid); Ask (k<<1|1, mid+1, T); }}intMain () {intt=read ();  while(t--) {memset (hash,0,sizeof(hash)); scanf ("%d",&N);  for(intI=1; i<=n;i++) {scanf ("%d%d", &a[i*2-1].x,&a[i*2].x); A[i*2-1].index=i; A[i*2].index=i; A[i*2-1].jj=1; A[i*2].jj=2; } a[0].x=0; Sort (a+1, a+n*2+1, CMP); //for (int i=1;i<=n*2;i++) cout<<a[i].index<< "" <<a[i].x<<endl;         inttmp=1;  for(intI=1; i<=n*2; i++){        if(a[i].x==a[i-1].x) tmp--; if(a[i].jj%2==1) Aa[a[i].index]=tmp; Elsebb[a[i].index]=tmp; TMP++; }//for (int i=1;i<=n;i++) cout<< "" <<aa[i]<< "" <<bb[i]<<endl;Build1,1, tmp-1);  for(intI=1; i<=n;i++) {Update (1, Aa[i],bb[i],i); }//for (int i=1;i<=16;i++) cout<<tr[i].tag<<endl;Ask1,1, tmp-1); intans=0;  for(intI=1; i<=n;i++)       {           if(Hash[i]) ans++; } cout<<ans<<Endl; }    return 0;}
Konjac Konjac Code

Poj2528mayor ' s Posters segment tree + discretization

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.