Under construction

Still being built. Things may change

Coloring Intervals

Interview

Time limit3sMemory limit512 MB

Summary
Given n closed intervals with distinct endpoints, find the minimum number of colors so that overlapping intervals get different colors.
Level

Medium4 of 10

Topics
Sorting, Intervals, Greedy, Array
Solved
No attempts yet

Problem

For real numbers a≤ba \le b, the closed interval [a,b][a, b] is the set of all real numbers between aa and bb, inclusive. For example, [3,5]={x∈R∣3≤x≤5}[3, 5] = \{x \in \mathbb{R} \mid 3 \le x \le 5\}.

Bob has nn closed intervals [a1,b1],[a2,b2],…,[an,bn][a_1, b_1], [a_2, b_2], \dots, [a_n, b_n]. For every pair of distinct indices i,j∈{1,2,…,n}i, j \in \{1, 2, \dots, n\}, all of the following hold.

  • aia_i and bib_i are positive integers.
  • ai≤bia_i \le b_i, so [ai,bi][a_i, b_i] is not empty.
  • ai≠bja_i \ne b_j, ai≠aja_i \ne a_j and bi≠bjb_i \ne b_j, so two different closed intervals never share an endpoint.

Bob wants to color each interval with a single color so that any two overlapping intervals get different colors. He wants to know how many colors that takes. In other words, find the smallest positive integer kk such that each interval can be labelled with one of 1,2,…,k1, 2, \dots, k and any two overlapping intervals always carry different labels. Two intervals overlap when their intersection is not empty.

Input

The first line contains the number TT of test cases. Each of the next TT lines describes one test case by giving nn, a1a_1, b1b_1, a2a_2, b2b_2, …\dots, ana_n, bnb_n, in that order. Two consecutive numbers on a line are separated by one or more spaces.

You may assume the following.

  • 1≤T≤101 \le T \le 10
  • n∈{2,3,…,100 000}n \in \{2, 3, \dots, 100\,000\}
  • aia_i and bib_i are positive integers no larger than 232−12^{32} - 1 for every i∈{1,2,…,n}i \in \{1, 2, \dots, n\}.

Output

For each test case, print the minimum number of colors kk on its own line. That is, print the smallest positive integer such that each of [a1,b1],[a2,b2],…,[an,bn][a_1, b_1], [a_2, b_2], \dots, [a_n, b_n] can be labelled with one of 1,2,…,k1, 2, \dots, k and any two overlapping intervals always carry different labels.

Examples1

  1. Example 1

    Input
    3
    4 1 2 3 5 4 8 6 7
    5 2 5 1 10 3 7 4 6 8 9
    4 3 7 4 5 2 9 6 8
    
    Expected output
    2
    4
    3