杳冥是什么意思| 人为什么会近视| 脚趾缝痒用什么药| 吃什么能马上晕倒住院| 荔枝为什么上火| 检查有没有怀孕挂什么科| 细水长流是什么意思| 早上六点是什么时辰| 姑姑的孙子叫我什么| 五台山求什么最灵| 731是什么意思| 血糖高什么水果可以吃| 女人肾虚是什么原因| sd是什么意思| 游乐场都有什么项目| 倾城是什么意思| 1009是什么星座| 报应是什么意思| 看扁桃体挂什么科| 吉可以加什么偏旁| 移情是什么意思| 去医院验血挂什么科| 女性寒性体质喝什么茶| 胰腺是什么器官| 男大女6岁有什么说法| 皮肤痒是什么病的前兆| 51是什么意思| 棉花是什么时候传入中国的| 紫色加绿色是什么颜色| 田螺小子是什么意思| 血脂挂什么科| 子宫内膜异位是什么原因造成的| 头发不干就睡觉有什么危害| 多发性脂肪瘤是什么原因造成的| 闰六月是什么意思| 李子吃了有什么好处| 什么是犯太岁| 蛋白粉是什么| 甲状腺挂什么科| 传染性单核细胞增多症是什么病| 送人礼物送什么好| 水里有什么| 暗里着迷什么意思| 子宫憩室是什么| 铁饱和度低什么原因| 尿胆红素2十是什么意思| 七情六欲指的是什么| 窈窕淑女君子好逑是什么意思| 妇科病是什么| 尿蛋白十1是什么意思| 子宫肌瘤是什么原因导致的| 南京市市长什么级别| 为什么会打喷嚏| 32年婚姻是什么婚| 混合痔是什么| 射精太快吃什么药| 呕气是什么意思| 扶正固本是什么意思| 杭州有什么| 海绵是什么材料做的| 党的执政理念是什么| 什么叫甲沟炎| 娃儿发烧用什么方法退烧快| 1994属什么生肖| 少将相当于地方什么级别| 腹痛拉肚子吃什么药| 颈椎病引起的头晕吃什么药| 男性粘液丝高什么原因| 为什么会有痔疮| 孩子长个子吃什么有利于长高| 人到中年为什么会发胖| 垂头丧气什么意思| 热锅上的蚂蚁是什么意思| 什么是精索静脉曲张| 右肾占位是什么意思| 九浅一深什么意思| 脚崴了挂什么科| dha中文叫什么| 孩子胃疼吃什么药| 乙肝表面抗体高是什么意思| 吃什么开胃增加食欲| 肠溶片和缓释片有什么区别| 清肺热用什么泡水喝比较好| 老酒是什么酒| 相位是什么意思| 芹菜煮水喝有什么功效| 尿道口发炎用什么药| 住院预交金是什么意思| 孕妇梦到蛇是什么意思| 9月份是什么季节| 保鲜卡是什么原理纸片| 脚底烧热是什么原因| 眼压高是什么症状| 1967属什么生肖| 膝盖咔咔响吃什么药| 胸膜炎是什么症状| 10月10日是什么星座| 土地确权是什么意思| 七个小矮人分别叫什么| 黄芪喝多了有什么副作用| 及什么意思| gh是什么激素| 尿酸高要注意什么饮食| 鸡的五行属什么| 245阳性是什么意思| 手术后吃什么伤口愈合快| 大学挂科是什么意思| 皮肤溃烂用什么药治愈最快| 水稻什么时候播种| 什么东西| 竹代表什么生肖| 腱鞘炎吃什么药好| 喝什么胸会变大| 四战之地的生肖是什么| 检查甲状腺挂什么科| 青砖茶属于什么茶| 手背发黄是什么原因| 屁多不臭是什么原因| 昆布是什么| 追悔莫及什么意思| 化痰祛痰吃什么药| 月经不规律吃什么药调理| 7大营养素是什么| 黑米和什么一起搭配煮粥最佳| 心跳突然加快是什么原因| 蓝脸的窦尔敦盗御马是什么歌| 尿酸高都有什么症状| 三阳开泰是什么意思| 玛瑙五行属什么| 手长水泡是什么原因| 13楼五行属什么| 什么样的蝴蝶| 率真是什么意思| 含字五行属什么| 象是什么结构的字| 什么是七七事变| 海米是什么| 谷氨酸钠是什么添加剂| 想长胖喝什么奶粉好| 左眉毛上有痣代表什么| 菊花茶和枸杞一起泡水有什么好处| 怕热易出汗是什么原因| 伤口流水是什么原因| 龋病是什么意思| 中国信什么教| 爸爸的爸爸叫什么| 乙肝肝炎表面抗体阳性是什么意思| 怙恃是什么意思| 喝什么胸会变大| 愿君多采撷是什么意思| 脚踝韧带拉伤吃什么| 做梦梦到很多蛇是什么意思| 男人太瘦吃什么可以长胖| 胃反酸水吃什么药| 肝不好吃什么好| 咩是什么意思| joeone是什么牌子| 四十年婚姻是什么婚| 胃酸反酸水吃什么药| thc是什么费用| 什么是寓言故事| 景德镇有什么好玩的| 什么是碱性磷酸酶| 18k金是什么意思| 女性痔疮挂什么科室| 牙掉了是什么预兆| 猫不能吃什么| 肾虚吃什么药好| 歺是什么意思| 咖喱块什么牌子的好| 嘴角烂是什么原因| 吃相难看是什么意思| 2004是什么年| 1206是什么星座| 胃食管反流能吃什么水果| kj什么意思| 梦见前夫是什么兆头| 颈椎压迫神经挂什么科| 激素六项检查挂什么科| 陕西八大怪是什么| 泡泡纱是什么面料| 虐心是什么意思| 急性痛风吃什么药| 身主天相是什么意思| 胃溃疡吃什么食物好| 滴滴是什么意思| 公积金基数是什么意思| 颈椎病用什么药膏| 肺坠积性改变什么意思| rot是什么意思| 鱼的尾巴有什么作用| 西打酒是什么意思| 喉结肿大是什么原因| 法令纹上的痣代表什么| 梅花鹿吃什么| 高血压能吃什么水果| 为什么吐后反而舒服了| 什么水果含糖量最低| 渗透压偏高是什么原因| 四月十八日是什么日子| 辛弃疾字什么号什么| 属猴的是什么星座| 眼睛蒙蒙的是什么原因| 女人吃葛根粉有什么好处| 为什么要当兵| 鲅鱼是什么鱼| 比是什么意思| 胃食管反流挂什么科| 数字17代表什么意思| 1月1日什么星座| 副乳有什么危害吗| 黑色柳丁是什么意思| 什么东西不导电| 十岁女孩喜欢什么礼物| 皮炎用什么药膏最有效| 腹部增强ct能检查出什么| 喜欢蹲着是什么原因| 确立是什么意思| 梦见狗打架是什么意思| 心律失常是什么意思| 鸡拉绿色粪便是什么病| 黄晓明的老婆叫什么名字| 风向是什么意思| 毛豆有什么营养价值| 底线是什么意思| 甲胎蛋白高是什么原因| 甲辰年五行属什么| 卡介疫苗什么时候打| 蜜蜂为什么会蜇人| 倍增是什么意思| 女性胆囊炎有什么症状| 盆腔炎吃什么药最好| 女人来月经有血块是什么原因| 路亚什么意思| 白癜风是什么样子的| longines是什么牌子| 不什么不什么的词语| 淫羊藿是什么| 孕妇梦见牛是什么意思| 做梦梦见兔子是什么意思| 4月15日是什么日子| 感冒了吃什么水果比较好| 为什么拉肚子| bdp是什么意思| other是什么品牌| 大快朵颐是什么意思| 槿字五行属什么| poems是什么意思| huidr是什么品牌| 早餐吃什么不升血糖| 西瓜禁忌和什么一起吃| 纷纷扬扬是什么意思| 较真的人是什么性格| 做梦梦到怀孕了是什么意思| 肾五行属什么| 宜夫痣是什么意思| 三个马念什么| loewe是什么牌子| 西地那非是什么| 孕妇羊水多是什么原因造成的| 风暴是什么意思| 男人要吃什么才能壮阳| 白日做梦是什么生肖| 11.22是什么星座| 百度Jump to content

