Mayor's posters (line segment tree + discretization)

Source: Internet
Author: User

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 l I 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 is 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 are placed.

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

Sample Input

151 42 68 103 47 10

Sample output

4

Solution:

The difficulty lies in discretization. The data is too large and must be discretization. It cannot be a normal discretization. If the two adjacent posters are not close to each other, they must be separated during discretization.

For example: () this group of data, after normal discretization should be (), then the answer should be 2.

() After normal discretization of this set of data, it should also be (), and the answer is 2, obviously, the answer to this question should be 3. The problem lies in the absence of an interval.

The solution is to determine whether it is close. If it is not close, add a number to the array during discretization. Here I use struct for discretization.

The rest is to find the number of posters, and color the line segments. The subsequent posters will overwrite the previous posters, and finally count the number of colors.

AC code:

# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; # define lson l, m, RT <1 # define rson m + 1, R, RT <1 | 1 const int maxn = 11111; int X [maxn <2], col [maxn <4], ANS, C [maxn <2]; bool flag [maxn <2]; struct San {int A, Pos ;} f [maxn <2]; bool CMP (San V, San t) {return v. A <t. a;} void Pushdown (int rt) {If (COL [RT]! = 0) {Col [RT <1] = Col [RT <1 | 1] = Col [RT]; Col [RT] = 0 ;}} void Update (int l, int R, int C, int L, int R, int RT) {If (L <= L & R> = r) {Col [RT] = C; return;} Pushdown (RT); int M = (L + r)> 1; if (L <= m) Update (L, r, C, lson); If (M <r) Update (L, R, C, rson);} void query (int l, int R, int RT) {If (COL [RT]! = 0) {If (! Flag [col [RT]) // use flag to check whether the color has appeared before {ans ++; flag [col [RT] = true;} return ;} if (L = r) return; int M = (L + r)> 1; query (lson); query (rson);} int main () {int t, n, ll, RR; scanf ("% d", & T); While (t --) {ans = 0; memset (COL, 0, sizeof (COL )); memset (flag, false, sizeof (FLAG); scanf ("% d", & N); int K = 0; For (INT I = 1; I <= N; I ++) {scanf ("% d", & LL, & RR); // use F to store values in the left and right ranges, therefore, there are 2 * n f [++ K]. A = ll; F [K]. pos = K; F [++ K]. A = RR; F [K]. pos = K;} Sort (F + 1, F + k + 1, CMP); // sort int t = 2 * N for the first time; for (INT I = 2; I <= 2 * n; I ++) {If (F [I]. a> F [I-1]. A + 1) // determine whether the two are adjacent. You do not need to consider whether the two belong to the same poster {f [++ T]. A = f [I-1]. A + 1; // Add a number next to f [T]. pos = 0; // If POS is set to 0, the valid value is not included in the discretization process.} int M = 1; C [1] = 1; sort (F + 1, F + t + 1, CMP); // secondary sorting, generation interval for (INT I = 2; I <= T; I ++) // deduplicated {If (F [I]. A = f [I-1]. a) C [I] = m; else C [I] = ++ m;} For (INT I = 1; I <= T; I ++) // obtain the valid value x [f [I]. pos] = C [I]; for (INT I = 1; I <= 2 * n-1; I + = 2) // color Update (X [I], X [I + 1], I, 1, 4 * n, 1); query (1, 4 * n, 1 ); // calculate the color type printf ("% d \ n", ANS);} 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.