# POJ 2528Mayor ' s posters segment tree discretization

Source: Internet
Author: User
Tags integer numbers

Mayor ' s Posters
 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 55856 Accepted: 16191

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`

Source

Alberta Collegiate Programming Contest 2003.10.18

Reference Segment Tree Full version

/**

Test Instructions: posters on the wall, posters can cover each other, ask to see a few posters at the end
Discretization is simply to use the values we need , such as interval [1000,2000],[1990,2012] We don't use [-∞,999][1001,1989][1991,1999][2001,2011][2013, +∞] These values, so I just need to 1000,1990,2000,2012 enough, to map it to 0,1,2,3, the complexity is greatly lowered
So discretization to save all the values that need to be used, after sorting, mapping to 1~n, so the complexity will be much smaller
The following two simple examples should be presented to illustrate the drawbacks of common discretization:
1-10 1-4 5-10
1-10 1-4 6-10
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

**/

Accode:

`#include <cstdio> #include <cstring> #include <algorithm> #define MAXN 12345#define ll Long long#define M ((l+r) >>1) #define TMP (ST&LT;&LT;1) #define Lson l,m,tmp#define Rson m+1,r,tmp|1using namespace Std;int tree[maxn <<2];bool hash[maxn];int li[maxn],ri[maxn];int col[maxn<<4];int ans;void push_down (int st) {if (col[st]!=-        1) {col[tmp]=col[tmp|1]=col[st];    Col[st]=-1;        }}void Update (int l,int r,int c,int l,int R,int St) {if (l<=l &&r<=r) {col[st]=c; return;} Push_down (ST), if (l<=m) update (L,R,C, Lson), if (m<r) update (L,r,c,rson);} void query (int l,int R,int st) {if (col[st]!=-1) {if (!hash[col[st]]) ans++;hash[col[st]]=true;return;} if (l==r) return;query (Lson); query (Rson);} int Bin (int key,int n,int tree[]) {int L = 0, R = N-1;while (l<=r) {if (tree[m] = = key) return m;if (tree[m] < ke Y) L = m + 1;else r = m-1;} return-1;} int main () {int T, n;scanf ("%d", &t), while (T--) {scanf ("%d", &n); int nn = 0;for (int i = 0; i < n; i + +) {scanf ("%d%d", &li[i], &ri[i]); tree[nn++] = li[i];tree[nn++] = Ri[i];} Sort (tree,tree+nn); int pos=1;for (int i=1;i<nn;i++) if (tree[i]! = tree[i-1]) tree[pos++]=tree[i];//de-weight for (int i= pos-1; I>0;i--) if (tree[i]!= tree[i-1]+1) tree[pos++] =tree[i-1] + 1;//dot prevent error sort (tree,tree+pos); Memset (col,-1, sizeof (CO l)); for (int i = 0; i < n; i + +) {//two points to find the Sea newspapers point then update the post order int l=bin (li[i],pos,tree); int R=bin (ri[i],pos,tree); Update (l,r , i,0,pos,1);} Ans=0;memset (hash,false,sizeof (hash)), query (0, pos,1);p rintf ("%d\n", ans);} return 0;}`

POJ 2528Mayor ' s posters segment tree discretization

Related Keywords:

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.