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> ...
随机推荐
- 静态分析第三发 so文件分析(小黄人快跑)
本文作者:i春秋作家——HAI_ 0×00 工具 1.IDA pro 2.Android Killer 0×01 环境 小黄人快跑 下载地址http://download.csdn.net/downl ...
- 详述MySQL服务在渗透测试中的利用
本文作者:i春秋签约作家——Binghe 致力于书写ichunqiu社区历史上最长篇最细致最真实的技术复现文章. 文章目录: MySQL之UDF提权 MySQL之MOF提权 MySQL之常规写启动项提 ...
- ffmpeg开发基础知识
1.音频采集 面临的问题:延时敏感,噪声消除,回声消除,静音检测 主要参数: 采样率,位宽,声道数,音视频帧 采样率: 也称为采样速度或者采样率,定义了每秒从连续信号中提取并组成离散信号的采样个数,它 ...
- SQL中文转拼音
使用下方的函数.. 忘了从哪抄的了..留存一份 如果只要首字母..建议将数据 Left(tableFiled,1) 后传入函数 如果字段是空或者null, 不会报错..返回空 方法体: SET AN ...
- Python中通过函数对象创建全局变量
先看下面这段代码,显然无法work. 因为代码试图在TestVariableScope()中引用一个没有被定义的变量a.所以必须报错,如下图-1. 不过如果你将第2行代码注释掉.代码就能跑通了,如图- ...
- python聚类算法实战详细笔记 (python3.6+(win10、Linux))
python聚类算法实战详细笔记 (python3.6+(win10.Linux)) 一.基本概念: 1.计算TF-DIF TF-IDF是一种统计方法,用以评估一字词对于一个文件集或一个语料库 ...
- MySQL 5.7.21版本sql_mode=only_full_group_by问题
用到GROUP BY 语句查询时com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expression #2 of SELECT ...
- Picasso处理同一url,但资源变了的情况
问题:上传头像成功后,头像的url没变,加载头像时还是从缓存中根据url加载以前的图片. 这个问题,很多人遇到过.也受到很多同行的启发. 图片url不变,感觉不是很合理,这样会把缓存搞乱. 但是,作为 ...
- javac后期需要重点阅读的类
(1)Annotate (300行) Enter annotations on symbols. Annotations accumulate in a queue,which is processe ...
- centos7-安装mysql5.6.36
本地安装了mysql5.7, 但和springboot整合jpa时会出现 hibernateException, 不知道为什么, 换个mysql5.6版本的mysql, 源码安装, cmake一直过 ...