#include <iostream>
#include <cstdlib> using namespace std; class BinaryIndexedTree {
private:
int *mem;
int capacity;
public:
BinaryIndexedTree (int n) {
if (n <= ) {
capacity = ;
return;
}
capacity = n;
mem = new int[capacity + ];
for (int i=; i<=capacity; i++) mem[i] = ;
}
~BinaryIndexedTree() {
delete[] mem;
}
int sum(int idx) {
if (idx++ >= capacity) idx = capacity;
int s = ;
while(idx > ) {
s += mem[idx];
idx -= idx & -idx;
}
return s;
} void add(int idx, int val) {
idx++;
while (idx <= capacity) {
mem[idx] += val;
idx += idx & -idx;
}
}
}; int main() {
int nums[] = {, , , , , , , };
int n = sizeof(nums) / sizeof(int); BinaryIndexedTree bit(n); for (int i=; i<n; i++) {
bit.add(i, nums[i]);
} for (int i=; i<n; i++) {
cout<<"["<<<<", "<<i<<"] :"<<bit.sum(i)<<endl;
} // solve a problem using BIT, calculate how many reverse order pairs
// in a shuffled array{1, 2, 3...n}; n = array.length
int array[] = {,,,,};
int size = sizeof(array) / sizeof(int);
BinaryIndexedTree bt(size);
int rsum = ;
for (int i=; i<size; i++) {
rsum += i - bt.sum(array[i]);
bt.add(array[i], );
}
cout<<rsum<<endl; system("pause");
return ;
}

参考:

  挑战程序设计竞赛第二版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. FunDA(15)- 示范:任务并行运算 - user task parallel execution

    FunDA的并行运算施用就是对用户自定义函数的并行运算.原理上就是把一个输入流截分成多个输入流并行地输入到一个自定义函数的多个运行实例.这些函数运行实例同时在各自不同的线程里同步运算直至耗尽所有输入. ...

  2. javascript——后台传值map类型转换成json对象

    前端需要对后端传过来的值进行解析之后再展示,而后端传过来的值可能是各种类型的,一般情况下要么和后端沟通下让他直接传给我们需要的类型,这个,我一般直接自己转,这次后端传回来一个map类型的对象,我转来转 ...

  3. Vue 部署单页应用,刷新页面 404/502 报错

    在 Vue 项目中,可以选择 hash或者 history.pushState() 实现路由跳转.如果在路由中使用history模式: export default new Router({ mode ...

  4. nginx 开启GZIP、域名指向index.html

    nginx 虽然默认开启了gzip压缩,但是有关压缩文件.压缩效率没有开启,在建设我的(个人博客)[www.fayinme.cn]中,直观的感受到gzip带来的访问速度提升的快感. 如何开启GZIP ...

  5. jQuery.cookie的使用指南

    一个轻量级的cookie 插件,可以读取.写入.删除 cookie. 配置 首先包含jQuery的库文件,在后面包含 jquery.cookie.js 的库文件. <script type=&q ...

  6. C的Define

    #define Conn(x,y) x##y  //表示x连接y #define ToChar(x) #@x //给x加上单引号 #define ToString(x) #x  //给x加双引号 #d ...

  7. iOS-消除CocoaPods内容警告

    前言 2018年2月26日 农历正月十一 星期一 今天就想更新下博客,内容不多,心情复杂: 突然想吟诗一首: 其实,我是一个善良的人: 其实,我是一枚... 算了,还是说正事吧 消除CocoaPods ...

  8. crop和resize操作区别

    crop:对图像进行剪切 resize:对图像进行伸缩 实践代码 import cv2 bb2d = [30, 30, 72 ,42] image = cv2.imread('car.png') pt ...

  9. Python shutil模块(目录和文件操作)

    import shutil #导入shutil模块 copyfileobj方法 将类文件对象fsrc的内容复制到类文件对象fdst shutil.copyfileobj(fsrc, fdst[, le ...

  10. Springboot 打jar包分离lib,配置文件正确方式(二)

    Springboot 打jar包分离lib,配置文件正确方式(二) 背景 从<Springboot 打jar包分离lib,配置文件正确方式>中,可以达到把配置文件和依赖第三方的jar包分离 ...