CF1799D2.Hot Start Up (hard version)

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

This is a hard version of the problem. The constraints of tt, nn, kk are the only difference between versions.

You have a device with two CPUs. You also have kk programs, numbered 11 through kk, that you can run on the CPUs.

The ii-th program (1≤i≤k1 \le i \le k) takes coldicold_i seconds to run on some CPU. However, if the last program we ran on this CPU was also program ii, it only takes hotihot_i seconds (hoti≤coldihot_i \le cold_i). Note that this only applies if we run program ii multiple times consecutively — if we run program ii, then some different program, then program ii again, it will take coldicold_i seconds the second time.

You are given a sequence a1,a2,…,ana_1, a_2, \ldots, a_n of length nn, consisting of integers from 11 to kk. You need to use your device to run programs a1,a2,…,ana_1, a_2, \ldots, a_n in sequence. For all 2≤i≤n2 \le i \le n, you cannot start running program aia_i until program ai−1a_{i - 1} has completed.

Find the minimum amount of time needed to run all programs a1,a2,…,ana_1, a_2, \ldots, a_n in sequence.

这是一个该问题的困难版本。两个版本之间唯一的区别在于 tt、nn、kk 的约束条件。

你拥有一台配备两个 CPU 的设备。此外,你还有 kk 个编号为 11 至 kk 的程序,可在 CPU 上运行。

第 ii 个程序(1≤i≤k1 \le i \le k)在某个 CPU 上运行需要 coldicold_i 秒。然而,若该 CPU 上次运行的程序也是程序 ii,则本次运行仅需 hotihot_i 秒(满足 hoti≤coldihot_i \le cold_i)。注意,该优化仅适用于程序 ii 在同一 CPU 上被连续多次运行的情形——例如,若先运行程序 ii,再运行某个其他程序,之后再次运行程序 ii,则第二次运行仍需 coldicold_i 秒。

现给定一个长度为 nn 的序列 a1,a2,…,ana_1, a_2, \ldots, a_n,其中每个元素均为 11 至 kk 之间的整数。你需要使用该设备按顺序依次运行程序 a1,a2,…,ana_1, a_2, \ldots, a_n。对所有 2≤i≤n2 \le i \le n,程序 aia_i 的运行必须在程序 ai−1a_{i-1} 完成后才能开始。

求按顺序运行全部程序 a1,a2,…,ana_1, a_2, \ldots, a_n 所需的最短总时间。

输入格式

Input consists of multiple test cases. The first line contains a single integer tt, the number of test cases (1≤t≤1051 \le t \le 10^5).

The first line of each test case contains nn and kk (1≤n,k≤3⋅1051 \le n, k \le 3 \cdot 10^5).

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤k1 \le a_i \le k).

The third line of each test case contains kk integers cold1,cold2,…,coldkcold_1, cold_2, \ldots, cold_k (1≤coldi≤1091 \le cold_i \le 10^9).

The fourth line of each test case contains kk integers hot1,hot2,…,hotkhot_1, hot_2, \ldots, hot_k (1≤hoti≤coldi1 \le hot_i \le cold_i).

It is guaranteed the sum of nn and the sum of kk over all test cases do not exceed 3⋅1053 \cdot 10^5.

输入包含多个测试用例。第一行包含一个整数 tt,表示测试用例的数量(1≤t≤1051 \le t \le 10^5)。

每个测试用例的第一行包含两个整数 nn 和 kk(1≤n,k≤3⋅1051 \le n, k \le 3 \cdot 10^5)。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤k1 \le a_i \le k)。

每个测试用例的第三行包含 kk 个整数 cold1,cold2,…,coldkcold_1, cold_2, \ldots, cold_k(1≤coldi≤1091 \le cold_i \le 10^9)。

每个测试用例的第四行包含 kk 个整数 hot1,hot2,…,hotkhot_1, hot_2, \ldots, hot_k(1≤hoti≤coldi1 \le hot_i \le cold_i)。

