InterSections

AtCoder
IDabc360_f
Time2000ms
Memory256MB
Difficulty
You are given $N$ intervals numbered $1$ to $N$. Interval $i$ is $[L_i, R_i]$. Two intervals $[l_a, r_a]$ and $[l_b, r_b]$ are said to **intersect** if and only if they satisfy either $(l_a < l_b < r_a < r_b)$ or $(l_b < l_a < r_b < r_a)$. Define $f(l, r)$ as the number of intervals $i$ ($1 \leq i \leq N$) that intersect with the interval $[l, r]$. Among all pairs of **integers** $(l, r)$ satisfying $0 \leq l < r \leq 10^{9}$, find the pair $(l, r)$ that maximizes $f(l, r)$. If there are multiple such pairs, choose the one with the smallest $l$. If there are still multiple pairs, choose the one with the smallest $r$ among them. (Since $0 \leq l < r$, the pair $(l, r)$ to be answered is uniquely determined.) ## Constraints * $1 \leq N \leq 10^{5}$ * $0 \leq L_i < R_i \leq 10^{9}$ $(1 \leq i \leq N)$ * All input values are integers. ## Input The input is given from Standard Input in the following format: $N$ $L_1$ $R_1$ $L_2$ $R_2$ $\vdots$ $L_N$ $R_N$ [samples]
Samples
Input #1
5
1 7
3 9
7 18
10 14
15 20
Output #1
4 11

The maximum value of $f(l, r)$ is $4$, and among the pairs $(l, r)$ that achieve $f(l, r) = 4$, the smallest $l$ is $4$. The pairs $(l, r)$ that satisfy $f(l, r) = 4$ and $l = 4$ are the following five:

*   $(l, r) = (4, 11)$
*   $(l, r) = (4, 12)$
*   $(l, r) = (4, 13)$
*   $(l, r) = (4, 16)$
*   $(l, r) = (4, 17)$

Among these, the smallest $r$ is $11$, so print $4$ and $11$.
Input #2
11
856977192 996441446
298251737 935869360
396653206 658841528
710569907 929136831
325371222 425309117
379628374 697340458
835681913 939343451
140179224 887672320
375607390 611397526
93530028 581033295
249611310 775998537
Output #2
396653207 887672321
API Response (JSON)
{
  "problem": {
    "name": "InterSections",
    "description": {
      "content": "You are given $N$ intervals numbered $1$ to $N$. Interval $i$ is $[L_i, R_i]$. Two intervals $[l_a, r_a]$ and $[l_b, r_b]$ are said to **intersect** if and only if they satisfy either $(l_a < l_b < r_",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc360_f"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given $N$ intervals numbered $1$ to $N$. Interval $i$ is $[L_i, R_i]$.\nTwo intervals $[l_a, r_a]$ and $[l_b, r_b]$ are said to **intersect** if and only if they satisfy either $(l_a < l_b < r_...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments