#P9579. 「Cfz Round 1」Elevator

    ID: 10641 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划 DP贪心线段树树状数组洛谷原创O2优化排序洛谷月赛

「Cfz Round 1」Elevator

Background

An elevator is a space that gives people plenty of time to think.

Problem Description

Given two arrays a,ba, b of length nn. We say a sequence pp is valid. Let the length of pp be mm. The sequence pp is valid if and only if:

  • p1=1p_1 = 1.
  • For all 1i<m1 \le i < m, we have pipi+1=1|p_i - p_{i+1}| = 1.
  • For all 1kn1 \le k \le n, there exists an ordered pair (i,j)(i, j) such that 1i<jm1 \le i < j \le m, pi=akp_i = a_k, and pj=bkp_j = b_k.

You need to output, among all valid sequences pp, the minimum possible length of pp.

Input Format

The first line contains an integer nn.

The next nn lines each contain two integers ai,bia_i, b_i.

Output Format

Output one integer, the minimum length of pp among all valid sequences pp.

2
3 2
2 5
7
4
4 7
10 8
9 11
4 2
18

Hint

[Sample Explanation #1]

The minimum length of pp is 77. One such sequence pp is {1,2,3,2,3,4,5}\{1,2,3,2,3,4,5\}.

[Constraints]

For all testdata, 1n5×1051 \le n \le 5\times10^5, 1ai,bi1091 \le a_i, b_i \le 10^9, and it is guaranteed that aibia_i \neq b_i.

This problem uses bundled testdata.

Subtask ID Points nn \le Special Property
11 99 11 None
22 5×1055\times10^5 Guaranteed ai<bia_i < b_i
33 2121 ai,bia_i, b_i are generated uniformly at random in [1,109][1,10^9]
44 2727 20002000 None
55 3434 5×1055\times10^5

Translated by ChatGPT 5