西安开启低空旅游模式 家门口体验热气球之旅

From Wikipedia, the free encyclopedia
Fenwick tree
Binary indexed tree
TypeBinomial tree
Invented1989
Invented byBoris Ryabko
Time complexity in big O notation
Operation Average Worst case
Search O(logn) O(logn)
Insert O(logn) O(logn)
Space complexity
Space O(n) O(n)
百度 结语  从多个方面上看,魅蓝S6所带来的价值已经超过了其价格,无论是拍照、游戏还是系统体验,在同价位的机型之中魅蓝S6基本能够横扫一大片对手。

A Fenwick tree or binary indexed tree (BIT) is a data structure that stores an array of values and can efficiently compute prefix sums of the values and update the values. It also supports an efficient rank-search operation for finding the longest prefix whose sum is no more than a specified value. Its primary use is operating on the cumulative distribution function of a statistical frequency table which is updated often.

This structure was proposed by Boris Ryabko in 1989[1] with a further modification published in 1992.[2] It has subsequently become known under the name Fenwick tree after Peter Fenwick, who described this structure in his 1994 article.[3]

A simple array of values is trivial (constant-time) to update but requires time to compute a prefix sum or search for a prefix length.

An array of prefix sums can return a prefix sum in constant time, and search for a prefix length in time, but requires time to update one of the values.

