#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. python del 方法的使用

    在Python 的自带函数中 del 函数是一个非常特殊但是又非常使用的函数 my_list = [1,2,3] my_dict = {"name":"lowman&qu ...

  2. Core Data Tutorial for IOS: Getting Started

    http://www.raywenderlich.com/934/core-data-tutorial-for-ios-getting-started

  3. biz_platform项目过程

    1.前台界面主要采用React框架.通过Ajax方式将数据与tornado服务器交互.以下代码为请求后台数据. var ThisPage = React.createClass({ render: f ...

  4. centos7在分区上建立文件系统和挂载

    在上一篇博客中,我们只说了硬盘的分区,既然进行分区,那么我们就要知道如何使用这些分区,就是接下来的建立文件系统和挂载. mkfs(make filesystem)格式化,建立文件系统 可以看到各种文件 ...

  5. (原创)确保JAVA线程安全的4种常用方法

    在Java中可以有很多方法来保证线程安全,比如使用同步方法.同步块,使用原子类(atomic concurrent classes),实现并发锁,使用volatile关键字,使用不变类和线程安全类. ...

  6. MFC 剪切板的使用、线程介绍

    一.MFC 剪切板 CListBox *pList = (CListBox*)GetDlgItem(IDC_LIST1); // 获取ListBox控件句柄 CString strTmp; pList ...

  7. scalac:cannot connnect to compile server(idea 编译scala)

    idea编译scala报错 解决办法: File->setting->scala compile server  (找到jdk填上 ok)

  8. gcc 编译问题

    一般情况一句话即可: gcc -o fuck fuck.c ./fuck 直接运行了 问题 1. 报错 ld 未找到 此时,gcc编译得分布来,并且指定特定的ld gcc -c  1.c //会在目录 ...

  9. visual studio 2008 快捷键

    Ctrl+m+Crtr+o折叠所有大纲Ctrl+M+Crtr+P: 停止大纲显示Ctrl+K+Crtr+C: 注释选定内容Ctrl+K+Crtr+U: 取消选定注释内容Ctrl+J : 列出成员 智能 ...

  10. [C++][转]CPU字节序 网络序 主机序 大端小端

    原帖:http://www.cnblogs.com/darktime/p/3298075.html 不同的CPU有不同的字节序类型 这些字节序是指整数在内存中保存的顺序 这个叫做主机序最常见的有两种1 ...