#P9691. [GDCPC2023] Base Station Construction

[GDCPC2023] Base Station Construction

题目描述

China Mobile Shenzhen Branch was registered in 19991999. Four years later, Guangdong Collegiate Programming Contest was held for the first time. China Mobile Shenzhen Branch, along with Guangdong Collegiate Programming Contest, witnesses the prosperity and development of the computer industry in Guangdong.

During the construction of a communication line, it is critical to carefully choose the locations for base stations. The distance from west to east of a city is nn kilometers. The engineers have investigated the cost to build a base station at 1,2,,n1, 2, \cdots, n kilometers from west to east, which are a1,a2,,ana_1, a_2, \cdots, a_n respectively.

To ensure communication quality for the residents, the locations of base stations also need to meet mm requirements. The ii-th requirement can be represented as a pair of integers lil_i and rir_i (1lirin1 \le l_i \le r_i \le n), indicating that there must be at least 11 base station between lil_i kilometers and rir_i kilometers (both inclusive) from west to east.

As the chief engineer, you need to decide the number of base stations to build and their locations, and finally calculate the minimum total cost to satisfy all requirements.

输入格式

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains an integer nn (1n5×1051 \le n \le 5 \times 10^5) indicating the distance from west to east of the city.

The second line contains nn integers a1,a2,,ana_1, a_2, \cdots, a_n (1ai1091 \le a_i \le 10^9) where aia_i indicates the cost to build a base station at ii kilometers from west to east.

The third line contains an integer mm (1m5×1051 \le m \le 5 \times 10^5) indicating the number of requirements.

For the following mm lines, the ii-th line contains two integers lil_i and rir_i (1lirin1 \le l_i \le r_i \le n) indicating that there must be at least 11 base station between lil_i kilometers and rir_i kilometers (both inclusive) from west to east.

It's guaranteed that neither the sum of nn nor the sum of mm of all test cases will exceed 5×1055 \times 10^5.

输出格式

For each test case output one line containing one integer indicating the minimum total cost to satisfy all requirements.

题目大意

【题目描述】

中国移动通信集团广东有限公司深圳分公司(以下简称深圳移动)于 19991999 年正式注册。四年后,广东省大学生程序设计竞赛第一次举办。深圳移动与广东省大学生程序设计竞赛一起见证了广东省计算机行业的兴旺与发展。

在建设通信线路的过程中,信号基站的选址是一个非常关键的问题。某城市从西到东的距离为 nn 千米,工程师们已经考察了在从西往东 1,2,,n1, 2, \cdots, n 千米的位置建设基站的成本,分别是 a1,a2,,ana_1, a_2, \cdots, a_n

为了保证居民的通信质量,基站的选址还需要满足 mm 条需求。第 ii 条需求可以用一对整数 lil_irir_i 表示(1lirin1 \le l_i \le r_i \le n),代表从西往东 lil_i 千米到 rir_i 千米的位置之间(含两端)至少需要建设 11 座基站。

作为总工程师,您需要决定基站的数量与位置,并计算满足所有需求的最小总成本。

【输入格式】

有多组测试数据。第一行输入一个整数 TT 表示测试数据组数,对于每组测试数据:

第一行输入一个整数 nn1n5×1051 \le n \le 5 \times 10^5)表示城市从西到东的距离。

第二行输入 nn 个整数 a1,a2,,ana_1, a_2, \cdots, a_n1ai1091 \le a_i \le 10^9),其中 aia_i 表示在从西往东 ii 千米的位置建设基站的成本。

第三行输入一个整数 mm1m5×1051 \le m \le 5 \times 10^5)表示需求的数量。

对于接下来 mm 行,第 ii 行输入两个整数 lil_irir_i1lirin1 \le l_i \le r_i \le n)表示从西往东 lil_i 千米到 rir_i 千米的位置之间(含两端)至少需要建设 11 座基站。

保证所有数据 nn 之和与 mm 之和均不超过 5×1055 \times 10^5

【输出格式】

每组数据输出一行一个整数,表示满足所有需求的最小总成本。

【样例解释】

对于第一组样例数据,最优方案是在从西往东 22 千米和 55 千米的位置建设基站。总成本为 2+100=1022 + 100 = 102

对于第二组样例数据,最优方案是在从西往东 22 千米和 44 千米的位置建设基站。总成本为 3+2=53 + 2 = 5

2
5
3 2 4 1 100
3
1 3
2 4
5 5
5
7 3 4 2 2
3
1 4
2 3
4 5
102
5

提示

For the first sample test case the optimal solution is to build base stations at 22 kilometers and 55 kilometers from west to east. The total cost is 2+100=1022 + 100 = 102.

For the second sample test case the optimal solution is to build base stations at 22 kilometers and 44 kilometers from west to east. The total cost is 3+2=53 + 2 = 5.