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
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
0 answers
how to find total sum of last k terms for every index i in range l to r?
how to find the total sum of last k terms for every index i in range l to r in an array of n size for q queries using Fenwick tree and k is different for every query.It's a problem on hackerearth hacker with team
i have its tutorial but i don't get…
user5208204
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
Count number of consecutive candies
I am trying to solve a problem that is defined as follows:
A candy is represented by a number from 1 to 1e6. A person
is said to have a set of k candies if he has candy 1 to k.
E.g. If a person bought candies 1, 2 and 6, then he has a set of 2
…

Donald
- 1,300
- 2
- 13
- 29
0
votes
1 answer
Fenwick Tree HackerEarth
I have the following implementation of a Fenwick tree to solve the following Question on HackeEarth.
#include
#define MAX 1000000007
using namespace std;
long long int dp[500006];
long long int GCD[500006];
// Function to Calculate…

Aakash Arayambeth
- 91
- 1
- 4
0
votes
1 answer
How do I implement a range update and range queries in Binary Indexed Tree?
I have read some tutorials on Binary Indexed Tree, but i'm not able to understand how to implement it when query and update both operations are in some range.

Akshay Bansal
- 3
- 3
0
votes
1 answer
Range update in BIT with a condition
Can I perform range updates in BIT with a condition. Lets say I have an array of negative and positive frequencies A[] = {1, -3, -4, 5, 9}. And I wanted to range update the values of the array with a condition : If the update value(x) is negative,…

RAGHU RAMAN
- 537
- 4
- 16
0
votes
1 answer
SPOJ DQUERY : TLE Even With BIT?
Here is The Problem i Want to Solve , I am Using The Fact That Prefix Sum[i] - Prefix Sum[i-1] Leads to Frequency being Greater than Zero to Identify Distinct Digits and Then i am Eliminating The Frequency , But Even with BIT , i am Getting a TLE…

Siddharth Singh
- 69
- 8
0
votes
1 answer
Incorrect update elements in Fenwick Tree
I wrote a program, which give an sum of range by getting command 's' and update an element by command 'u', but it works incorrect.
Help me please.
#include
#include
#include
using namespace std;
template

Porfiriy
- 207
- 2
- 11
0
votes
1 answer
Find the total number of increasing subsequence of length k
Suppose I have an array 1,2,2,10.
The increasing sub-sequences of length 3 are 1,2,4 and 1,3,4(index based).
So, the answer is 2. Problem LINK
I want a better solution using BIT tree which could improve my solution.
I Have tried using BIT tree but…

QWE
- 45
- 9
0
votes
1 answer
Segmentation fault in Fenwick tree(Binary Index Tree)
I've realised Fenwick Tree, but when i try to find sum on segment there is Segmentation Fault 11.
#include
#include
using namespace std;
class Fenwick{
public:
Fenwick();
int sum(int l, int r);
void update(int pos,…

ratkke
- 469
- 1
- 4
- 13
-1
votes
1 answer
Optimizing Fenwick Tree (C++)
I have the following implementation of the Fenwick Tree algorithm.
#include
#define LSOne(S) (S & (-S))
using namespace std;
typedef long long int L;
int rsq(L b, L x[]){
int sum=0;
for(; b; b-=LSOne(b)) sum+=x[b];
…

Francis Salvamante
- 63
- 2
- 12
-1
votes
1 answer
multiplication using fenwick tree
We can do updation in a fenwick tree like adding a value and multiplication by a value. I have the following code for adding a value x to the element at position l .
while(l <= n-1)
{
tree[l] = tree[l] + x;
l = l + (l&(-l));
}
Similarly I…
user4637855
-3
votes
1 answer
What is the most optimal way of solving this problem?
I saw this coding question on an online coding competition but I couldn't find the most optimal solution.
Here is the question:
"You are given an array A of N integers and Q queries.Each query is of the following type :
1 pos val: Update the element…