保证所有测试用例中 nn 的总和与 kk 的总和均不超过 3⋅1053 \cdot 10^5。

输出格式

For each test case, print the minimum time needed to run all programs in the given order.

对于每个测试用例,输出按给定顺序运行所有程序所需的最短时间。

输入输出样例

  • 输入#1

    9
    3 2
    1 2 2
    3 2
    2 1
    4 2
    1 2 1 2
    5 3
    2 1
    4 3
    1 2 3 1
    100 100 100
    1 1 1
    5 2
    2 1 2 1 1
    65 45
    54 7
    5 3
    1 3 2 1 2
    2 2 2
    1 1 1
    5 1
    1 1 1 1 1
    1000000000
    999999999
    5 6
    1 6 1 4 1
    3 6 4 1 4 5
    1 1 1 1 4 1
    1 3
    3
    4 5 6
    1 2 3
    8 3
    3 3 3 1 2 3 2 1
    10 10 8
    10 10 5

    输出#1

    6
    11
    301
    225
    8
    4999999996
    11
    6
    63

说明/提示

In the first test case, we can do the following:

  • Run program a1=1a_1 = 1 on CPU 11. It takes cold1=3cold_1 = 3 seconds to run.
  • Run program a2=2a_2 = 2 on CPU 22. It takes cold2=2cold_2 = 2 seconds to run.
  • Run program a3=2a_3 = 2 on CPU 22. The last program run on this CPU was also program 22, so it takes hot2=1hot_2 = 1 second to run.

In total, we need 3+2+1=63 + 2 + 1 = 6 seconds to run them all. We can show this is optimal.

In the second test case, we can use do the following:

  • Run program a1=1a_1 = 1 on CPU 11. It takes cold1=5cold_1 = 5 seconds to run.
  • Run program a2=2a_2 = 2 on CPU 22. It takes cold2=3cold_2 = 3 seconds to run.
  • Run program a3=1a_3 = 1 on CPU 11. The last program run on this CPU was also program 11, so it takes hot1=2hot_1 = 2 seconds to run.
  • Run program a4=2a_4 = 2 on CPU 22. The last program run on this CPU was also program 22, so it takes hot2=1hot_2 = 1 second to run.

In total, we need 5+3+2+1=115 + 3 + 2 + 1 = 11 seconds. We can show this is optimal.

在第一个测试用例中,我们可以执行以下操作:

  • 在 CPU 11 上运行程序 a1=1a_1 = 1。其运行耗时为 cold1=3cold_1 = 3 秒。
  • 在 CPU 22 上运行程序 a2=2a_2 = 2。其运行耗时为 cold2=2cold_2 = 2 秒。
  • 在 CPU 22 上运行程序 a3=2a_3 = 2。该 CPU 上次运行的程序也是程序 22,因此其运行耗时为 hot2=1hot_2 = 1 秒。

总计需要 3+2+1=63 + 2 + 1 = 6 秒来完成全部运行。可以证明这是最优方案。

在第二个测试用例中,我们可以执行以下操作:

  • 在 CPU 11 上运行程序 a1=1a_1 = 1。其运行耗时为 cold1=5cold_1 = 5 秒。
  • 在 CPU 22 上运行程序 a2=2a_2 = 2。其运行耗时为 cold2=3cold_2 = 3 秒。
  • 在 CPU 11 上运行程序 a3=1a_3 = 1。该 CPU 上次运行的程序也是程序 11,因此其运行耗时为 hot1=2hot_1 = 2 秒。
  • 在 CPU 22 上运行程序 a4=2a_4 = 2。该 CPU 上次运行的程序也是程序 22,因此其运行耗时为 hot2=1hot_2 = 1 秒。

总计需要 5+3+2+1=115 + 3 + 2 + 1 = 11 秒。可以证明这是最优方案。

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

首页