CF1501A.Alexey and Train

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Alexey is travelling on a train. Unfortunately, due to the bad weather, the train moves slower that it should!

Alexey took the train at the railroad terminal. Let's say that the train starts from the terminal at the moment 00. Also, let's say that the train will visit nn stations numbered from 11 to nn along its way, and that Alexey destination is the station nn.

Alexey learned from the train schedule nn integer pairs (ai,bi)(a_i, b_i) where aia_i is the expected time of train's arrival at the ii-th station and bib_i is the expected time of departure.

Also, using all information he has, Alexey was able to calculate nn integers tm1,tm2,…,tmntm_1, tm_2, \dots, tm_n where tmitm_i is the extra time the train need to travel from the station i−1i - 1 to the station ii. Formally, the train needs exactly ai−bi−1+tmia_i - b_{i-1} + tm_i time to travel from station i−1i - 1 to station ii (if i=1i = 1 then b0b_0 is the moment the train leave the terminal, and it's equal to 00).

The train leaves the station ii, if both conditions are met:

  1. it's on the station for at least ⌈bi−ai2⌉\left\lceil \frac{b_i - a_i}{2} \right\rceil units of time (division with ceiling);
  2. current time ≥bi\ge b_i.

Since Alexey spent all his energy on prediction of time delays, help him to calculate the time of arrival at the station nn.

阿列克谢正在乘坐一列火车。不幸的是,由于天气恶劣,火车行驶速度比预期更慢!

阿列克谢在铁路终点站登上了火车。假设火车在时刻 00 从终点站出发。此外,假设火车沿途将依次停靠 nn 个车站,编号为 11 到 nn,而阿列克谢的目的地是第 nn 号车站。

阿列克谢查阅了列车时刻表,获得了 nn 个整数对 (ai,bi)(a_i, b_i),其中 aia_i 表示火车预计到达第 ii 个车站的时刻,bib_i 表示火车预计离开第 ii 个车站的时刻。

此外,凭借所掌握的所有信息,阿列克谢还计算出了 nn 个整数 tm1,tm2,…,tmntm_1, tm_2, \dots, tm_n,其中 tmitm_i 表示火车从第 i−1i-1 号车站行驶至第 ii 号车站所需的额外时间。形式化地说,火车从第 i−1i-1 号车站到第 ii 号车站实际所需时间为 ai−bi−1+tmia_i - b_{i-1} + tm_i(当 i=1i = 1 时,b0b_0 表示火车离开终点站的时刻,其值为 00)。

火车离开第 ii 号车站需同时满足以下两个条件:

  1. 火车在该站停留时间至少为 ⌈bi−ai2⌉\left\lceil \frac{b_i - a_i}{2} \right\rceil 个单位时间(向上取整除法);
  2. 当前时刻 ≥bi\ge b_i。

由于阿列克谢已将全部精力用于预测延误时间,请你帮他计算他抵达第 nn 号车站的时刻。

输入格式

The first line contains one integer tt (1≤t≤1001 \le t \le 100) — the number of test cases.

The first line of each test case contains the single integer nn (1≤n≤1001 \le n \le 100) — the number of stations.

Next nn lines contain two integers each: aia_i and bib_i (1≤ai<bi≤1061 \le a_i \lt b_i \le 10^6). It's guaranteed that bi<ai+1b_i \lt a_{i+1}.

Next line contains nn integers tm1,tm2,…,tmntm_1, tm_2, \dots, tm_n (0≤tmi≤1060 \le tm_i \le 10^6).

第一行包含一个整数 tt(1≤t≤1001 \le t \le 100)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤1001 \le n \le 100)—— 车站的数量。

接下来的 nn 行每行包含两个整数:aia_i 和 bib_i(1≤ai<bi≤1061 \le a_i \lt b_i \le 10^6)。保证 bi<ai+1b_i \lt a_{i+1}。

下一行包含 nn 个整数 tm1,tm2,…,tmntm_1, tm_2, \dots, tm_n(0≤tmi≤1060 \le tm_i \le 10^6)。

输出格式

For each test case, print one integer — the time of Alexey's arrival at the last station.

对于每个测试用例,输出一个整数——Alexey 到达最后一个车站的时间。

输入输出样例

  • 输入#1

    2
    2
    2 4
    10 12
    0 2
    5
    1 4
    7 8
    9 10
    13 15
    19 20
    1 2 3 4 5

    输出#1

    12
    32

说明/提示

In the first test case, Alexey arrives at station 11 without any delay at the moment a1=2a_1 = 2 (since tm1=0tm_1 = 0). After that, he departs at moment b1=4b_1 = 4. Finally, he arrives at station 22 with tm2=2tm_2 = 2 extra time, or at the moment 1212.

In the second test case, Alexey arrives at the first station with tm1=1tm_1 = 1 extra time, or at moment 22. The train, from one side, should stay at the station at least ⌈b1−a12⌉=2\left\lceil \frac{b_1 - a_1}{2} \right\rceil = 2 units of time and from the other side should depart not earlier than at moment b1=4b_1 = 4. As a result, the trains departs right at the moment 44.

Using the same logic, we can figure out that the train arrives at the second station at the moment 99 and departs at the moment 1010; at the third station: arrives at 1414 and departs at 1515; at the fourth: arrives at 2222 and departs at 2323. And, finally, arrives at the fifth station at 3232.

在第一个测试用例中,Alexey 在时刻 a1=2a_1 = 2(因为 tm1=0tm_1 = 0)准时抵达第 1 号车站。随后,他在时刻 b1=4b_1 = 4 离开该站。最终,他于时刻 1212 抵达第 2 号车站,比原计划多花了 tm2=2tm_2 = 2 单位时间。

在第二个测试用例中,Alexey 比原计划多花 tm1=1tm_1 = 1 单位时间抵达第一站,即在时刻 22 到达。列车一方面需在该站至少停留 ⌈b1−a12⌉=2\left\lceil \frac{b_1 - a_1}{2} \right\rceil = 2 单位时间,另一方面不得早于时刻 b1=4b_1 = 4 离站。因此,列车恰好于时刻 44 离站。

依同样逻辑可推得:列车于时刻 99 抵达第二站,并于时刻 1010 离开;于时刻 1414 抵达第三站,并于时刻 1515 离开;于时刻 2222 抵达第四站,并于时刻 2323 离开;最终于时刻 3232 抵达第五站。

输入解题思路,AI测评打分。不知道怎么写?

首页