Codeforces Round 1102 (Div. 2)
F
容器、高度与两个版本(困难版)
Vessels, Heights and Two Versions (Hard Version)
Codeforces
2000
原题
← EG →

容器、高度与两个版本(困难版)AI (CI翻译)

This is the hard version of the problem. The difference between the versions is that in this version, constraints on $n$ and on the number of test cases are higher. You can hack only if you solved all versions of this problem.

这是本题的困难版。两个版本的区别在于,本版本中 $n$ 的约束以及测试用例数量的约束更高。只有在解决了本题的所有版本后才能进行 hack。AI (CI翻译)

There are $n$ communicating vessels of infinite height arranged in a circle. The base area of each vessel is $1$ cm$^2$, and between the $i$-th vessel and the $(i \bmod n) + 1$-th vessel there is a connection of negligible volume at height $h_i$ cm. For each vessel $i$, find the maximum total volume of water in cm$^3$ that can be placed in these vessels under the condition that the $i$-th vessel remains empty.

有 $n$ 个高度无限的连通容器,它们沿圆周排列。每个容器的底面积为 $1$ cm$^2$,第 $i$ 个容器与第 $(i \bmod n) + 1$ 个容器之间,在高度 $h_i$ cm 处有一条体积可忽略不计的连接。对于每个容器 $i$,求在保证第 $i$ 个容器为空的条件下,能够放入这些容器中的水的最大总体积(单位为 cm$^3$)。AI (CI翻译)

Formally, you are given an array $h_1, h_2, \ldots, h_n$. A cyclic array of non-negative integers $w_1, w_2, \ldots, w_n$ is called good if the following holds:

形式化地说,给定一个数组 $h_1, h_2, \ldots, h_n$。如果一个由非负整数组成的循环数组 $w_1, w_2, \ldots, w_n$ 满足以下条件,则称其为好的数组:AI (CI翻译)

  • For every $i$ from $1$ to $n$, if $\max(w_i, w_{i \bmod n + 1}) \gt h_i$, then $w_i = w_{i \bmod n + 1}$. In other words, if the maximum of two neighboring elements of the array $w$ exceeds the corresponding element of the array $h$, then these two neighboring elements of the array $w$ must be equal.
  • 对于每个 $i$($1$ 到 $n$),如果 $\max(w_i, w_{i \bmod n + 1}) \gt h_i$,那么 $w_i = w_{i \bmod n + 1}$。换句话说,如果数组 $w$ 中两个相邻元素的最大值超过数组 $h$ 中对应的元素,那么这两个相邻元素必须相等。
AI (CI翻译)

For each $i$ from $1$ to $n$, output the maximum possible sum $w_1 + w_2 + \ldots + w_n$ among all good arrays $w_1, w_2, \ldots, w_n$, under the condition that $w_i = 0$.

对于每个 $i$($1$ 到 $n$),在满足 $w_i = 0$ 的条件下,求所有好的数组 $w_1, w_2, \ldots, w_n$ 中 $w_1 + w_2 + \ldots + w_n$ 的最大可能值。AI (CI翻译)

Input

输入格式

AI (CI翻译)

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

每个测试包含多个测试用例。第一行包含测试用例的数量 $t$($1 \le t \le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)

The first line of each test case contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of vessels.

每个测试用例的第一行包含一个整数 $n$($3 \le n \le 2 \cdot 10^5$),表示容器的数量。AI (CI翻译)

The second line of each test case contains $n$ integers $h_1, h_2, \ldots, h_n$ ($1 \le h_i \le 10^9$) — the heights of the partitions between the vessels.

每个测试用例的第二行包含 $n$ 个整数 $h_1, h_2, \ldots, h_n$($1 \le h_i \le 10^9$),表示容器之间隔板的高度。AI (CI翻译)

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

保证所有测试用例的 $n$ 之和不超过 $2 \cdot 10^5$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, output $n$ integers — the $l$-th integer means the maximum total volume of water in cm$^3$ in the vessels under the condition that the $l$-th vessel remains empty.

对于每个测试用例,输出 $n$ 个整数,其中第 $l$ 个整数表示在保证第 $l$ 个容器为空的条件下,这些容器中水的最大总体积(单位为 cm$^3$)。AI (CI翻译)

Note

说明

AI (CI翻译)

Consider the first test case.

考虑第一个测试用例。AI (CI翻译)

  • To keep vessel $1$ empty, one good array is $w = [0, 1, 2, 3]$, with a total of $6$ cm$^3$ of water.
  • To keep vessel $2$ empty, one good array is $w = [1, 0, 2, 3]$, with a total of $6$ cm$^3$ of water.
  • To keep vessel $3$ empty, one good array is $w = [2, 2, 0, 3]$, with a total of $7$ cm$^3$ of water.
  • To keep vessel $4$ empty, one good array is $w = [3, 3, 3, 0]$, with a total of $9$ cm$^3$ of water.
  • 为了让容器 $1$ 为空,一个好的数组是 $w = [0, 1, 2, 3]$,水的总体积为 $6$ cm$^3$。
  • 为了让容器 $2$ 为空,一个好的数组是 $w = [1, 0, 2, 3]$,水的总体积为 $6$ cm$^3$。
  • 为了让容器 $3$ 为空,一个好的数组是 $w = [2, 2, 0, 3]$,水的总体积为 $7$ cm$^3$。
  • 为了让容器 $4$ 为空,一个好的数组是 $w = [3, 3, 3, 0]$,水的总体积为 $9$ cm$^3$。
AI (CI翻译)

For example, the array $w = [2, 2, 0, 4]$ is not good, because $\max(w_3, w_4) \gt h_3$, and therefore $w_3 = w_4$ must hold.

例如,数组 $w = [2, 2, 0, 4]$ 不是好的数组,因为 $\max(w_3, w_4) \gt h_3$,因此必须满足 $w_3 = w_4$。AI (CI翻译)

It can be shown that each of the arrays above has the maximum possible sum among all suitable options.

可以证明,上述每个数组在所有符合条件的方案中都取得了最大可能的总和。AI (CI翻译)

样例

Input #1
4
4
1 2 3 4
5
5 3 1 5 2
6
3 4 2 6 1 5
7
1 2 1 4 2 3 5
Output #1
6 6 7 9
17 16 14 14 17
21 21 20 20 21 21
17 17 17 17 21 21 22
InputCopy OutputCopy
题目信息
题号
2234F
时间限制
2 s
内存限制
256 MB
标签
数据结构
DFS 及类似
并查集
贪心
模拟
双指针

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。