QOJ.ac

QOJ

時間限制: 6 s 記憶體限制: 256 MB 總分: 100

#4488. 购买手办

统计

题目描述

在“堇庭华彩”活动期间,作为宫司大人雇佣的专业人士,早柚被指派去购买一个手办,即“雷电将军御前决斗手办”。

共有 $n$ 个人(编号从 $1$ 到 $n$)打算购买手办,商店设有 $m$ 个窗口,队列编号从 $1$ 到 $m$。第 $i$ 个人到达的时间为 $a_i$,购买手办所需的时间为 $s_i$(保证每个人的到达时间 $a_i$ 互不相同)。当一个人到达商店时,他会选择当前排队人数最少的队列。如果存在多个排队人数最少的队列,他会选择编号最小的那个。需要注意的是,如果有人在同一时刻离开队列,该人会在所有人离开后进行选择。

早柚从昨晚就一直在这里等着买手办。但在漫长的等待后,她的眼睛开始变得昏昏欲睡……并睡着了。如果早柚买不到这些手办,天领奉行的天狗就会把她关起来一辈子!商店会在这些人买完手办后关闭,这意味着她必须在最后一个人离开之前醒来。现在宫司大人想知道早柚最晚能在什么时间醒来。

例如,有两个人排在同一条队伍中,$a_1 = 1, s_1 = 2, a_2 = 2, s_2 = 2$。当第一个人到达时,队伍中没有人,所以购买手办的开始时间和结束时间分别为 $1$ 和 $3$。当第二个人到达时,第一个人还在排队,所以购买手办的开始时间和结束时间分别为 $3$ 和 $5$。如果最后一个人的结束时间是 $x$,则答案为 $x$。

输入格式

第一行包含一个整数 $T$ ($1 \le T \le 10$)。

每个测试用例的第一行包含两个正整数 $n$ 和 $m$ ($1 \le n \le 2 \times 10^5, 1 \le m \le 2 \times 10^5$),分别表示人数和队列数。

接下来 $n$ 行,每行包含两个整数 $a_i$ 和 $s_i$ ($1 \le a_i, s_i \le 10^9$),表示第 $i$ 个人的到达时间和购买耗时。

保证所有测试用例中 $n$ 的总和不超过 $2 \times 10^6$,且 $m$ 的总和不超过 $2 \times 10^6$。

输出格式

对于每个测试用例,输出一行,包含一个整数,即早柚最晚醒来的时间,也就是最后一个人离开的时间。

样例

输入 1

1
5 3
2 4
1 3
5 1
3 4
4 2

输出 1

7

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.