Repovive
  • Dashboard
  • Ranking
  • Submissions
  • Discussion
  1. Contests
  2. Standard Round 1
  3. 4E

My submissions

/
/
/
/

Announcements

My submissions

/
/
/
/

Announcements

Solve
Author

4E. Permute Intervals

2500 pts·2s·256 MB

Input

Output

Samples

Input
t
3
Case 1
3
1 3
2 4
3 5
Case 2
3
5 7
1 3
2 4
Case 3
4
2 4
1 3
5 7
1 2
Explanation
Case 1
Case 2
Case 3
Output
Case 1
1 2 3
Case 2
2 3 1
Case 3
1 2 4 3

You are given nnn closed intervals [li,ri][l_i, r_i][li​,ri​].

You want to choose a permutation p1,p2,…,pnp_1, p_2, \ldots, p_np1​,p2​,…,pn​ of {1,2,…,n}\{1,2,\ldots,n\}{1,2,…,n} such that there exists an array a1,a2,…,ana_1, a_2, \ldots, a_na1​,a2​,…,an​ satisfying:

  • li≤ai≤ril_i \le a_i \le r_ili​≤ai​≤ri​ for each 1≤i≤n1 \le i \le n1≤i≤n,
  • api≤api+1a_{p_i} \le a_{p_{i+1}}api​​≤api+1​​ for each 1≤i<n1 \le i < n1≤i<n.

Among all permutations ppp for which such an array aaa exists, output the lexicographically smallest permutation.

It is guaranteed that at least one valid permutation exists.

Constraints

  • 1≤t≤1041 \le t \le 10^41≤t≤104
  • 1≤n≤2×1051 \le n \le 2 \times 10^51≤n≤2×105
  • 1≤li≤ri≤1091 \le l_i \le r_i \le 10^91≤li​≤ri​≤109
ttt nl1r1l2r2⋮lnrn}×t\left. \begin{array}{l} n \\ l_1 \quad r_1 \\ l_2 \quad r_2 \\ \vdots \\ l_n \quad r_n \end{array} \right\} \times tnl1​r1​l2​r2​⋮ln​rn​​⎭⎬⎫​×t
p1p2⋯pn}×t\left. \begin{array}{l} p_1 \quad p_2 \quad \cdots \quad p_n \end{array} \right\} \times tp1​p2​⋯pn​​}×t

Scenario 1: n=3n=3n=3, intervals [1,3],[2,4],[3,5][1,3], [2,4], [3,5][1,3],[2,4],[3,5]. Answer: 1 2 31\ 2\ 31 2 3. Witness: a=[1,2,3]a = [1, 2, 3]a=[1,2,3]. Reading in permutation order: a1=1≤a2=2≤a3=3a_1=1 \le a_2=2 \le a_3=3a1​=1≤a2​=2≤a3​=3. All values in range. The identity permutation works because the intervals already overlap in increasing order. All 666 permutations are valid (intervals overlap heavily), but 1 2 31\ 2\ 31 2 3 is lexicographically smallest.

Scenario 2: n=3n=3n=3, intervals [5,7],[1,3],[2,4][5,7], [1,3], [2,4][5,7],[1,3],[2,4]. Answer: 2 3 12\ 3\ 12 3 1. Witness: a=[5,1,2]a = [5, 1, 2]a=[5,1,2]. Reading in permutation order: a2=1≤a3=2≤a1=5a_2=1 \le a_3=2 \le a_1=5a2​=1≤a3​=2≤a1​=5. Interval 111 has high values [5,7][5,7][5,7] while intervals 2,32,32,3 have low values, so index 111 must come last. Only 222 valid permutations exist: 2 3 12\ 3\ 12 3 1 and 3 2 13\ 2\ 13 2 1. Lexicographically smallest is 2 3 12\ 3\ 12 3 1.

Scenario 3: n=4n=4n=4, intervals [2,4],[1,3],[5,7],[1,2][2,4], [1,3], [5,7], [1,2][2,4],[1,3],[5,7],[1,2]. Answer: 1 2 4 31\ 2\ 4\ 31 2 4 3. Witness: a=[2,2,5,2]a = [2, 2, 5, 2]a=[2,2,5,2]. Reading in permutation order: a1=2≤a2=2≤a4=2≤a3=5a_1=2 \le a_2=2 \le a_4=2 \le a_3=5a1​=2≤a2​=2≤a4​=2≤a3​=5. Interval 3=[5,7]3=[5,7]3=[5,7] has high values so must come last. Among the remaining, index 4=[1,2]4=[1,2]4=[1,2] fits before 333. There are 666 valid permutations; 1 2 4 31\ 2\ 4\ 31 2 4 3 is lexicographically smallest.