Poj 2528 mayor's poster

Source: Internet
Author: User
Tags integer numbers

Link: http://poj.org/problem? Id = 2528

Description

The citizens of bytetown, AB, cocould not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. the city councel has finally decided to build an electoral
Wall for placing the posters and introduce the following rules:
  • Every candidate can place exactly one poster on the wall.
  • All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in bytetown ).
  • The wall is divided into segments and the width of each segment is one byte.
  • Each poster must completely cover a contiguous Number of wall segments.

They have built a wall 10000000 bytes long (such that there is enough place for all candidates ). when the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. moreover, the candidates
Started placing their posters on wall segments already occupied by other posters. Everyone in bytetown was curous whose posters will 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 are placed given the information about posters '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 two integer numbers Li 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 <= Li
<= RI <= 10000000. After the I-th poster is 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 are placed.

The picture below has strates the case of the sample input.

Sample Input

151 42 68 103 47 10

Sample output

4

PS: This is the first time that we have been engaged in discretization. There is a problem with the data of this question. nyoj 9 is the original question. poj can be used, nyoj can not be used, and testing

3

1 10 1 3 6 10 the correct answer should be 3. in the code. 2. the root cause is the same value in discretization. in this example, 1 4 1 2 3 4 is discrete. so the answer is 2.

A wall is used to hold posters. Due to the wide range, line segment trees may cause serious waste if they are not discretization ~

Discretization: discretization is a very common technique in programming. It can effectively reduce time complexity. The basic idea is to "only consider the values I need" in many possible cases ". Discretization can improve an inefficient algorithm or even implement an algorithm that is impossible at all. To grasp this idea, we must understand the characteristics of this method from a large number of questions.

To put it simply, we need to separate the data we need and increase the efficiency of time and space.

This question is discrete by means of sorting. This example is given. <>, <>.

By sorting 2000 3000 4000 5000 8000 10000

Ing 1 2 3 4 5 6

The preceding statements can represent <1, 3>, <2, 4>, <5, 6>.

AC code: (although it is incorrect ~~)

# Include <stdio. h> # include <iostream> # include <algorithm> # include <cstring> # include <string> using namespace STD; # define n Limit 8int T; int POS [N] [2]; struct segtree {int left, right, Col;} stree [N * 4]; struct lishanhua {int point, num ;} list [N * 2]; int CMP (const void * P1, const void * P2) {return (* (lishanhua *) P1 ). point> (* (lishanhua *) P2 ). point )? 1:-1;} bool flag [n + 1]; void build (int l, int R, int K) // build {stree [K]. left = L; stree [K]. right = r; stree [K]. col = 0; If (L = r) return; int mid = (L + r)/2; build (L, mid, 2 * k ); build (Mid + 1, R, 2 * k + 1);} void insert (int l, int R, int K, int C) // insert a line segment {If (stree [K]. left = L & stree [K]. right = r) {stree [K]. col = C; return;} If (stree [K]. col> 0 & stree [K]. col! = C) {stree [2 * K]. col = stree [K]. col; stree [2 * k + 1]. col = stree [K]. col; stree [K]. col = 0;} int mid = (stree [K]. right + stree [K]. left)/2; If (r <= mid) insert (L, R, 2 * k, c); else if (L> mid) insert (L, R, 2 * k + 1, C); else {insert (L, mid, 2 * k, c); insert (Mid + 1, R, 2 * k + 1, c) ;}} void query (int K) {If (stree [K]. col! = 0) {If (! Flag [stree [K]. col]) {T ++; flag [stree [K]. col] = true;} return;} query (2 * k); query (2 * k + 1);} int main () {int I, j, test, num; scanf ("% d", & Test); While (test --) {memset (flag, 0, sizeof (FLAG); scanf ("% d", & num ); T = 0; for (I = 0; I <num; I ++) {scanf ("% d", & Pos [I] [0], & Pos [I] [1]); list [I * 2]. point = POS [I] [0]; list [I * 2 + 1]. point = POS [I] [1]; list [I * 2]. num =-(I + 1); list [I * 2 + 1]. num = I + 1;} qsort (list, 2 * num, sizeof (list [0]), CMP); // sort int temp = Li St [0]. Point, Mm = 1; // discrete for (I = 0; I <2 * num; I ++) {If (list [I]. Point! = Temp) {mm ++; temp = list [I]. point;} If (list [I]. num <0) POS [-(list [I]. num)-1] [0] = mm; elsepos [list [I]. num-1] [1] = mm;} build (1, mm, 1); for (I = 0; I <num; I ++) insert (Pos [I] [0], POS [I] [1], 1, I + 1); query (1); printf ("% d \ n ", t );}}

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.