POJ 2528 Segment Tree + discretization

Source: Internet
Author: User
Tags integer numbers

E-Segment Update crawling in process ... crawling failed time limit:1000MS Memory Limit:65536KB 64bi T IO Format:%lld &%llu Submit Status

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 Poste Rs ' size, 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

151 42 68 103 47 10

Sample Output

4

crawling in process ... crawling failed time limit:1000MS Memory Limit:65536KB 64B

Thinking Analysis: Segment tree interval update problem, but to note that the length of the given may be very large, there are 1e9, without processing directly maintain a segment tree will certainly

Mle,tle, but we notice that there are at most only 2e4 points, so we can use discrete ideas to pre-process the interval, so-called discretization,

In my understanding, it seems that a large interval is mapped to a very small interval without altering the original size coverage, but attention to simple discretization may

The

error occurs, example one: 1-10 1-4 5-10
example two: 1-10 1-4 6-10
ordinary discretization has become [1,4] [Up] [3,4]
segment 2 covers [up], Line 3 covers [3,4], then line 1 is completely covered out?
example one is completely covered, and example two is not overwritten

The solution is for a distance greater than 1 of the two adjacent points, the middle and then insert a point, the subject also used the idea of the lazy mark

The direct update interval is marked without first processing the child nodes, and if necessary, the tag is passed down one layer at a later point.

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;Const intmaxn=20000+ -;//Number of pointsinttree[maxn<<4];intLI[MAXN],RI[MAXN];intlisan[3*MAXN];BOOLvisit[3*MAXN];voidPushdown (intp) {Tree[p<<1]=tree[(p<<1)|1]=Tree[p]; TREE[P]=-1;}voidUpdateintPintLintRintXintYintv) {    if(x<=l&&y>=R) {Tree[p]=v; return; }    if(tree[p]!=-1) Pushdown (p); intMid= (l+r) >>1; if(y<=mid) Update (p<<1, l,mid,x,y,v); Else if(x>mid) Update ((p<<1)|1, mid+1, r,x,y,v); ElseUpdate (p<<1, l,mid,x,mid,v), Update ((p<<1)|1, mid+1, r,mid+1, y,v);}intans;voidQueryintPintLintR) {    //cout<<p<<endl;    if(tree[p]!=-1)    {        if(!Visit[tree[p]]) {ans++; VISIT[TREE[P]]=true; }        return; }    if(L==R)return; //if (tree[p]!=-1) pushdown (p);    intMid= (l+r) >>1; Query (P<<1, L,mid); Query (P<<1)|1, mid+1, R);}intMain () {intT; scanf ("%d",&T); intN;  while(t--) {scanf ("%d",&N); memset (Tree,-1,sizeof(tree)); memset (Visit,false,sizeof(visit)); inttot=0;  for(intI=0; i<n;i++) {scanf ("%d%d",&li[i],&Ri[i]); Lisan[tot++]=Li[i]; Lisan[tot++]=Ri[i]; } sort (Lisan,lisan+tot);//tot is the length of the array        intM=unique (Lisan,lisan+tot)-Lisan; intt=m;  for(intI=1; i<t;i++)        {            if(lisan[i]-lisan[i-1]>1) Lisan[m++]=lisan[i-1]+1; } sort (Lisan,lisan+m); //for (int i=0;i<m;i++)//cout<<lisan[i]<< ""; //cout<<endl;         for(intI=0; i<n;i++)        {            intX=lower_bound (Lisan,lisan+m,li[i])-Lisan; intY=lower_bound (Lisan,lisan+m,ri[i])-Lisan; Update (1,0, M-1, X,y,i); } ans=0; Query (1,0, M-1); printf ("%d\n", ans); }}

POJ 2528 Segment Tree + discretization

Related Article

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.