Implementation:Binary Indexed Tree 树状数组
#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 树状数组的更多相关文章
- Binary Indexted Tree 树状数组入门
感谢http://www.cnblogs.com/xudong-bupt/p/3484080.html 树状数组(BIT)是能够完成下述操作的数据结构: 给定一初始值全为零的数列a1,a2a,a3.. ...
- HDU3333 Turing Tree 树状数组+离线处理
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- POJ 3321 Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Lim ...
- HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
- POJ--3321 Apple Tree(树状数组+dfs(序列))
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...
- gym 100589A queries on the Tree 树状数组 + 分块
题目传送门 题目大意: 给定一颗根节点为1的树,有两种操作,第一种操作是将与根节点距离为L的节点权值全部加上val,第二个操作是查询以x为根节点的子树的权重. 思路: 思考后发现,以dfs序建立树状数 ...
- POJ 3321:Apple Tree 树状数组
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22131 Accepted: 6715 Descr ...
- 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. ...
- POJ3321 Apple Tree(树状数组)
先做一次dfs求得每个节点为根的子树在树状数组中编号的起始值和结束值,再树状数组做区间查询 与单点更新. #include<cstdio> #include<iostream> ...
随机推荐
- Zookeeper原理分析之存储结构ZkDatabase
ZKDatabase在内存中维护了zookeeper的sessions, datatree和commit logs集合. 当zookeeper server启动的时候会将txnlogs和snapsho ...
- webpack快速入门——CSS中的图片处理
1.首先在网上随便找一张图片,在src下新建images文件夹,将图片放在文件夹内 2.在index.html中写入代码:<div id="pic"></div& ...
- 小A老师的学习法
3.13
- AFNetworking 报3840
工作中遇到前后台交互,前端解析不了后端返回的数据格式 ,原因在于没有标准统一的请求格式 这是个坑,但是还是有办法修复 错误提示: Error Domain=NSCocoaErrorDomain Cod ...
- python Snakes 库安装
SNAKES : A A Flexible High-Level Petri Nets Library SNAKES是python一个可以用于Petri网的库 python2安装SNAKES库: 在 ...
- Python小白学习之路(二十)—【打开文件的模式二】【文件的其他操作】
打开文件的模式(二) 对于非文本文件,我们只能使用b模式,"b"表示以字节的方式操作(而所有文件也都是以字节的形式存储的,使用这种模式无需考虑文本文件的字符编码.图片文件的jgp格 ...
- css实现栏目两边斜线的效果
实现效果: 具体实现代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- oracle 转 mysql 最新有效法
关键字:Oracle 转 MySQL . Oracle TO MySQL 没事试用了一下Navicat家族的新产品Navicat Premium,他集 Oracle.MySQL和PostgreSQL管 ...
- .Net Core 发布WindowsServices
项目代码文件夹 执行命令 dotnet publish -c Release -r win-x64
- error 'there is already an open datareader associated with this command which must be closed first'
This can be easily solved by allowing MARS in your connection string. Add MultipleActiveResultSets=t ...