POSTERS - Election Posters

A parliamentary election was being held in Byteland. Its enterprising and orderly citizens decided to limit the entire election campaign to a single dedicated wall, so as not to ruin the panorama with countless posters and billboards. Every politician was allowed to hang exactly one poster on the wall. All posters extend from top to bottom, but are hung at different points of the wall, and may be of different width. The wall is divided horizontally into sections, and a poster completely occupies two or more adjacent sections.

With time, some of the posters were covered (partially or completely) by those of other politicians. Knowing the location of all the posters and the order in which they were hung, determine how many posters have at least one visible section in the end.


The input begins with the integer t, the number of test cases. Then t test cases follow.

Each test case begins with a line containing integer n - the number of posters (1 <= n <= 40000). Then n lines follow, the i-th (1 <= i <= n) containing exactly two integers li ri, denoting the numbers of the leftmost and rightmost sections covered by the i-th poster (1 <= li < ri <= 107). The input order corresponds to the order of hanging posters.


For each test case output a line containing one integerĀ - the number of posters with visible sections.


Sample input:
1 4
2 6
8 10
3 4
7 10

Sample output:

An illustration of the sample input is given below.

The wall with posters

hide comments
sharif ullah: 2017-11-15 10:53:16

1 3
4 6
6 7
8 8
1 1
3 4

This is not valid input cz a poster completely occupies two or more adjacent sections.

prayassahni: 2016-10-29 18:43:47

can't find a test case wrong but getting WA(lazy propgation+segment tree)

sieunhanbom04: 2016-08-17 19:50:23

weak test cases I suppose

fork_my_code: 2016-07-31 08:15:16

just go through image and write code blindly using set

VISHAL ASHANK: 2016-07-27 03:52:18

nice problem.. no need of coordinate compression.. just be lazy

m_elbrbry: 2016-06-21 12:30:38

@Jannatul Ferdows Jenny
your test case is NOT VALID.
"and a poster completely occupies two or more adjacent sections".

j1k7_7(JaskamalKainth): 2016-06-14 11:11:07

Basic Segment tree with Lazy Prop , Co-ord Compression.

Child's play!

minhthai: 2016-02-10 16:25:12

if u use sweep, be careful with the coordinate format

saurabh anand: 2016-01-01 18:30:25

segment tree + lazy propagation takes < 0.5 s

azizul hakim: 2015-12-21 08:42:32

Use coordinate compression. that saves both time, and also saves from overflow while summing... -_- got so many wa.

Added by:adrian
Time limit:7s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Resource:VI Polish Collegiate Team Programming Contest (AMPPZ), 2001