一维树状数组(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:\n", 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\n", (readSum(b) - readSum(a - 1)));
                break;
            default:
                break;
            }
        }
    }
}
时间: 2024-08-29 18:05:29

一维树状数组(HD1166)的相关文章

一维树状数组

Apple Tree http://poj.org/problem?id=3321 1 #include<cstdio> 2 #include<cstring> 3 #define mt(a,b) memset(a,b,sizeof(a)) 4 const int M=100010; 5 struct G{ 6 struct E{ 7 int u,v,next; 8 }e[M<<1]; 9 int le,head[M]; 10 void init(){ 11 le=0;

HDU - 1556 Color the ball (一维树状数组 + 区间修改 + 单点求值)

HDU - 1556 Color the ball Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但是N次以后lele已经忘记了第I个气

一维树状数组入门

个人觉得非常棒的博客:https://www.cnblogs.com/xenny/p/9739600.html 第一类:单点更新,区间查询 例题:http://acm.hdu.edu.cn/showproblem.php?pid=1166 AC代码: 1 /* */ 2 # include <iostream> 3 # include <cstdio> 4 # include <cstring> 5 # include <string> 6 # includ

一维 + 二维树状数组 + 单点更新 + 区间更新 详解

树状数组详解: 假设一维数组为A[i](i=1,2,...n),则与它对应的树状数组C[i](i=1,2,...n)是这样定义的: C1 = A1 C2 = A1 + A2 C3 = A3 C4 = A1 + A2 + A3 + A4 C5 = A5 C6 = A5 + A6 ................. C8 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 ................ 如图可知: 为奇数的时候他是代表他本身,而为偶数的时候则是代表着自

树状数组(二叉索引树 BIT Fenwick树) *【一维基础模板】(查询区间和+修改更新)

刘汝佳:<训练指南>Page(194) #include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; //一维树状数组基础模板 int lowbit(int x) { return x&(-x); } int c[1001]; int sum(int x) //计算从1到x的数组元素的和 { int

POJ 1195 Mobile phones(二维树状数组)

题目链接:POJ 1195 题意: 给出一个S*S的矩阵(行.列号从1开始),每个元素初始值为0,有两种操作:一种是第X行第Y列元素值加A:另一种是查询给定范围矩阵的所有元素之和(L<=X<=R,B<=Y<=T). 分析: 查询给定范围矩阵的所有元素之和是二维区间和,可以转换为二维前缀和求值.类比一维前缀和求法,二维区间和S(L, B, R, T) = S(1, 1, R, T) - S(1 ,1, L-1, T) - S(1, 1, R, B-1) + S(1, 1, L-1,

深入理解树状数组

树状数组(Binary Indexed Tree(BIT), Fenwick Tree)是一个查询和修改复杂度都为log(n)的数据结构.主要用于查询任意两位之间的所有元素之和,但是每次只能修改一个元素的值:经过简单修改可以在log(n)的复杂度下进行范围修改,但是这时只能查询其中一个元素的值(如果加入多个辅助数组则可以实现区间修改与区间查询). 百度上给出了令人难以理解的概念,其实这个东西我也是琢磨了一天,参考了大量博客的笔记才搞清楚了大致思路和原理,说说心得吧! 假设数组a[1..n],那么

hdu 5480|| bestcoder   #57 div 2 Conturbatio(前缀和||树状数组)

Conturbatio Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 211    Accepted Submission(s): 99 Problem Description There are many rook on a chessboard, a rook can attack the row and column it bel

树状数组入门

用office做了一张pdf - - 这是一维的情形,如果是二维,可以把每一行的一维树状数组看成一个节点,然后再把二维树状数组看成一维树状数组. 好文章:https://www.topcoder.com/community/data-science/data-science-tutorials/binary-indexed-trees/#prob 两道入门题:http://acm.hdu.edu.cn/showproblem.php?pid=1556 http://poj.org/problem