zoukankan      html  css  js  c++  java
  • 一维树状数组(HD1166)

    #define _CRT_SECURE_NO_WARNINGS
    #include<iostream>
    #include<string.h>
    using namespace std;
    
    #define BITMAX 50001        //数组大小
    typedef int valueType;    //元素类型定义
    valueType BITree[BITMAX];    //一维树状数组,初始化
    /* 2^k k表示节点编号 x 二进制末尾 0 的个数 */
    inline int lowbit(int x)
    {
        return x & (-x);
    }
    
    /* 一维,C[x]=A[ x-lowbit(x)+1 ....... x ] */
    /* 节点 x 的值增加 add */
    /* x 不能为 0 ,,应该从 1 起步,否则无限循环下去 */
    inline void addPoint(int x, int add, int n)
    {
        for (int i = x; i <= n; i += lowbit(i)){
            BITree[i] += add;
        }
    }
    
    /* 获取前 x 项和*/
    inline valueType readSum(int x)
    {
        valueType sum = 0;
        for (int i = x; i > 0; i -= lowbit(i))
            sum += BITree[i];
        return sum;
    }
    
    
    int main()
    {
        int t, n;
        char s[6];
        int a, b;
        scanf("%d", &t);
        for (int w = 1; w <= t; ++w){
            printf("Case %d:
    ", w);
            scanf("%d", &n);
            memset(BITree, 0, sizeof(BITree));
            for (int i = 1; i <= n; ++i){
                scanf("%d", &a);
                addPoint(i, a, n);
            }
            while (scanf("%s",s) && s[0] != 'E'){
                switch (s[0])
                {
                case 'A':
                    scanf("%d %d", &a, &b);
                    addPoint(a, b, n);
                    break;
                case 'S':
                    scanf("%d %d", &a, &b);
                    addPoint(a, -b, n);
                    break;
                case 'Q':
                    scanf("%d %d", &a, &b);
                    printf("%d
    ", (readSum(b) - readSum(a - 1)));
                    break;
                default:
                    break;
                }
            }
        }
    }
  • 相关阅读:
    正则表达式的学习笔记
    apply()的使用
    for循环性能测试
    js基础复习~Array对象
    判断值是否为undefined
    layui 单选框取消选中
    layui 表单验证
    火狐浏览器下,表头边框无效
    css3 宽度百分比减去固定宽度 无效问题
    javaScript中的 call 和 apply
  • 原文地址:https://www.cnblogs.com/jokoz/p/4768874.html
Copyright © 2011-2022 走看看