PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000153. Tìm đoạn ở bên phải (Find Right Interval)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tìm đoạn ở bên phải (Find Right Interval)

Find Right Interval

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn closed intervals [si,ei][s_i,e_i] numbered from 00 to n−1n-1 in input order. All starting points sis_i are distinct.

A right interval for interval ii is an interval jj with sj≥eis_j\ge e_i and the smallest possible sjs_j. If no such interval exists, return −1-1. The right interval may be interval ii itself. Print the corresponding original indices in input order.

Input

  • The first line contains the number nn of intervals.
  • Each of the next nn lines contains the start sis_i and end eie_i for zero-based index ii.

Output

Print nn integers in one line. The ii-th answer is the zero-based index of the right interval for interval ii, or −1-1 if none exists.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le30; −103≤si≤ei≤103-10^3\le s_i\le e_i\le10^3; all sis_i are distinct.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le2000; −105≤si≤ei≤105-10^5\le s_i\le e_i\le10^5; all sis_i are distinct.
  • Subtask 3 (50 points): 1≤n≤2⋅1041\le n\le2\cdot10^4; −106≤si≤ei≤106-10^6\le s_i\le e_i\le10^6; all sis_i are distinct.

Examples

Example 1

Input

3
3 4
2 3
1 2

Output

-1 0 1

Explanation

The intervals are 0:[3,4]0:[3,4], 1:[2,3]1:[2,3], and 2:[1,2]2:[1,2]. No start is at least 44, so interval 00 yields −1-1. For interval 11, start 33 of interval 00 matches its end, so the answer is 00. For interval 22, eligible starts are 22 and 33; the smaller belongs to interval 11, so the answer is 11.