POJ 2528 Mayor ' s Posters (segment tree interval overlay, discretization)

Source: Internet
Author: User
Tags integer numbers sort
Mayor ' s Posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 49385 Accepted: 14304

Description the citizens of Bytetown, AB, could not stand so the candidates in the mayoral election campaign has been p Lacing 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 ten
3 4
7 10

Sample Output

4

Source Alberta Collegiate Programming Contest 2003.10.18

The main idea: to put a poster on one wall, each poster must be plastered with a full tile (pictured above). Ask how many posters can be seen (showing some of them).
Analysis: Since it is the problem of the interval, it must be solved by the segment tree. So it is easy to think of the tile as the interval, the poster to stick to the scope of the tile to cover the range.
The problem is abstract: stain on one side of the wall and finally ask a few different colors.
The problem is almost settled here. But a look at the data range, Tile has 10000000, if the achievements, it must be mle. Discretization is used here.
--------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------
Discretization: The popular point, discretization is the compression interval, so that the original long-range mapping to a new short interval, but the interval compression before and after the coverage of the relationship is unchanged. As an example:

There is a 1 to 10 axis (length 9), given a 4 interval [2,4] [3,6] [8,10] [6,9], the covering relationship is the latter covering the former, each interval is dyed 1 2 3 4.

Now we take the 8 endpoints of these 4 intervals, 2 4 3 6 8 10 6 9

Then delete the same endpoint, where the same endpoint is 6, leaving 2 4 3 6 8 10 9

Sort it in ascending order, get 2 3 4 6 8 9 10

And then build the mapping

2 3 4 6 8 9 10

↓↓↓↓↓↓↓

1 2 3 4 5 6 7

So the new 4 interval is [1,3] [2,4] [5,7] [4,6], the coverage relationship has not been changed. The new axis is 1 to 7, that is, the length of the original axis is compressed from 9 to 6, it is obvious that the construction [1,7] segment tree is more space-saving and faster to search than the segment tree that constructs [1,10], but the results of the solution are consistent. (The above I was copying others = =, oneself is also the first time contact discretization, just think he speaks very popular.) ) -------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------
Back to this question, in fact, and ordinary discretization is not the same. The reason to look at the picture in the title: He a tile represents an interval, not a point, which needs to be handled when discretization.
Why is it impossible to make ordinary discretization if it represents an interval?
For example: for the interval "1,4" and "5,10", such as the above, "1,10" times all covered, if the above interval discretization, will be "" "and" 3,4 "two intervals, can be considered" 1,4 "are all covered. The
looks at "1,4", "6,10", and the discretization results are the same as "1,4" and "5,10", but in fact, "1,10" is not completely overwritten.
How to resolve. After the interval is sorted and traversed, if the adjacent two number difference is greater than 1, it is arbitrary to add a number in the size of two. This will be a good solution to the above problem.

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #define M 10050
using namespace Std; struct post{int l,r;}
POST[M];
int x[m<<1],ans,a[m<<4];
int tohash[10000005]; struct tree{int l,r,color;}
TREE[1000005];
	void build (int l,int r,int root) {tree[root].l=l;
	Tree[root].r=r;   Tree[root].color=-1;
	The initialization is assigned to-1 if (l==r) return;
	int mid=l+r>>1;
	Build (l,mid,root<<1);
Build (mid+1,r,root<<1|1); } void pushdown (int root) {//If there are multiple colors within a range, it is assumed that color is -1 if (tree[root].color!=-1) {Tree[root<<1].color=tree[ro
			ot<<1|1].color=tree[root].color;
	Tree[root].color=-1;
} return;
		} void Update (int l,int r,int z,int root) {if (l==tree[root].l&&tree[root].r==r) {tree[root].color=z;
	return;
	} pushdown (root);
	int mid=tree[root].l+tree[root].r>>1;
	if (r<=mid) update (L,R,Z,ROOT&LT;&LT;1);
	else if (l>mid) update (L,R,Z,ROOT&LT;&LT;1|1);
else {update (l,mid,z,root<<1);		Update (MID+1,R,Z,ROOT&LT;&LT;1|1);
	}} void query (int root) {int flag,j;
	if (tree[root].color!=-1) {//If the current range is not multiple colors, add it directly into the array.
		A[ans++]=tree[root].color;
	Return
	} if (TREE[ROOT].L==TREE[ROOT].R) return;
	Query (root<<1);
	Query (root<<1|1);
Return
  } int main () {int t,i,j,k,n;
  scanf ("%d", &t);
  	while (t--) {scanf ("%d", &n);
  	j=0;
  	 for (i=0;i<n;i++) {scanf ("%d%d", &AMP;POST[I].L,&AMP;POST[I].R);
  	 X[J++]=POST[I].L;
  	X[J++]=POST[I].R;
  	} sort (x,x+j);     J=unique (X,X+J)-X; The unique function is used to select the repeating number and place it at the end of the array, and returns the end of the non-repeating sequence.   (equivalent to the number of repetitions removed) for (i=j-1;i>=0;i--) if (x[i]!=x[i-1]+1) x[j++]=x[i-1]+1;
  	Solve the common discretization defect sort (x,x+j);
  	int num=1;     for (i=0;i<j;i++) tohash[x[i]]=num++;
    Discrete mapping build (1,num-1,1);
    	
    for (i=0;i<n;i++) {update (tohash[post[i].l],tohash[post[i].r],i+1,1);
    } memset (A,0,sizeof (a));
    ans=0;
    Query (1);
    
    Sort (A,a+ans);
 

 int Y=1; for (i=1;i<ans;i++)
 if (a[i]!=a[i-1]) y++;
    This will remove the previously repeated colors, and select only the colors that are not duplicated.
  printf ("%d\n", y);
} return 0; }



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.