{"id":1491,"date":"2018-01-03T23:09:20","date_gmt":"2018-01-04T07:09:20","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1491"},"modified":"2020-04-13T20:43:20","modified_gmt":"2020-04-14T03:43:20","slug":"fenwick-tree-binary-indexed-tree-sp3","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/sp\/fenwick-tree-binary-indexed-tree-sp3\/","title":{"rendered":"\u82b1\u82b1\u9171 Fenwick Tree \/ Binary Indexed Tree \/ \u6811\u72b6\u6570\u7ec4 SP3"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 Fenwick Tree \/ Binary Indexed Tree - \u5237\u9898\u627e\u5de5\u4f5c SP3\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/WbafSgetDDk?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>\u672c\u671f\u8282\u76ee\u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86Fenwick Tree\/Binary Indexed Tree\/\u6811\u72b6\u6570\u7ec4\u7684\u539f\u7406\u548c\u5b9e\u73b0\u4ee5\u53ca\u5b83\u5728leetcode\u4e2d\u7684\u5e94\u7528\u3002<br>\nIn this episode, we will introduce Fenwick Tree\/Binary Indexed Tree, its idea and implementation and show its applications in leetcode.<\/p>\n<p>Fenwick Tree is mainly designed for solving the single point update range sum problems. e.g. what&#8217;s the sum between i-th and j-th element while the values of the elements are mutable.<\/p>\n<p>Init the tree (include building all prefix sums) takes O(nlogn)<\/p>\n<p>Update the value of an element takes O(logn)<\/p>\n<p>Query the range sum takes O(logn)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1496\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/sp3-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/sp3-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/sp3-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/sp3-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><\/p>\n<p><ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\"><\/ins><br>\n<script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1495\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/sp3-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/sp3-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/sp3-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/sp3-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><strong>Applications:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/data-structure\/307-range-sum-query-mutable\/\">\u82b1\u82b1\u9171 307. Range Sum Query &#8211; Mutable<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/difficulty\/hard\/315-count-of-smaller-numbers-after-self\/\">\u82b1\u82b1\u9171 315. Count of Smaller Numbers After Self<\/a><\/li>\n<\/ul>\n<p><strong>Implementation:<\/strong><\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">class FenwickTree {    \npublic:\n    FenwickTree(int n): sums_(n + 1, 0) {}\n    \n    void update(int i, int delta) {\n        while (i &lt; sums_.size()) {\n            sums_[i] += delta;\n            i += lowbit(i);\n        }\n    }\n    \n    int query(int i) const {        \n        int sum = 0;\n        while (i &gt; 0) {\n            sum += sums_[i];\n            i -= lowbit(i);\n        }\n        return sum;\n    }\nprivate:\n    static inline int lowbit(int x) { \n        return x &amp; (-x); \n    }\n    vector&lt;int&gt; sums_;\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true \">class FenwickTree {\n    int sums_[];\n    public FenwickTree(int n) {\n        sums_ = new int[n + 1];\n    }\n    \n    public void update(int i, int delta) {\n        while (i &lt; sums_.length) {\n            sums_[i] += delta;\n            i += i &amp; -i;\n        }\n    }\n    \n    public int query(int i) {\n        int sum = 0;\n        while (i &gt; 0) {\n            sum += sums_[i];\n            i -= i &amp; -i;\n        }\n        return sum;\n    }\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true \">class FenwickTree:\n    def __init__(self, n):\n        self._sums = [0 for _ in range(n + 1)]\n        \n    def update(self, i, delta):\n        while i &lt; len(self._sums):\n            self._sums[i] += delta\n            i += i &amp; -i\n    \n    def query(self, i):\n        s = 0\n        while i &gt; 0:\n            s += self._sums[i]\n            i -= i &amp; -i\n        return s<\/pre>\n<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Applications<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/data-structure\/307-range-sum-query-mutable\/\">https:\/\/zxi.mytechroad.com\/blog\/data-structure\/307-range-sum-query-mutable\/<\/a><\/li><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-315-count-of-smaller-numbers-after-self\/\">https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-315-count-of-smaller-numbers-after-self\/<\/a><\/li><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1409-queries-on-a-permutation-with-key\/\">https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1409-queries-on-a-permutation-with-key\/<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u671f\u8282\u76ee\u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86Fenwick Tree\/Binary Indexed Tree\/\u6811\u72b6\u6570\u7ec4\u7684\u539f\u7406\u548c\u5b9e\u73b0\u4ee5\u53ca\u5b83\u5728leetcode\u4e2d\u7684\u5e94\u7528\u3002 In this episode, we will introduce Fenwick Tree\/Binary Indexed Tree, its idea and implementation and show its applications in leetcode. Fenwick&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[170],"tags":[202,204],"class_list":["post-1491","post","type-post","status-publish","format-standard","hentry","category-sp","tag-binary-indexed-tree","tag-fenwick-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=1491"}],"version-history":[{"count":12,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1491\/revisions"}],"predecessor-version":[{"id":6616,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1491\/revisions\/6616"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}