1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. class BinaryIndexedTree {
  7. private:
  8. int *mem;
  9. int capacity;
  10. public:
  11. BinaryIndexedTree (int n) {
  12. if (n <= ) {
  13. capacity = ;
  14. return;
  15. }
  16. capacity = n;
  17. mem = new int[capacity + ];
  18. for (int i=; i<=capacity; i++) mem[i] = ;
  19. }
  20. ~BinaryIndexedTree() {
  21. delete[] mem;
  22. }
  23. int sum(int idx) {
  24. if (idx++ >= capacity) idx = capacity;
  25. int s = ;
  26. while(idx > ) {
  27. s += mem[idx];
  28. idx -= idx & -idx;
  29. }
  30. return s;
  31. }
  32.  
  33. void add(int idx, int val) {
  34. idx++;
  35. while (idx <= capacity) {
  36. mem[idx] += val;
  37. idx += idx & -idx;
  38. }
  39. }
  40. };
  41.  
  42. int main() {
  43. int nums[] = {, , , , , , , };
  44. int n = sizeof(nums) / sizeof(int);
  45.  
  46. BinaryIndexedTree bit(n);
  47.  
  48. for (int i=; i<n; i++) {
  49. bit.add(i, nums[i]);
  50. }
  51.  
  52. for (int i=; i<n; i++) {
  53. cout<<"["<<<<", "<<i<<"] :"<<bit.sum(i)<<endl;
  54. }
  55.  
  56. // solve a problem using BIT, calculate how many reverse order pairs
  57. // in a shuffled array{1, 2, 3...n}; n = array.length
  58. int array[] = {,,,,};
  59. int size = sizeof(array) / sizeof(int);
  60. BinaryIndexedTree bt(size);
  61. int rsum = ;
  62. for (int i=; i<size; i++) {
  63. rsum += i - bt.sum(array[i]);
  64. bt.add(array[i], );
  65. }
  66. cout<<rsum<<endl;
  67.  
  68. system("pause");
  69. return ;
  70. }

参考:

  挑战程序设计竞赛第二版p176

Implementation:Binary Indexed Tree 树状数组的更多相关文章

  1. Binary Indexted Tree 树状数组入门

    感谢http://www.cnblogs.com/xudong-bupt/p/3484080.html 树状数组(BIT)是能够完成下述操作的数据结构: 给定一初始值全为零的数列a1,a2a,a3.. ...

  2. HDU3333 Turing Tree 树状数组+离线处理

    Turing Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. POJ 3321 Apple Tree(树状数组)

                                                              Apple Tree Time Limit: 2000MS   Memory Lim ...

  4. HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)

    题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...

  5. POJ--3321 Apple Tree(树状数组+dfs(序列))

    Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...

  6. gym 100589A queries on the Tree 树状数组 + 分块

    题目传送门 题目大意: 给定一颗根节点为1的树,有两种操作,第一种操作是将与根节点距离为L的节点权值全部加上val,第二个操作是查询以x为根节点的子树的权重. 思路: 思考后发现,以dfs序建立树状数 ...

  7. POJ 3321:Apple Tree 树状数组

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22131   Accepted: 6715 Descr ...

  8. E - Apple Tree(树状数组+DFS序)

    There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...

  9. POJ3321 Apple Tree(树状数组)

    先做一次dfs求得每个节点为根的子树在树状数组中编号的起始值和结束值,再树状数组做区间查询 与单点更新. #include<cstdio> #include<iostream> ...

随机推荐

  1. 静态分析第三发 so文件分析(小黄人快跑)

    本文作者:i春秋作家——HAI_ 0×00 工具 1.IDA pro 2.Android Killer 0×01 环境 小黄人快跑 下载地址http://download.csdn.net/downl ...

  2. 详述MySQL服务在渗透测试中的利用

    本文作者:i春秋签约作家——Binghe 致力于书写ichunqiu社区历史上最长篇最细致最真实的技术复现文章. 文章目录: MySQL之UDF提权 MySQL之MOF提权 MySQL之常规写启动项提 ...

  3. ffmpeg开发基础知识

    1.音频采集 面临的问题:延时敏感,噪声消除,回声消除,静音检测 主要参数: 采样率,位宽,声道数,音视频帧 采样率: 也称为采样速度或者采样率,定义了每秒从连续信号中提取并组成离散信号的采样个数,它 ...

  4. SQL中文转拼音

    使用下方的函数.. 忘了从哪抄的了..留存一份 如果只要首字母..建议将数据  Left(tableFiled,1) 后传入函数 如果字段是空或者null, 不会报错..返回空 方法体: SET AN ...

  5. Python中通过函数对象创建全局变量

    先看下面这段代码,显然无法work. 因为代码试图在TestVariableScope()中引用一个没有被定义的变量a.所以必须报错,如下图-1. 不过如果你将第2行代码注释掉.代码就能跑通了,如图- ...

  6. python聚类算法实战详细笔记 (python3.6+(win10、Linux))

    python聚类算法实战详细笔记 (python3.6+(win10.Linux)) 一.基本概念:     1.计算TF-DIF TF-IDF是一种统计方法,用以评估一字词对于一个文件集或一个语料库 ...

  7. MySQL 5.7.21版本sql_mode=only_full_group_by问题

      用到GROUP BY 语句查询时com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expression #2 of SELECT ...

  8. Picasso处理同一url,但资源变了的情况

    问题:上传头像成功后,头像的url没变,加载头像时还是从缓存中根据url加载以前的图片. 这个问题,很多人遇到过.也受到很多同行的启发. 图片url不变,感觉不是很合理,这样会把缓存搞乱. 但是,作为 ...

  9. javac后期需要重点阅读的类

    (1)Annotate (300行) Enter annotations on symbols. Annotations accumulate in a queue,which is processe ...

  10. centos7-安装mysql5.6.36

    本地安装了mysql5.7, 但和springboot整合jpa时会出现 hibernateException, 不知道为什么, 换个mysql5.6版本的mysql,  源码安装, cmake一直过 ...