luogu#P9883. [EC Final 2021] Fenwick Tree
[EC Final 2021] Fenwick Tree
题目描述
Prof. Pang is giving a lecture on the Fenwick tree (also called binary indexed tree).
In a Fenwick tree, we have an array of length which is initially all-zero ( for any ). Each time, Prof. Pang can call the following procedure for some position () and value :
def update(pos, val):
while (pos <= n):
c[pos] += val
pos += pos & (-pos)
Note that pos & (-pos)
equals to the maximum power of that divides pos
for any positive integer pos
.
In the procedure, can be any real number. After calling it some (zero or more) times, Prof. Pang forgets the exact values in the array . He only remembers whether is zero or not for each from to . Prof. Pang wants to know what is the minimum possible number of times he called the procedure assuming his memory is accurate.
输入格式
The first line contains a single integer denoting the number of test cases.
For each test case, the first line contains an integer . The next line contains a string of length . The -th character of the string is 1
if is nonzero and 0
otherwise.
It is guaranteed that the sum of over all test cases is no more than .
输出格式
For each test case, output the minimum possible number of times Prof. Pang called the procedure. It can be proven that the answer always exists.
题目大意
定义一次「操作」为选定任意一个 和 后对 序列进行以下操作(显然就是树状数组的单点加操作):
def update(pos, val):
while (pos <= n):
c[pos] += val
pos += pos & (-pos)
给定序列长度 和一个长度为 的 串 ,以及一个初始全为 的序列 。 求最少的操作数使得最终的 序列满足 $\forall 1\le i\le n: s_i=\texttt0 \Leftrightarrow c_i=0,s_i=1\Leftrightarrow c_i\not=0$。
你只需要输出最少的操作数。
3
5
10110
5
00000
5
11111
3
0
3
提示
For the first example, Prof. Pang can call update(1,1)
, update(2,-1)
, update(3,1)
in order.
For the third example, Prof. Pang can call update(1,1)
, update(3,1)
, update(5,1)
in order.