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
Jannatul Ferdows Jenny: 2015-06-30 11:21:17

Weak test cases :| My ac code failed at this -


2 2
4 8
3 5
9 15
5 5
1 1
3 4
4 4
2 3



Figured the problem after submitting the same code at lightoj

Nashir Ahmed: 2015-06-17 09:58:52

will sqrt ( 10^7 ) * 40000 pass??
Number of test case should've been included :|

Hailo: 2015-06-16 08:45:16

Its not necesary to compress coordinates.

Eddy Cael: 2015-04-27 20:48:10

Nice sweep problem. :D

a b : 2014-11-02 17:57:15

Last edit: 2015-02-17 12:21:28
maniAC: 2014-08-06 01:07:34

Nice problem.

Ellie And Joel: 2014-06-13 11:44:15

what's problem with test 0-th? it fail it self.

Pushkar Singh: 2014-02-19 11:35:02

it's failing on 0th test case itself. Any idea what possibly can be 0th test case.

amirmd76: 2013-12-14 09:27:31

7 seconds ? :O
It's too much .It can be solved in less than 0.5 second.

Last edit: 2013-12-14 09:28:00
Haidar Abboud: 2013-05-22 11:34:30

Comment #1 : done using an O(N log N) algorithm. Can it be solved in O(N) ?
Comment #2 :

Hint : Use coordinate compression to avoid TLE.

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