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
2
votes
0 answers
Adding a Geometric Progression (GP) on any segment of an array using Segment Trees
I know how to make a Range Update on a Segment Tree involving Addition of a constant or Addition of an AP to any given segment of an array by lazy propagation and making subsequent queries about the sum of any given segment but cannot apply the same…

user3780367
- 21
- 2
1
vote
1 answer
Can Fenwick Tree and Segment Tree in C++ perform insertions and deletions in logarithmic time?
I apologize in advance for what may be a really dumb question. I've been reading about fenwick tree and segment tree a lot recently
(specific implementation of segment tree is here:…

JohnZ
- 87
- 8
1
vote
0 answers
Optimise array calculation in python
The task:
You are given an array a of 0 < N ≤ 100000 elements and 0 < M ≤ 100000 queries. Each query can be one of two types:
? l r k — REQ(l, r, k)
or
+ i delta — inc(i, delta): increase a[i] value by delta.
It is guaranteed that elements of array…

Lily
- 11
- 3
1
vote
1 answer
Dynamic Range Sum Queries
I was solving a CSES problem. It's a simple, Fenwick tree problem, and I write the code, which works perfectly on smaller inputs but giving wrong answers for larger inputs.
the problem link: https://cses.fi/problemset/task/1648/
my solution to the…

anand singh
- 103
- 7
1
vote
1 answer
Calculating sub array sums with a Fenwick tree
I have a problem and I don't know if I can solve it with a Fenwick tree.
Here's the problem:
I have an original array a = [8,4,7,0]. Now I iterate through the array and at each step I am interested in the sorted sub-arrays, which look like this:…

Johannes Heß
- 37
- 6
1
vote
1 answer
Point Updates in Fenwick Tree
I have a hard time understanding how does adding LSB to the current index gives us the next place which contains the given point.
void update(int k, int x) {
while (k <= n) {
tree[k] += x;
k += k&-k; // adding LSB (least…
user13040283
1
vote
0 answers
Implementation of Fenwick Tree gives TLE
Link to Code Pastebin
#include
using namespace std;
vector update(int x,long long int val,vector b,int n)
{
b[x]+=(val);
x+=(x&-x);
while(x<=n)
{
b[x]+=(val);
…

Rider
- 23
- 4
1
vote
1 answer
range XORed sum using BIT or Fenwick tree
For a given array of integers, we have to calculate XORed sum withing a given range [L, R], by XORed sum I mean Σ(Arr[i]^p) where i:[L,R] and p is some number. This can be easily done while calculating the XORed sum till every i-th element in array…

Roshan
- 150
- 16
1
vote
0 answers
What is the approach to solve spoj KPMATRIX?
The problem link is here. The problem is basically to count all such sub matrices of a given matrix of size N by M, whose sum of elements is between A and B inclusive. N,M<=250. 10^-9<=A<=B<=10^9.
People have solved it using DP and BIT. I am not…

sherelock
- 464
- 5
- 9
1
vote
1 answer
Range update/ Query Binary Index Tree
Problem Statement:
https://www.hackerrank.com/contests/epiccode/challenges/square-array
Given an array of integers A1,A2,…AN, you have to perform two types of
queries in the array.
1 x y. Add 1×2 to Ax, add 2×3 to Ax+1, add 3×4 to Ax+2, add 4×5…

Nakshatra
- 663
- 1
- 6
- 14
1
vote
0 answers
Why does the function RangeUpdate() for a Fenwick Tree not giving the correct output?
I've implemented Fenwick Tree (BIT) with a function RangeUpdate() to update (increment) every element into a range by some value. All other functions i've implemented works fine except this. It doesn't increment every element by the given value.…

Abdullah Al Imran
- 1,166
- 2
- 17
- 30
1
vote
1 answer
How to find the unique digits in a given range of an array efficiently?
Given an array (not sorted) and few range queries. For each query I need to find the number of unique digits within the given range. Here is the naive approach I came up with.
#include
#include
using namespace std;
int main()
{
…

Wasim Thabraze
- 780
- 1
- 12
- 29
1
vote
1 answer
Length of longest chain of balanced parentheses in given ranges
First, define a string of "balanced" parentheses as a string such that
for every '(' there is one unique, matching ')' somewhere after that
'('.
For example, the following strings are all "balanced":
()
()()
(())()
while these are not:
)(…

1110101001
- 4,662
- 7
- 26
- 48
1
vote
1 answer
Fenwick Tree Java
I tried to implement the Fenwick Tree in Java, but I am not getting the desired result.
Here is my code:
import java.io.*;
import java.util.*;
import java.math.*;
class fenwick1 {
public static int N;
public static long[] a;
public…

user2784016
- 179
- 1
- 7
1
vote
1 answer
Counting number of points in lower left quadrant?
I am having trouble understanding a solution to an algorithmic problem
In particular, I don't understand how or why this part of the code
s += a[i];
total += query(s);
update(s);
allows you to compute the total number of points in the lower left…

1110101001
- 4,662
- 7
- 26
- 48