POJ 2528 (segment tree + discretization) Poster of the mayor

Source: Internet
Author: User

http://poj.org/problem?id=2528

The main idea is that the mayor will post posters, give the length of the wall and the length of the poster to be posted in turn (refer to the topic to the picture), ask the last poster you can see a few

Is that some of the first posters may be affixed to the poster completely covered, it is not visible

Here is a very abstract interval update, the length of the wall for the establishment of a segment of the total interval tree, each posted a poster represents the color of the interval to the corresponding, each poster color of course

are not the same, ask for the last number of colors on the line, but also here to use the basis of discretization

Discretization is a way to improve the space-time efficiency of an algorithm by mapping an infinite number of individuals to a limited space in an infinite space.

To put it simply, let's say that the interval length is 100 million or more, but the specific value in the interval is only used at up to 1 million, and it does not say whether it can open such a large array, it also causes

Waste of memory, so use discretization to save unnecessary waste

For the example of this topic (top three)

If the color of the interval 1 7 2 6 8 10 is changed, then the remaining numbers are not used

So we sort these numbers, x[1]=1 x[2]=2 x[3]=6 x[4]=7 x[5]=8 x[6]=10

But if each adjacent difference is greater than 1, insert a number (insert x[i]-1 well),

(If you don't plug in,

Suppose three posters are: 1 10 1 4 6 10

discretization x[1] = 1 x[2]=4 x[3]=6 x[4]=10
The first poster: The 1~4 of the wall was dyed as 1;
The second Zhang Hai Chime: The wall is dyed as 2,3~4 still 1;
The third Zhang Hai The clock: The Wall's three-and-a-3,1~2 is still 2.
Finally, the first poster is completely covered and then output 2, but the correct answer is obviously 3.

After inserting the new sort to x[1]=1 x[2]=2 x[3]=5 x[4]=6 x[5]=7 x[6]=8 x[7]=9 x[8]=10

and build on this new 8-length array.

Code

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 structPoint {7     intL,r;8     intmark,sum;9 };TenPoint tree[10001*4*4],num[10001*4]; One intans,res[10001*4],visit[10001*4]; A voidBuildintIintLeftintRight ) - { -Tree[i].l=left,tree[i].r=Right ; thetree[i].mark=tree[i].sum=0; -     if(Left==right)return ; -     intMid= (left+right)/2; -Build (i*2, left,mid); +Build (i*2+1, mid+1, right); - } + voidUpdateintIintLeftintRightintval) A { at     if(Left<=tree[i].l&&tree[i].r<=right) {tree[i].mark=tree[i].sum=val;return ;} -     if(Tree[i].mark) -     { -tree[i*2].mark=tree[i*2+1].mark=Tree[i].mark; -tree[i*2].sum=tree[i*2+1].sum=Tree[i].mark; -tree[i].mark=0; in     } -     intMid= (TREE[I].R+TREE[I].L)/2; to     if(left>mid) Update (i*2+1, left,right,val); +     Else if(right<=mid) Update (i*2, left,right,val); -     Else the     { *Update (i*2, left,mid,val); $Update (i*2+1, mid+1, right,val);Panax Notoginseng     } - } the intFindinti) + { A     if(tree[i].l==TREE[I].R) the     { +         if(!Visit[tree[i].sum]) -         { $visit[tree[i].sum]=1; $++ans; -         } -         returnans; the     } -     if(Tree[i].mark)Wuyi     { thetree[i*2].mark=tree[i*2+1].mark=Tree[i].mark; -tree[i*2].sum=tree[i*2+1].sum=Tree[i].mark; Wutree[i].mark=0; -     } AboutFind (i*2); $Find (i*2+1); - } - intMain () - { A     intT,i,n,tn,tn1,powr,powl; +scanf"%d",&t); the  -         if(t==0)return 0; $          while(t--) the         { theres[0]=0; thescanf"%d",&n); the              for(i=1; i<=n;i++) -             { inscanf"%d%d",&num[i].l,&NUM[I].R); the                if(num[i].l>num[i].r) Swap (NUM[I].L,NUM[I].R); theres[++res[0]]=NUM[I].L; Aboutres[++res[0]]=NUM[I].R; the             } theSort (res+1, res+res[0]+1); Discretization of theTn1=tn=unique (res+1, res+res[0]+1)-Res; +              for(i=2; i<tn;i++)//Insert Number -                if(res[i]-res[i-1]>1) theres[tn1++]=res[i]-1;Bayires[0]=tn1-1; theSort (res+1, res+res[0]+1); theBuild1,1, res[0]);//build with a new interval -              for(i=1; i<=n;i++) -             { thePowl=lower_bound (res+1, res+1+res[0],NUM[I].L)-Res; thePowr=lower_bound (res+1, res+1+res[0],NUM[I].R)-Res; theUpdate1, powl,powr,i); the             } -ans=0; thememset (Visit,0,sizeof(visit)); thevisit[0]=1; theprintf"%d\n", Find (1));94         } the  the     return 0; the}

POJ 2528 (segment tree + discretization) Poster of the mayor

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.