POJ 2528 Mayor & #39; s posters (discretization line segment tree), pojposters

Source: Internet
Author: User
Tags integer numbers

POJ 2528 Mayor's posters (discretization line segment tree), pojposters

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

Since the number of points reaches 1e7, and the update is 1e4, discretization is considered.

Then I thought of the decisive timeout of the map result. I can only change the method.

The common practice is to place the (l, r) range of the Two-dimensional array mp [maxn <2] [2.

# Include <cstdio> # include <cstring> # include <algorithm> # include <vector> # include <string> # include <iostream> # include <queue> # include <cmath> # include <map> # include <stack> # include <bitset> using namespace std; # define REPF (I, a, B) for (int I = a; I <= B; ++ I) # define REP (I, n) for (int I = 0; I <n; ++ I) # define CLEAR (a, x) memset (a, x, sizeof a) typedef long LL; typedef pair <int, int> p Il; const int maxn = 100100; int col [maxn <2], vis [maxn <2]; int mp [maxn <2] [2]; struct node {int val; // value int num; // node number} e [maxn <2]; int t, m, ans; void pushdown (int rs) {if (col [rs]) {col [rs <1] = col [rs <1 | 1] = col [rs]; col [rs] = 0 ;}} void build (int rs, int l, int r) {col [rs] = 0; if (l = r) return; int mid = (l + r)> 1; build (rs <1, l, mid); build (rs <1 | 1, mid + 1, r);} void update (int x, int y, int c, int l, int r, int rs) {if (l> = X & r <= y) {col [rs] = c; return;} pushdown (rs); int mid = (l + r)> 1; if (x <= mid) update (x, y, c, l, mid, rs <1); if (y> mid) update (x, y, c, mid + 1, r, rs <1 | 1);} void query (int l, int r, int rs) {if (col [rs]) {if (! Vis [col [rs]) ans ++; vis [col [rs] = 1; return;} if (l = r) return; int mid = (l + r)> 1; query (l, mid, rs <1); query (mid + 1, r, rs <1 | 1);} int cmp (node l1, node l2) {return l1.val <l2.val;} int main () {scanf ("% d ", & t); while (t --) {scanf ("% d", & m); int l = 1; REP (I, m) {scanf ("% d", & mp [I] [0], & mp [I] [1]); e [I <1]. val = mp [I] [0]; e [I <1]. num =-(I + 1); // set the left endpoint to negative to distinguish the right endpoint. num can restore the original store ID e [I <1 | 1]. val = mp [I] [1]; e [I <1 | 1]. num = I + 1 ;} Sort (e, e + 2 * m, cmp); int tmp = e [0]. val, len = 1; REP (I, 2 * m) {if (e [I]. val! = Tmp) {len ++; tmp = e [I]. val;} if (e [I]. num <0) mp [-e [I]. num-1] [0] = len; else mp [e [I]. num-1] [1] = len;} build (1, 1, len); CLEAR (vis, 0); REP (I, m) update (mp [I] [0], mp [I] [1], I +, len, 1); // normal update ans = 0; query (1, len, 1 ); 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.