QOJ.ac

QOJ

Time Limit: 4 s Memory Limit: 256 MB Total points: 100

#10969. 包裹提取

Statistics

Farmer John 在数轴上以一种奇特的方式分布了奶牛和包裹,过程如下:

  • Farmer John 选择一个数 $M$ ($1 \le M \le 10^{18}$)。
  • Farmer John 选择 $N$ ($1 \le N \le 2 \cdot 10^4$) 个区间 $[L_i, R_i]$ 来放置奶牛 ($1 \le L_i \le R_i \le 10^{18}$)。他在位置 $L_i, L_i + M, L_i + 2M, \ldots, R_i$ 处各放置一头奶牛。保证 $R_i - L_i$ 是 $M$ 的倍数。
  • Farmer John 选择 $P$ ($1 \le P \le 2 \cdot 10^4$) 个区间 $[A_i, B_i]$ 来放置包裹 ($1 \le A_i \le B_i \le 10^{18}$)。他在位置 $A_i, A_i + M, A_i + 2M, \ldots, B_i$ 处各放置一个包裹。保证 $B_i - A_i$ 是 $M$ 的倍数。

奶牛和包裹分布完成后,Farmer John 想知道奶牛拾取所有包裹需要多长时间。每一秒,Farmer John 可以通过对讲机向某头奶牛发出指令,使其向左或向右移动一个单位距离。如果奶牛移动到了包裹所在的位置,它就能拾取该包裹。Farmer John 想知道奶牛拾取所有包裹所需的最短时间(以秒为单位)。

输入格式

第一行包含 $M, N, P$。接下来的 $N$ 行,每行包含两个整数 $L_i$ 和 $R_i$。接下来的 $P$ 行,每行包含两个整数 $A_i$ 和 $B_i$。

输出格式

输出一个整数,表示在每一秒只能向一头奶牛发出一个向左/向右指令的情况下,奶牛拾取所有包裹所需的最短时间。

样例

样例输入 1

100 3 7
10 10
20 20
30 30
7 7
11 11
13 13
17 17
24 24
26 26
33 33

样例输出 1

22

说明 1

在上述测试用例中,假设奶牛和包裹均从左到右编号。Farmer John 可以按照以下步骤在 22 秒内拾取所有包裹:

  • 向奶牛 1 发出 3 次向左指令,使其拾取包裹 1
  • 向奶牛 3 发出 3 次向右指令,使其拾取包裹 7
  • 向奶牛 2 发出 4 次向右指令,使其拾取包裹 5
  • 向奶牛 1 发出 10 次向右指令,使其拾取包裹 2、3 和 4
  • 向奶牛 2 发出 2 次向右指令,使其拾取包裹 6

样例输入 2

2 1 1
1 5
2 6

样例输出 2

3

说明 2

有三头奶牛和三个包裹。Farmer John 可以向每头奶牛发出一次向右指令。

数据范围

  • 测试点 3-4:保证奶牛和包裹的总数不超过 $2 \cdot 10^5$。
  • 测试点 5-10:保证 $N, P \le 500$。
  • 测试点 11-13:保证没有任何奶牛区间或包裹区间相交。
  • 测试点 14-20:无附加限制。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.