poj-2528 Mayor ' s Posters (segment tree + discretization)

Source: Internet
Author: User

Test instructions: Posters on the wall, posters can be covered between each other, ask the last can see a few posters.

Idea: This problem data range is very large, directly engage in easy timeout + Hyper memory, need to discretization.

Discretization is simply to use the values we need to use, such as interval [1000,2000],[1990,2012]

We can't use [-inf,999][1001,1989],[1991,1999],[2001,2011][2013,+inf] these values,

So just 1000,1990,2000,2012 is enough, mapping it to 0,1,2,3 complexity will be lowered.

So discretization is to save all the values that need to be used, and after sorting, mapping to 1-n, you can reduce the complexity.

But the problem is that each number actually represents a unit length (not a single point), and common discretization can cause a lot of errors.

Like what:

1-10 1-4 5-10
1-10 1-4 6-10

[1,4][1,2][3,4] After discretization
To solve this flaw, we can add some processing to the sorted array, for example [1,2,6,10]
If the adjacent digit spacing is greater than 1, add any number to it, such as add [1,2,3,6,7,10], and then do the line segment tree.
Segment tree Feature: Update: Segment replace query: Simple hash

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 #defineLson l,m,rt<<16 #defineRson m+1,r,rt<<1|17 8 Const intMAXN =11111;9 BOOLHASH[MAXN];Ten intLI[MAXN],RI[MAXN]; One intx[maxn*3]; A intcol[maxn<<4]; - intCNT; -  the voidPushdown (intRT) - { -     if(col[rt]!=-1) -     { +col[rt<<1]=col[rt<<1|1]=Col[rt]; -col[rt]=-1; +     } A } at  - voidUpdateintLintRintCintLintRintRT) - { -     if(l<=l&&r<=R) -     { -col[rt]=C; in         return; -     } to pushdown (RT); +     intM= (l+r) >>1; -     if(l<=m) update (L,r,c,lson); the     if(m<R) Update (L,r,c,rson); * } $ Panax Notoginseng voidQueryintLintRintRT) - { the     if(col[rt]!=-1) +     { A         if(!hash[col[rt]]) cnt++; thehash[col[rt]]=true; +         return; -     } $     if(L==R)return; $     intM= (l+r) >>1; - query (Lson); - query (Rson); the } - Wuyi intBin (intKeyintNintx[]) the { -     intL=0, r=n-1; Wu      while(l<=R) -     { About         intM= (l+r) >>1; $         if(X[m]==key)returnm; -         if(X[m]<key) l=m+1; -         Elser=m-1; -     } A     return-1; + } the  - intMain () $ { the     //freopen ("A.txt", "R", stdin); the     intT,n; thescanf"%d",&t); the      while(t--) -     { inscanf"%d",&n); the         intnn=0; the          for(intI=0; i<n;i++) About         { thescanf"%d%d",&li[i],&ri[i]); thex[nn++]=Li[i]; thex[nn++]=Ri[i]; +         } -Sort (x,x+nn); the         intm=1;Bayi          for(intI=1; i<nn;i++)//Remove the duplicate the         { the             if(x[i]!=x[i-1]) x[m++]=X[i]; -         } -          for(inti=m-1;i>0; i--)//Insert any number if the spacing is greater than 1 the         { the             if(x[i]!=x[i-1]+1) x[m++]=x[i-1]+1; the         } theSort (x,x+m); -memset (col,-1,sizeof(col)); the          for(intI=0; i<n;i++)//find the position of the left and right boundary in X the         { the             intL=Bin (li[i],m,x);94             intR=Bin (ri[i],m,x); theUpdate (L,r,i,0M1);//updated with the color of the first I the         } theCnt=0;98memset (Hash,0,sizeof(hash)); AboutQuery0M1); -printf"%d\n", CNT);101     }102     return 0;103}

poj-2528 Mayor ' 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.