Questions tagged [binary-indexed-tree]

Binary Indexed Tree(BIT) also known as Fenwick Tree is a tree based advanced data structure that can be used to store and calculate cumulative sum or frequency. BIT is more efficient than other data structures (like Segment Tree and other RMQ) considering the time complexity, memory complexity and Line of Code.

Binary Indexed Tree(BIT) also known as Fenwick Tree is a data structure providing efficient methods for calculation and manipulation of the prefix sums of a table of values. It was proposed by Peter Fenwick in 1994.

BIT primarily solve the problem of balancing prefix sum calculation efficiency with element modification efficiency. The efficiency of these operations comes as a trade-off - greater efficiency in prefix sum calculation is achieved by pre-calculating values, but as more values are pre-calculated more must be re-calculated upon any modification to the underlying value table.

The initial building of BIT requires O(nLogn) time. But it can effectively run a range query on the built tree in O(logn) time and also update the tree in O(logn) time. Most importantly BIT can store the frequency table or the build tree in O(n) memory space.

Use this tag when you are asking a problem related to Binary Indexed Tree. You should include code snippet or pseudo-code if necessary.

Referance

  1. Fenwick Tree, Wikipedia
  2. Topcoder Tutorial
  3. A New Data Structure for Cumulative Frequency Tables (1994)
28 questions
1
vote
2 answers

Range update in Binary Indexed Tree

How can I use Binary Indexed Tree for range update such that each element A[k] in a range say [i..j] is updated to A[k]*c where c is some constant. And I need to do point queries after such update operations. I tried with the function below but it…
0
votes
0 answers

Range query question on fenwick/binary indexed tree

So I have an integer array in which I need to find the sum between range L to R Also I need to add some value x(int) to range L to R i.e update operation. I have solved these questions using fenwick tree range sum/update queries but this problem is…
0
votes
1 answer

Fenwick tree(BIT). Find the smallest index with given cumulative frequency in O(logN)

let's say I have a BIT(Fenwick Tree) with non-negative values and I want to find the smallest index in it for given cumulative frequency in O(logN). Now, I can do it O(log^2(N)) like this. int l = 0, r = n; while(l < r) { int midd = l +…
0
votes
1 answer

how to increase the size limit of a mutable list in kotlin?

I was attempting to solve the multiset question (https://codeforces.com/contest/1354/problem/D) on codeforces using Fenwick Tree Data structure. I passed the sample test cases but got the memory limit error after submitting, the testcase is…
Kushagra
  • 41
  • 1
  • 1
  • 5
0
votes
1 answer

C++ Counting inversions in array, Fatal Signal 11 (BIT)

I was given this challenge in a programming "class". Eventually I decided to go for the "Binary Indexed Trees" solution, as data structures are a thing I'd like to know more about. Implementing BIT was somewhat straight forward, things after that -…
Nedas Bolevičius
  • 311
  • 1
  • 2
  • 10
0
votes
1 answer

can someone provide me the algorithm of 2-d binary indexed tree?

I searched on internet but couldn't find a good one. I got some help from geeksforgeeks.org but can't understand the construction part where we are subtracting v1-v2-v2-v4+v3 from aux[i][j] while updating the BIT array. Just let me know why we are…
goku
  • 156
  • 2
  • 8
0
votes
1 answer

String query with binary indexed tree

I want to range query a string using Fenwick tree. But something is going wrong with my code. Concatenation is giving error Eror is:[Error] no match for 'operator+=' (operand types are 'std::vector >' and 'std::string {aka std::basic_string}') …
nRT
  • 13
  • 4
0
votes
1 answer

RMQ using two fenwick trees (binary indexed tree)

Based on this paper, I found that it is quite brilliant to use two BITs to do RMQ in O(lg N), as it is easier to code than segment tree, and the paper claims it performs better than other data structures as well. I understand how to build the tree…
shole
  • 4,046
  • 2
  • 29
  • 69
0
votes
1 answer

Find number of items with weight k in a range (with updates and queries)

I am trying to solve the following problem: Given an array of items with integer weights (arbitrary order), we can have 2 possible operations: Query: Output the number of items that are of weight k, in the range x to y. Update: Change the…
Donald
  • 1,300
  • 2
  • 13
  • 29
0
votes
1 answer

Fenwick tree Sum query

This is the code for the sum query from Index 0 to Index X Int query(int x){ Int sum=0; for(; x>0; x -= x &(-x) ) sum += BIT[x]; Return sum; } I have two arrays BIT[] and a[]. I store the values from array a to BIT for queries. Now…
Sam_Buck
  • 25
  • 5
-1
votes
1 answer

Couldn't figure out whats wrong with my Binary Indexed Tree Solution

I am solving a problem. Count of Range Sum Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive. Range sum S(i, j) is defined as the sum of the elements in nums between indices i and j…
Isabella Lu
  • 1
  • 2
  • 3
-1
votes
1 answer

BIT To Query For a given range

We have an array arr[0 . . . n-1]. We should be able to efficiently find the minimum value from index qs (query start) to qe (query end) where 0 <= qs <= qe <= n-1 I know the data structure Segment Tree for this. I am wondering if Binary Index Tree…
user4415506
  • 109
  • 1
  • 7
-2
votes
1 answer

Binary index tree( range update and point query)

Can some one help me understand in binary index tree when we do range update- Why no we update every element why only starting element and ending element For example 0 we have to update array element range in BIT for arr[x] to arr[y] .As per…
1
2