淘汰赛AI (CI翻译)
Bessie is making a show called Moojutsu Cowsen. For one episode, she invites $n$ sorcerers and lines them up from left to right. The initial skill level of the $i$-th sorcerer is $a_i$.
Bessie 正在制作一档名为 Moojutsu Cowsen 的节目。在其中一期节目中,她邀请了 $n$ 名巫师,并将他们从左到右排成一列。第 $i$ 名巫师的初始能力值为 $a_i$。AI (CI翻译)
The sorcerers compete in a king-of-the-hill tournament. The leftmost remaining sorcerer starts as the champion, and his current skill is equal to his initial skill.
这些巫师将进行一场“擂主赛”。最左边的幸存巫师首先成为擂主,其当前能力值等于他的初始能力值。AI (CI翻译)
Then, the champion faces each remaining sorcerer to his right, one by one. Suppose the current champion has skill $s$, and the next sorcerer has skill $x$.
接下来,擂主依次与右侧的每一名幸存巫师进行对决。假设当前擂主的能力值为 $s$,下一名巫师的能力值为 $x$。AI (CI翻译)
If $s \lt x$, the champion forfeits. The next sorcerer becomes the new champion with skill $x$.
如果 $s \lt x$,擂主将弃权。下一名巫师成为新的擂主,其能力值为 $x$。AI (CI翻译)
Otherwise, the champion fights and wins (the champion still wins when $s = x$). In this case, the champion's current skill becomes $s+x$.
否则,擂主进行战斗并获胜(当 $s = x$ 时,擂主仍然获胜)。此时,擂主的当前能力值变为 $s+x$。AI (CI翻译)
Bessie finds forfeits boring, so before running the tournament, she may remove some sorcerers from the lineup.
Bessie 觉得弃权很无聊,因此在比赛开始前,她可以从队列中移除一些巫师。AI (CI翻译)
You are given a permutation $p_1,p_2,\ldots,p_n$ of the integers from $1$ to $n$. For each $0 \le i \le n-1$, Bessie removes sorcerers $p_1,p_2,\ldots,p_i$ from the lineup. If $i=0$, no sorcerers are removed. The relative order of all remaining sorcerers does not change.
给定一个由 $1$ 到 $n$ 组成的排列 $p_1,p_2,\ldots,p_n$。对于每个 $0 \le i \le n-1$,Bessie 都会从队列中移除巫师 $p_1,p_2,\ldots,p_i$。当 $i=0$ 时,不移除任何巫师。所有剩余巫师的相对顺序保持不变。AI (CI翻译)
For each such $i$, determine how many forfeits happen when Bessie runs the tournament using only the remaining sorcerers.
对于每个这样的 $i$,请确定 Bessie 仅使用剩余巫师进行比赛时会发生多少次弃权。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 a single integer $n$ ($1 \le n \le 2\cdot 10^5$).
每个测试用例的第一行包含一个整数 $n$($1 \le n \le 2\cdot 10^5$)。AI (CI翻译)
The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$).
每个测试用例的第二行包含 $n$ 个整数 $a_1,a_2,\ldots,a_n$($1 \le a_i \le 10^9$)。AI (CI翻译)
The third line of each test case contains a permutation $p_1,p_2,\ldots,p_n$ of the integers from $1$ to $n$.
每个测试用例的第三行包含一个由 $1$ 到 $n$ 组成的排列 $p_1,p_2,\ldots,p_n$。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.
对于每个测试用例,输出 $n$ 个整数。AI (CI翻译)
The $i$-th integer should be the number of forfeits after removing sorcerers $p_1,p_2,\ldots,p_{i-1}$.
第 $i$ 个整数应为移除巫师 $p_1,p_2,\ldots,p_{i-1}$ 后发生的弃权次数。AI (CI翻译)
Note
说明
AI (CI翻译)For the first test case, the answers are $2,1,0,0$.
对于第一个测试用例,答案为 $2,1,0,0$。AI (CI翻译)
Before any removals, the array is $[1,2,4,3]$. The champion with skill $1$ forfeits against skill $2$, and then the champion with skill $2$ forfeits against skill $4$. The champion with skill $4$ defeats skill $3$, so there are $2$ forfeits.
在移除任何巫师之前,数组为 $[1,2,4,3]$。能力值为 $1$ 的擂主在与能力值为 $2$ 的巫师对决时弃权,随后能力值为 $2$ 的擂主在与能力值为 $4$ 的巫师对决时弃权。能力值为 $4$ 的擂主击败了能力值为 $3$ 的巫师,因此共发生了 $2$ 次弃权。AI (CI翻译)
After removing sorcerer $1$, the remaining array is $[2,4,3]$. The champion with skill $2$ forfeits against skill $4$, and then the champion with skill $4$ defeats skill $3$, so there is $1$ forfeit.
移除巫师 $1$ 后,剩余数组为 $[2,4,3]$。能力值为 $2$ 的擂主在与能力值为 $4$ 的巫师对决时弃权,随后能力值为 $4$ 的擂主击败了能力值为 $3$ 的巫师,因此发生了 $1$ 次弃权。AI (CI翻译)
After removing sorcerers $1$ and $2$, the remaining array is $[4,3]$. The champion defeats the only remaining sorcerer, so there are $0$ forfeits. After removing sorcerers $1$, $2$, and $3$, only one sorcerer remains, so there are also $0$ forfeits.
移除巫师 $1$ 和 $2$ 后,剩余数组为 $[4,3]$。擂主击败了唯一剩下的巫师,因此发生了 $0$ 次弃权。移除巫师 $1$、$2$ 和 $3$ 后,只剩下一名巫师,因此同样发生了 $0$ 次弃权。AI (CI翻译)
For the second test case, the answers are $1,0,2,1,0$.
对于第二个测试用例,答案为 $1,0,2,1,0$。AI (CI翻译)
Before any removals, the array is $[3,1,7,2,6]$. The champion with skill $3$ defeats skill $1$ and gains one skill point, then forfeits against skill $7$. After that, the champion defeats skills $2$ and $6$, so there is $1$ forfeit.
在移除任何巫师之前,数组为 $[3,1,7,2,6]$。能力值为 $3$ 的擂主击败了能力值为 $1$ 的巫师,能力值增加了 1 点;随后,他在与能力值为 $7$ 的巫师对决时弃权。之后,新的擂主击败了能力值为 $2$ 和 $6$ 的巫师,因此共发生了 $1$ 次弃权。AI (CI翻译)
After removing sorcerer $3$, whose skill is $7$, the remaining array is $[3,1,2,6]$. The champion defeats every remaining sorcerer, so there are $0$ forfeits.
移除能力值为 $7$ 的巫师 $3$ 后,剩余数组为 $[3,1,2,6]$。擂主击败了所有剩余巫师,因此发生了 $0$ 次弃权。AI (CI翻译)
After also removing sorcerer $1$, the remaining array is $[1,2,6]$ The champion with skill $1$ forfeits against skill $2$, and then the champion with skill $2$ forfeits against skill $6$, so there are $2$ forfeits.
再移除巫师 $1$ 后,剩余数组为 $[1,2,6]$。能力值为 $1$ 的擂主在与能力值为 $2$ 的巫师对决时弃权,随后能力值为 $2$ 的擂主在与能力值为 $6$ 的巫师对决时弃权,因此发生了 $2$ 次弃权。AI (CI翻译)
After also removing sorcerer $5$, the remaining array is $[1,2]$. There is $1$ forfeit. Finally, after also removing sorcerer $2$, only one sorcerer remains, so there are $0$ forfeits.
再移除巫师 $5$ 后,剩余数组为 $[1,2]$,发生了 $1$ 次弃权。最后,再移除巫师 $2$ 后,只剩下一名巫师,因此发生了 $0$ 次弃权。AI (CI翻译)
样例
3
4
1 2 4 3
1 2 3 4
5
3 1 7 2 6
3 1 5 2 4
6
10 1 2 20 3 4
4 1 2 3 5 6
2 1 0 0
1 0 2 1 0
1 0 3 2 1 0
题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。