A Fenwick tree allows all three operations to be performed in time. This is achieved by representing the values as a tree with nodes where each node in the tree stores the sum of the values from the index of its parent (exclusive) up to the index of the node (inclusive). The tree itself is implicit and can be stored as an array of values, with the implicit root node omitted from the array. The tree structure allows the operations of value retrieval, value update, prefix sum, and range sum to be performed using only node accesses.

Motivation

[edit]

Given an array of values, it is sometimes desirable to calculate the running total of values up to each index according to some associative binary operation (addition on integers being by far the most common). Fenwick trees provide a method to query the running total at any index, or prefix sum, while allowing changes to the underlying value array and having all further queries reflect those changes.

Fenwick trees are particularly designed to implement adaptive arithmetic coding, which maintains counts of each symbol produced and needs to convert those to the cumulative probability of a symbol less than a given symbol. Development of operations it supports were primarily motivated by use in that case.[citation needed]

Description

[edit]

A Fenwick tree is an implicit tree where nodes are consecutively numbered, and parent-child relationships are determined by arithmetic on the node indexes.

An important function in this index arithmetic is the least significant set bit. This is the greatest power of two which divides an index . This is the power of two (1, 2, 4, 8, ...) and not the exponent (0, 1, 2, 3, ...). It can be efficiently computed in two's complement arithmetic as (where & denotes bitwise AND).

A Fenwick tree is most easily understood using a one-based array with values. Using half-open interval syntax, let the range from (exclusive) to (inclusive). The corresponding Fenwick array stores the range sums . That is, the sum of values ending with and including .

A fictitious node 0 is used in some descriptions, but is never actually accessed and need not be explicitly stored. but the value is never actually needed. may be considered to contain the sum of the empty range with value 0.

A "Fenwick tree" is actually three implicit trees over the same array: the interrogation tree used for translating indexes to prefix sums, the update tree used for updating elements, and the search tree for translating prefix sums to indexes (rank queries).[4] The first two are normally walked upwards, while the third is usually walked downwards.

The interrogation tree

[edit]

The interrogation tree is defined so that the parent of node is . For example, the parent of 6 = 1102 is 4 = 1002. Implicit node 0 is the root.

Each level of the tree contains nodes with indices corresponding to sums of distinct powers of 2 (with representing an empty sum 0). For example, level contains nodes and level contains nodes

