#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. hdoj1072 Nightmare(bfs)

    题目大意: 在迷宫中有一个炸弹,过六个单位时间就会爆炸,要你求一个起点到迷宫的终点的最短距离,迷宫中有时间重置器,当你走到这个格子,炸弹的爆炸时间重新置为0,迷宫中标识为墙壁的格子不能走,到达任意一个 ...

  2. php文件每隔几秒执行一次

    说实话,linux 下面的crontab 任务完全可以实现所有的定时任务脚本,但是有些脚本只需要在一段时间内执行,过了这段时间之后,就不再执行定时脚本了.在使用crontab的时候,就需要人为的关闭掉 ...

  3. 实现可搜索仿select下拉选中

    由于在优化项目中,发现先前写的一个活化石级的的可搜索下拉功能在高速搜索中会出现卡顿现象 1.起初的解决方法是在搜索事件中加入防抖函数隔一段时间才去触发他,同时搜索的不再是html文档片段,而是直接对数 ...

  4. HTTP协议及WWW服务应用

    一.用户访问网站的流程图 二.DNS解析的流程图 三.用户访问网站的基本流程原理阐述 ① 用户在浏览器中输入请求的地址回车 ② 先找本地的缓存和Hosts文件,有解析的对应IP直接返回个客户端IP地址 ...

  5. Java之集合(五)LinkedList

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7298017.html 1.前言 Java中另一个常见的list就是本章将要讲的LinkedList.ArrayL ...

  6. JDK1.10+scala环境的搭建之linux环境(centos6.9)

    ---恢复内容开始--- 第一步:安装jdk1.10版本 进入网页 http://oracle.com/technetwork/java/javase/downloads/index.html  下载 ...

  7. Android 开发工具类 21_SAXForHandler

    解析 XML 有两种形式: 1.XMLReader XMLReaser xmlReader = saxParser.getXMLReader(); xmlReadeer.setContentHandl ...

  8. [转] TCPIP 网络协议层对应的RFC文档

    TCPIP网络协议层对应的RFC文档 RFC - Request For Comments 请求注解 TCP/IP层 网络协议 RFC文档 Physical Layer Data Link Layer ...

  9. Centos下Kubernetes+Flannel部署(新)

    一.准备工作 1) 三台centos主机 k8s master: 10.11.151.97  tc-151-97 k8s node1: 10.11.151.100  tc-151-100 k8s no ...

  10. HMM分词实例

    class HMM(object): def __init__(self): import os # 主要是用于存取算法中间结果,不用每次都训练模型 self.model_file = 'model/ ...