• 2021 ICPC Asia East Continent Final L. 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[1…n]of length nn which is initially all-zero (c[i]=0 for any 1≤i≤n). Each time, Prof. Pang can call the following procedure for some position pospos (1≤pos≤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 2that 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 c. He only remembers whether c[i]is zero or not for each i from 1 to n. Prof. Pang wants to know what is the minimum possible number of times he called the procedure assuming his memory is accurate.

    Input

    The first line contains a single integer T (1≤T≤105)denoting the number of test cases.

    For each test case, the first line contains an integer n (1≤n≤105). The next line contains a string of length n. The ii-th character of the string is 1 if c[i] is nonzero and 0 otherwise.

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

    Output

    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.

    Example

    input

    Copy

    3
    5
    10110
    5
    00000
    5
    11111
    

    output

    Copy

    3
    0
    3
    

    Note

    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.

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. #define int long long
    4. int lowbit(int x)
    5. {
    6. return x&(-x);
    7. }
    8. void solve()
    9. {
    10. int n;
    11. cin>>n;
    12. string s;
    13. cin>>s;
    14. vector<int>cnt(n+1);
    15. for(int i=1;i<=n;i++)
    16. {
    17. if(s[i-1]=='1')
    18. {
    19. if(i+lowbit(i)<=n)
    20. {
    21. cnt[i+lowbit(i)]++;
    22. }
    23. }
    24. }
    25. int sum=0;
    26. for(int i=1;i<=n;i++)
    27. {
    28. if(s[i-1]=='0'&&cnt[i]==1)
    29. {
    30. sum++;
    31. }
    32. else if(s[i-1]=='1'&&cnt[i]==0)
    33. {
    34. sum++;
    35. }
    36. }
    37. cout<<sum<<"\n";
    38. }
    39. signed main()
    40. {
    41. /*ios::sync_with_stdio(false);
    42. cin.tie(0);
    43. cout.tie(0);*/
    44. int _;
    45. cin>>_;
    46. while(_--)
    47. {
    48. solve();
    49. }
    50. return 0;
    51. }

  • 相关阅读:
    嵌入式软件架构设计-函数调用
    如何保养维护实验室超声波清洗机
    通信达交易接口常量定义
    Django Web架构:全面掌握Django模型字段(上)
    Nginx详细配置指南
    jQuery 入门-----第二节(2):案例综合:用jQuery写:新浪微博下拉菜单和tab栏切换
    轻量级的开源代理服务器Tinyproxy安装与配置
    【配置环境】Visual Studio 配置 OpenCV
    平安城市解决方案-最新全套文件
    Windows右键没有新建Word、PPT与Excel的解决方法
  • 原文地址:https://blog.csdn.net/m0_61949623/article/details/126515543