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
Source
Alberta Collegiate Programming Contest 2003.10.18
Train of Thought: First discretization, interval
1 42 68 103 47 10
Processed
1 42 57 83 46 8
Use the line segment tree to maintain the color values of each interval. Each poster corresponds to a different color value.
# Include <cstdio> # include <algorithm> using namespace STD; struct {int L, R;} Q [10000]; struct H {// used for discretization, id indicates the original poster. If POS is 0, it indicates l in the poster. If POS is 1, it indicates RINT Val, ID, Pos;} H [20000]; int color [80000]; bool vis [10001]; bool CMP (struct h a, struct h B) {return. val <B. val;} void build (INT idx, int S, int e) {If (s! = E) {int mid = (S + E)> 1; build (idx <1, S, mid); Build (idx <1 | 1, mid + 1, e);} color [idx] = 0; // The color value of all nodes is initialized to 0} void add (INT idx, int S, int e, int L, int R, int Val) {If (S = L & E = r) // If the interval is the same, change the color value {color [idx] = val; return;} If (color [idx] & Color [idx]! = Val) // if there is a color and the color value is different, the color value is assigned to the son node and the color is set to zero. {color [idx <1] = color [idx]; color [idx <1 | 1] = color [idx]; color [idx] = 0;} int mid = (S + E)> 1; if (r <= mid) add (idx <1, S, mid, L, R, Val); // In the son else if (L> mid) add (idx <1 | 1, Mid + 1, E, L, R, Val ); // There are {Add (idx <1, S, mid, L, mid, Val); add (idx <1 | 1, mid + 1, E, Mid + 1, R, Val) ;}} int query (INT idx) {If (color [idx]) // uses a color as the recursive boundary, because adding after discretization will certainly not have no color value from the root node to the leaf node. {If (! Vis [color [idx]) {vis [color [idx] = 1; return 1;} return 0;} return query (idx <1) + query (idx <1 | 1) ;}int main () {int t, n, I, last, total; scanf ("% d", & T ); while (t --) {scanf ("% d", & N); for (I = 0; I <n; I ++) {scanf ("% d", & Q [I]. l, & Q [I]. r); H [I * 2]. val = Q [I]. l; H [I * 2]. id = I; H [I * 2]. pos = 0; H [I * 2 + 1]. val = Q [I]. r; H [I * 2 + 1]. id = I; H [I * 2 + 1]. pos = 1;} // discretization sort (H, H + N * 2, CMP); last =-1; Total = 0; for (I = 0; I <n * 2; I ++) {If (H [I]. Val! = Last) {total ++; If (H [I]. Val! = Total) {If (H [I]. pos) Q [H [I]. id]. R = total; else Q [H [I]. id]. L = total;} Last = H [I]. val;} else {If (H [I]. val! = Total) {If (H [I]. pos) Q [H [I]. id]. R = total; else Q [H [I]. id]. L = total ;}}// discretization completed for (I = 0; I <= N; I ++) vis [I] = 0; build (, total ); for (I = 0; I <n; I ++) {Add (1, 1, total, Q [I]. l, Q [I]. r, I + 1);} printf ("% d \ n", query (1 ));}}