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.

Input

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.

Output

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

Example

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

Sample output:
4

An illustration of the sample input is given below.

The wall with posters


hide comments
ab_biswas09: 2019-03-16 17:21:41

co-ordinate compression => 0.04 sec

tanmayak99: 2019-01-13 15:05:14

Did it with (Lazy+Segtree) . How to do it with Coordinate compression ?

anshumanc007: 2018-12-14 11:36:03

solved with lazy prop! how to do with DSU?

phoemur: 2018-11-24 23:13:14

With coordinate compression even a naive O(n*m) gets AC here

Segtree + lazy = 0.02s
Coord compression + Brute force = 0.08s

Last edit: 2018-11-24 23:18:08
lapmid: 2018-10-26 16:54:47

AC using Lazy , Anyone Please explain this using DSU

dlp_302: 2018-10-08 16:21:21

dsu

kaiser17: 2018-07-11 21:04:20

too easy

akshat_pat: 2018-04-25 16:10:58

Weak test cases.

jacobian_det: 2018-02-03 16:30:02

Nice problem using dsu and standard problem for segment tree + lazy

Last edit: 2018-02-03 16:30:53
rht_20: 2017-12-16 01:27:48

maybe dataset is weak.
using coordinate compression my ac code failed at this test case:
input:
1
6
1 3
4 6
6 7
8 10
1 2
3 4
output:
5
without using coordinate compression my code passed all the test cases.but it takes 3.7s.


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