#P9883. [EC Final 2021] Fenwick Tree

    ID: 9287 Type: RemoteJudge 1000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 4 Uploaded By: Tags>2021Special JudgeO2优化ICPC

[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 c[1n]c[1\ldots n] of length nn which is initially all-zero (c[i]=0c[i]=0 for any 1in1\le i\le n). Each time, Prof. Pang can call the following procedure for some position pospos (1posn1\leq pos \leq n) and value valval:

def update(pos, val):
    while (pos <= n):
        c[pos] += val
        pos += pos & (-pos)

Note that pos & (-pos) equals to the maximum power of 22 that divides pos for any positive integer pos.

In the procedure, valval can be any real number. After calling it some (zero or more) times, Prof. Pang forgets the exact values in the array cc. He only remembers whether c[i]c[i] is zero or not for each ii from 11 to nn. 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 T (1T105)T~(1 \le T \le 10^5) denoting the number of test cases.

For each test case, the first line contains an integer n (1n105)n~(1 \le n \le 10 ^ 5). The next line contains a string of length nn. The ii-th character of the string is 1 if c[i]c[i] is nonzero and 0 otherwise.

It is guaranteed that the sum of nn over all test cases is no more than 10610^6.

输出格式

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.

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.