A Fenwick tree (or binary indexed tree) is a fast data structure for storing and maintaining cumulative frequency tables.
Questions tagged [fenwick-tree]
75 questions
6
votes
1 answer
How to find the total number of Increasing sub-sequences of certain length with Binary Index Tree(BIT)
How can I find the total number of Increasing sub-sequences of certain length with Binary Index Tree(BIT)?
Actually this is a problem from Spoj Online Judge
Example
Suppose I have an array 1,2,2,10
The increasing sub-sequences of length 3 are 1,2,4…

Mostafiz Rahman
- 8,169
- 7
- 57
- 74
5
votes
4 answers
MySQL: Forcing query to use indices with local variable in WHERE clause
Context
I have an application that selects a weighted random entry from a table for which prefix summation (of weights) is a crucial part. The simplified table definition looks like this:
CREATE TABLE entries (
id INT NOT NULL PRIMARY KEY…

concat
- 3,107
- 16
- 30
4
votes
0 answers
Non-fixed-size Fenwick Tree implementation
I'm planning to implement a non-fixed-size Fenwick tree in Java. That is, a Fenwick tree that allows interleaving range queries with adding/removing elements.
All implementations and samples I've seen so far are for fixed-size Fenwick trees, where…

fps
- 33,623
- 8
- 55
- 110
4
votes
2 answers
O(klogn) time algorithm to find kth smallest element from a Fenwick-Tree
I mean to find the kth smallest actual frequency in a Fenwick-Tree in O(k log(n)) time.
If my data is:
Tree = [1,3,1,10,3]
Actual frequency = [1,2,1,6,3]
So the second smallest element would be at index 1.

Ninja420
- 3,542
- 3
- 22
- 34
4
votes
1 answer
SPOJ INVCNT - how?
can anyone help me with this task http://www.spoj.com/problems/INVCNT/ . First I try to think in BIT-way but I can't. Can anyone explain the solution of this task with BIT. BIT- Binary indexed tree
c++

Dario
- 115
- 1
- 6
4
votes
1 answer
Binary Indexed Tree (Fenwick Tree) - about updating
I've recently learned the Fenwick Tree (Binary Indexed Tree) data structure.
When querying , I can understand why to subtract (idx & -idx).
However, I can't really understand why to add (idx & -idx) when updating a value.
In other words, I know we…

Haidar Abboud
- 47
- 6
3
votes
2 answers
Why would you ever add 10^9+7 to a number and then take mod with 10^9+7
I was solving a problem of Fenwick tree named shill and wave sequence and it wasn't passing all the test cases until I added a line looking at the solution and now want to find its purpose ,here is my code
#include
using namespace…

AVIRAL KHANDUJA
- 31
- 1
3
votes
1 answer
Find the number of players cannot win the game?
We are given n players, each player has 3 values assigned A, B and C.
A player i cannot win if there exists another player j with all 3 values A[j] > A[i], B[j] > B[i] and C[j] > C[i]. We are asked to find number of players cannot win.
I tried this…

tusharRawat
- 719
- 10
- 24
3
votes
1 answer
Update step in Fenwick Trees
My question concerns the full reasoning behind the update step in Binary Indexed Trees (Fenwick Trees). As such, when updating our array with a certain increment, at a certain position, the update goes like this:
void updateBIT(int BITree[], int n,…

user43389
- 721
- 6
- 18
3
votes
2 answers
How to find which position have prefix sum M in BIT?
Suppose I have created a Binary Indexed Tree with prefix sum of length N. The main array contains only 0s and 1s. Now I want to find which index has a prefix sum M(That means have exactly M 1s).
Like my array is a[]={1,0,0,1,1};
prefix-sum would…

madMDT
- 448
- 1
- 4
- 16
3
votes
1 answer
linearizing a tree to an array and answering "sum" queries on paths
The question is motivated by the travtree problem in codechef. In the editorial they recommend linearizing the tree to an array by recording for each node its discovery and exit times in a DFS traversal. Now we can quickly answer queries about sum…

ihadanny
- 4,377
- 7
- 45
- 76
3
votes
0 answers
Range Update - Range Query using Fenwick Tree
http://ayazdzulfikar.blogspot.in/2014/12/penggunaan-fenwick-tree-bit.html?showComment=1434865697025#c5391178275473818224
For example being told that the value of the function or f (i) of the index-i is an i ^ k, for k> = 0 and always stay on this…

Nakshatra
- 663
- 1
- 6
- 14
3
votes
1 answer
Maximum Sum of a non decreasing sub-sequence in an array using fenwick tree or BIT
How can we find the maximum sum of a non-decreasing sub-sequence in an array using fenwick tree? For example we have 1 4 4 2 2 3 3 1, here the maximum sum of a non-decreasing sub-sequence is 11 (1 2 2 3 3).

shalzangel
- 387
- 2
- 10
2
votes
1 answer
How can I find the sum of all possible inversion count in all the subarray?
I need to find some of the inversion counts in all subarray in the least possible time complexity.
Two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j
I have tried it using Fenwick Tree implementation but getting time limit…

Pension Fo
- 29
- 1
2
votes
5 answers
How do I efficiently multiply a range of values of an array with a given number?
The naive way would be to linearly iterate the range and multiply with each number in the range.
Example: Array: {1,2,3,4,5,6,7,8,9,10};
Multiply index 3 to index 8 with 2. Assuming one based index.
Result array should be :…

Scott
- 77
- 1
- 6