CF1784C.Monsters (hard version)
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is the hard version of the problem. In this version, you need to find the answer for every prefix of the monster array.
In a computer game, you are fighting against n monsters. Monster number i has ai health points, all ai are integers. A monster is alive while it has at least 1 health point.
You can cast spells of two types:
- Deal 1 damage to any single alive monster of your choice.
- Deal 1 damage to all alive monsters. If at least one monster dies (ends up with 0 health points) as a result of this action, then repeat it (and keep repeating while at least one monster dies every time).
Dealing 1 damage to a monster reduces its health by 1 .
Spells of type 1 can be cast any number of times, while a spell of type 2 can be cast at most once during the game.
For every k=1,2,…,n , answer the following question. Suppose that only the first k monsters, with numbers 1,2,…,k , are present in the game. What is the smallest number of times you need to cast spells of type 1 to kill all k monsters?
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t ( 1≤t≤104 ). The description of the test cases follows.
Each test case consists of two lines. The first line contains a single integer n ( 1≤n≤2⋅105 ) — the number of monsters.
The second line contains n integers a1,a2,…,an ( 1≤ai≤n ) — monsters' health points.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 .
输出格式
For each test case, print n integers. The k -th of these integers must be equal to the smallest number of times you need to cast spells of type 1 to kill all k monsters, if only monsters with numbers 1,2,…,k are present in the game.
输入输出样例
输入#1
2 3 3 1 2 6 4 1 5 4 1 1
输出#1
2 1 0 3 2 4 4 4 4
说明/提示
In the first test case, for k=n , the initial health points of the monsters are [3,1,2] . It is enough to cast a spell of type 2:
- Monsters' health points change to [2,0,1] . Since monster number 2 dies, the spell is repeated.
- Monsters' health points change to [1,0,0] . Since monster number 3 dies, the spell is repeated.
- Monsters' health points change to [0,0,0] . Since monster number 1 dies, the spell is repeated.
- Monsters' health points change to [0,0,0] .
Since it is possible to use no spells of type 1 at all, the answer is 0 .
In the second test case, for k=n , the initial health points of the monsters are [4,1,5,4,1,1] . Here is one of the optimal action sequences:
- Using a spell of type 1, deal 1 damage to monster number 1 . Monsters' health points change to [3,1,5,4,1,1] .
- Using a spell of type 1, deal 1 damage to monster number 4 . Monsters' health points change to [3,1,5,3,1,1] .
- Using a spell of type 1, deal 1 damage to monster number 4 again. Monsters' health points change to [3,1,5,2,1,1] .
- Use a spell of type 2:
- Monsters' health points change to [2,0,4,1,0,0] . Since monsters number 2 , 5 , and 6 die, the spell is repeated.
- Monsters' health points change to [1,0,3,0,0,0] . Since monster number 4 dies, the spell is repeated.
- Monsters' health points change to [0,0,2,0,0,0] . Since monster number 1 dies, the spell is repeated.
- Monsters' health points change to [0,0,1,0,0,0] .
- Using a spell of type 1, deal 1 damage to monster number 3 . Monsters' health points change to [0,0,0,0,0,0] .
Spells of type 1 are cast 4 times in total. It can be shown that this is the smallest possible number.