29 questions
1
vote
1
answer
337
views
How to Use Binary Indexed Tree for Counting Reverse Pairs in an Array?
I'm currently attempting to solve the Reverse Pairs problem on LeetCode, where I need to count the number of reverse pairs (i, j) in an array nums such that 0 <= i < j < nums.length and nums[...
0
votes
1
answer
359
views
Why we need two fenwick tree to get range sum of [l, r] given there are array of numbers and range updates and queries are performed on the array?
Given array of numbers of size n. We will perform 2 operations such as:
Update: update range of numbers (l, r)
Query: sum of numbers (l, r).
To compute range update, range queries can we use only one ...
1
vote
1
answer
332
views
Quick Way of Finding How many Substrings has first and last character repeated inside
This is a problem about substrings that I created. I am wondering how to implement an O(nlog(n)) solution to this problem because the naive approach is pretty easy. Here is how it goes. You have a ...
3
votes
0
answers
316
views
Binary Indexed Tree: Why does "i + lowBit(i)" work?
I know that if we want to update node with index i, we need to recursively update node i = i + lowBit(i) until the new value exceeds the size of the binary indexed tree.
My question is: how to prove ...
0
votes
1
answer
535
views
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, ...
0
votes
1
answer
3k
views
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 ...
3
votes
0
answers
270
views
How can we calculate weighted cumulative sum of squares with prefix range updates by one?
Given an array A with n elements which starts with all 0s and another array W also with n elements (all greater than 0), we want to perform the following operation repeatedly;
For a given k, ...
7
votes
4
answers
11k
views
What does "x += x & (-x)" mean?
I found that many people use x += x & (-x), x -= x & (-x) to solve the interval tree problem (While implementing data structures like segment tree, binary indexed tree etc).
Can you explain ...
0
votes
1
answer
81
views
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 ...
4
votes
1
answer
660
views
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 ...
1
vote
2
answers
140
views
Count "minimal" values
Problem:
I have an input of n vectors:
(x, y, z): x ∈ {1..n},y ∈ {1..n},z ∈ {1..n} (every "dimension" is set(1..n))
*I mean that in one vector x,y,z can be the same(x=y=z),
but for ∀v1,v2 =&...
3
votes
0
answers
306
views
Difference between Binary Search Tree and Binary Index Tree
Is Binary Index Tree and Binary Search Tree are same thing?
If not whats the actual difference between them and when to use what?
1
vote
1
answer
534
views
Answer queries about the number of distinct numbers in a given range
The problem
I have an array with N numbers. The numbers may be disctints and may also be unordered. I have to answer Q queries about how many distinct numbers there are between A and B. Where A, B ...
1
vote
0
answers
146
views
how to get inversion count with update
Inversion count in an given array is a very famous with a time complexity of O(NlogN). However, I wonder whether there is a way to do it with update.
Input format:
first line consist of an integer n;
...
0
votes
1
answer
683
views
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 ...
0
votes
1
answer
334
views
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 >'...
1
vote
1
answer
560
views
Application of binary indexed tree
I was trying to solve this algorithmic problem and I came across this nice solution:
"The idea is to treat the ai, bi and
ci asymmetrically. The BIT supports minimum
queries for key intervals ...
1
vote
1
answer
788
views
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 ...
0
votes
1
answer
542
views
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 ...
0
votes
1
answer
396
views
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 ...
0
votes
1
answer
345
views
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 ...
-1
votes
1
answer
99
views
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 ...
-2
votes
1
answer
205
views
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 ...
2
votes
0
answers
329
views
How to solve problems with Binary Indexed Tree?
This question sounds very vague and needs some explanation:
I learned about Binary Indexed Tree a few weeks ago. This data structure is a brilliant design. It actually took me very long to figure out ...
-1
votes
1
answer
67
views
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 ...
1
vote
2
answers
3k
views
Can someone please explain the solution to "Almost sorted intervals" to me?
Here is the problem, and here is a solution.
First part is simple enough. It's the second part that I don't get, no matter how hard I try. You basically have two sets of intervals and need to find ...
1
vote
2
answers
364
views
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 ...
7
votes
1
answer
6k
views
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
...
2
votes
1
answer
1k
views
Brackets matching using BIT
edit: I was trying to solve a spoj problem. Here is the link to the problem :
http://spoj.pl/problems/BRCKTS
I can think of two possible data structures for solving the problem one using segment tree ...