Poj2528 mayor's posters (line segment tree + discretization)

Source: Internet
Author: User
Tags integer numbers
Mayor's posters
Time limit:1000 ms   Memory limit:65536 K
Total submissions:42808   Accepted:12481

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
/* Question: paste a poster, enter the Left and Right endpoints, and ask the maximum number of questions that are not covered. discretization is required. The questions of the Line Segment tree are rarely done .... Come on !!! C ++ wa, G ++ too much, don't understand ..... The time of the Code is basically the same as that of others: */# include <cstdio> # include <cstring> # include <algorithm> using namespace STD; # define lson I <1, l, mid # define rson I <1 | 1, Mid + 1, rconst int max = 10000 + 10; struct poster {int L, R;} p [Max]; struct node {// The int ing int L, R; bool bcovered;} seg [max <3]; // because each vertex has two endpoints, so it takes 8 times int ncount [max <3]; int hash [10000000 + 10]; void build (int I, int L, int R) {seg [I]. L = L; seg [I]. R = r; seg [I]. bcovered = false; If (SEG [I]. L = seg [I]. r) return; int mid = (SEG [I]. L + seg [I]. r)> 1; build (lson); Build (rson);} bool post (int I, int L, int R) {If (SEG [I]. bcovered) return false; // If (SEG [I]. L = L & seg [I]. R = r) {// The previously overwritten seg [I]. bcovered = true; return true;} bool bresult; int mid = (SEG [I]. L + seg [I]. r)> 1; if (r <= mid) {bresult = post (I <1, L, R);} else if (L> mid) {bresult = post (I <1 | 1, L, R);} else {bool b1 = post (lson); bool b2 = post (rson ); bresult = b1 | B2; // The post operation cannot be taken directly. If the frontend is true, it is not run.} If (SEG [I <1]. bcovered & seg [I <1 | 1]. bcovered) // If the left and right subnodes are completely overwritten, the parent node is also overwritten by seg [I]. bcovered = true; // if this is not done, the subsequent access to the parent node will return directly, but will not access the child node, and return bresult;} void solve () {int T; scanf ("% d", & T); While (t --) {int n, k = 0; scanf ("% d", & N ); for (INT I = 0; I <n; I ++) {scanf ("% d", & P [I]. l, & P [I]. r); ncount [k ++] = P [I]. l; ncount [k ++] = P [I]. r;} Sort (ncount, ncount + k); k = unique (ncount, ncount + k)-ncount; // deduplication, unique returns the end address for (INT I = 0; I <K; I ++) hash [ncount [I] = I; build (, k-1 ); // The root node should start from 1 and the range is 0 --- K-1 // if the root node starts from 0, then the subnode should be 2 * I + 1 and 2 * I + 2 int ans = 0; For (INT I = N-1; I> = 0; I --) {// Here it should be n. Do not write it as K if (post (1, hash [p [I]. l], hash [p [I]. r]) // The ing between the root node and the Left and Right nodes is passed in. // paste the root node from the external node and determine whether it is fully covered by ans ++ ;} printf ("% d \ n", ANS) ;}} int main () {solve (); Return 0 ;}

Poj2528 mayor's posters (line segment tree + discretization)

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.