Node has children (), and total descendants. (These numbers include nodes greater than , which are omitted and never accessed.)

The below diagram shows the structure of a 16-node Fenwick tree's interrogation tree, including the root, so it corresponds to a 15-element array A:

Depiction of a 16-node Fenwick interrogation tree containing range sums of a 15-node array A

To find the prefix sum , sum the values in , its parent, its parent's parent, and so on up to (but not including) the root. To compute a range sum , subtract the prefix sums for and .

This can be optimized by stopping at their first common ancestor. An extreme example is asking for a single entry . In this case, the common ancestor of and is , so start with and then, as long as , subtract and update i := i - lsb(i).

The update tree

[edit]

The update tree is the mirror image of the interrogation tree. The parent of node is (where | denotes bitwise OR). For example, the parent of 6 = 1102 is 8 = 10002.

This conceptual tree is infinite, but only the part with indexes up to is stored or used. Excluding the fictitious nodes with indexes greater than it will be a forest of disjoint trees, one for each bit set in the binary representation of .

Here, a node's ancestors are all nodes whose range sums include its own. For example, holds the sum of , holds the sum of , and so on.

To modify one of the values , add the change to , then 's parent, then its grandparent, and so on, until the index exceeds .

The search tree

[edit]

Unlike the other two trees, the search tree is a binary tree, arranged in an order Knuth calls a "sideways heap".[5] Each node is assigned a height equal to the number of trailing zeros in the binary representation of its index, with the parent and children being the numerically closest index(es) of the adjacent height. Nodes with odd indexes () are leaves. Nodes with even indexes have the closest two nodes of the next-lowest index as children, . Node 's parent in the search tree is .

For example, the children of 6 = 1102 are 5 = 1012 and 7 = 1112, and its parent is 4 = 1002.

Although this tree is potentially infinite, we may define its root to be the highest existing node, whose index is the greatest power of 2 less than or equal to .

It is possible for a node to have a fictitious parent with an index greater than yet still have an existing grandparent. If the example above applied to a 5-node tree, then node 5 would have a fictitious parent 6, but an existing grandparent 4.

The search tree may be considered a combination of the previous two trees. A node's left subtree contains all of its descendants in the update tree, while its right subtree contains all of its descendants in the interrogation tree. A node's parent in the search tree is either its interrogation or update parent (depending on whether the node is a right or left child, respectively), and the other type of parent may be found by multiple upward steps in the search tree.

However, upward traversals in the search tree are uncommon; its primary use is to perform rank queries: given a prefix sum, at what index does it appear? This is done by a downward traversal through the search tree. During the traversal, three variables are maintained: The current node's index, the rank being sought in the subtree rooted at the current node, and a "fallback index" to be returned if the rank sought is greater than can be found in the subtree.

Initially, the current node is the root, the rank sought is the original query, and the fallback index is a special "overflow" value indicating that the rank is not in the tree. (Depending on the application, or might be used for this purpose.)

Each step, either the current node is a fictitious node (index greater than ), or we must decide if the position sought is to the left or right of the end of the current node. If the rank sought is less than the Fenwick array value for the current node, we must search its left subtree. If it is greater, search its right subtree. If it is equal, the direction chosen depends on how you wish to handle searches for sums lying exactly between two nodes.

These three possibilities are then further divided based on whether the current node is a leaf or not:

  • If the current node is a leaf and:
    • the target is in its (empty) left subtree, return the current index.
    • it is fictitious or the target is in its right subtree, return the fallback index.
  • If the current node is not a leaf and:
    • it is fictitious, search for the same rank in its left subtree with an unchanged fallback index.
    • the target is in its left subtree, search for the same rank in its left subtree with the current index as the fallback index.
    • the target is in its right subtree, search for the target rank minus the current node's value in the right subtree, with an unchanged fallback index.

Pseudocode

[edit]

A simple pseudocode implementation of the two main operations on a Fenwick tree—query and update—is as following:

function query(tree, index) is
    sum := 0
    while index > 0 do
        sum += tree[index]
        index -= lsb(index)
    return sum

function update(tree, index, value) is
    while index < size(tree) do
        tree[index] += value
        index += lsb(index)

The function computes the least significant set bit of the given or, equivalently, the largest power of two that is also a divisor of . For example, , as shown in its binary representation: . This function can be simply implemented in code through a bitwise AND operation: lsb(n) = n & (-n), assuming two's complement negation.[3]

Construction

[edit]

One na?ve algorithm to construct a Fenwick tree consists of initializing the tree with null values and updating each index individually. This solution works in time, but an construction is possible:[6]

function construct(values) is
    tree := values
    for index from 1 to size(tree) do
        parentIndex := index + lsb(index)
        if parentIndex ≤ size(tree) then
            tree[parentIndex] += tree[index]
    return tree

See also

[edit]

References

[edit]
  1. ^ Boris Ryabko (1989). "A fast on-line code" (PDF). Soviet Math. Dokl. 39 (3): 533–537. Archived (PDF) from the original on 2025-08-14. Retrieved 2025-08-14.
  2. ^ Boris Ryabko (1992). "A fast on-line adaptive code" (PDF). IEEE Transactions on Information Theory. 28 (1): 1400–1404. Archived (PDF) from the original on 2025-08-14. Retrieved 2025-08-14.
  3. ^ a b Peter M. Fenwick (1994). "A new data structure for cumulative frequency tables". Software: Practice and Experience. 24 (3): 327–336. CiteSeerX 10.1.1.14.8917. doi:10.1002/spe.4380240306. S2CID 7519761.
  4. ^ Marchini, Stefano; Vigna, Sebastiano (14 October 2019). "Compact Fenwick trees for dynamic ranking and selection". arXiv:1904.12370 [cs.DS]. Extensive discussion of practical implementation details.
  5. ^ Knuth, Donald (2011). Combinatorial Algorithms, Part 1. The Art of Computer Programming. Vol. 4A. Upper Saddle River, NJ: Addison-Wesley Professional. pp. 164–165.
  6. ^ Halim, Steven; Halim, Felix; Effendy, Suhendry (3 December 2018). Competitive Programming 4. Vol. 1. Lulu Press, Incorporated. ISBN 978-1-716-74552-2.
[edit]
为什么泡完脚后非常痒 龙虾不能和什么一起吃 黑脸是什么意思 什么是核素 为什么会流鼻血
韩愈是什么朝代的 女性吃金蝉有什么好处 懒觉什么意思 梦见生了个儿子是什么意思 心肝火旺吃什么中成药
做活检是什么意思 中元节应该说什么 猴子是什么颜色 为什么突然有狐臭了 包皮红肿用什么药
6.30什么星座 儿童抗o高会引起什么病 什么是爱情观 一什么画 戒备心是什么意思
女人左手心痒预示什么ff14chat.com 梦见粉条是什么意思hcv9jop5ns5r.cn 什么是交际花hcv9jop2ns2r.cn 胃幽门螺杆菌有什么症状hcv9jop7ns4r.cn 琀是什么意思bjcbxg.com
59岁生日有什么讲究jasonfriends.com 体寒的人吃什么食物好hcv8jop2ns8r.cn 本我是什么意思hcv9jop0ns1r.cn 1963年属兔的是什么命hcv9jop0ns5r.cn 7月8号是什么星座hcv9jop6ns3r.cn
血小板低是什么原因hcv7jop6ns6r.cn 透明的什么hcv7jop9ns0r.cn 排尿困难是什么原因男性hcv9jop2ns6r.cn 自闭症是什么人投胎hcv9jop5ns6r.cn 什么是庞氏骗局hcv7jop5ns6r.cn
双肾盂分离是什么意思hcv8jop7ns3r.cn 党群是什么意思hcv9jop1ns4r.cn 梦见鸡死了是什么预兆hcv9jop5ns9r.cn 格五行属什么hcv8jop4ns1r.cn 梦见自己儿子死了是什么意思qingzhougame.com
百度