fenwick-tree

Is it possible to query number of distinct integers in a range in O(lg N)?

落花浮王杯 提交于 2020-06-22 06:14:08
问题 I have read through some tutorials about two common data structure which can achieve range update and query in O(lg N): Segment tree and Binary Indexed Tree (BIT / Fenwick Tree). Most of the examples I have found is about some associative and commutative operation like "Sum of integers in a range", "XOR integers in a range", etc. I wonder if these two data structures (or any other data structures / algorithm, please propose) can achieve the below query in O(lg N)? (If no, how about O(sqrt N))

How do I efficiently multiply a range of values of an array with a given number?

只谈情不闲聊 提交于 2020-01-12 03:53:14
问题 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 : {1,2,6,8,10,12,14,16,9,10}; I know that Binary indexed tree can be used for the 'sum' part. How can I efficiently multiply a given range with a number? 回答1: If you want to actually modify the array, you can't do better than the naive linear algorithm: you have to iterate

How to find the total number of Increasing sub-sequences of certain length with Binary Index Tree(BIT)

﹥>﹥吖頭↗ 提交于 2019-12-20 09:45:30
问题 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 and 1,3,4 So, the answer is 2 . 回答1: Let: dp[i, j] = number of increasing subsequences of length j that end at i An easy solution is in O(n^2 * k) : for i = 1 to n do dp[i, 1] = 1 for i = 1 to n do for j = 1 to i - 1 do if array[i] > array[j] for p

Number of Increasing Subsequences of length k

北慕城南 提交于 2019-12-18 03:45:35
问题 I am trying to understand the algorithm that gives me the number of increasing subsequences of length K in an array in time O(n k log(n)). I know how to solve this very same problem using the O(k*n^2) algorithm. I have looked up and found out this solution uses BIT (Fenwick Tree) and DP. I have also found some code, but I have not been able to understand it. Here are some links I've visited that have been helpful. Here in SO Topcoder forum Random webpage I would really appreciate if some can

How to use Binary Indexed tree to count the number of elements that is smaller than the value at index?

淺唱寂寞╮ 提交于 2019-12-12 02:55:27
问题 The problem is to count the number of of values less than the value after index. Here is the code, but I can't understand how binary indexed tree has been used to do this? #include <iostream> #include <vector> #include <algorithm> #define LL long long #define MOD 1000000007 #define MAXN 10 using namespace std; typedef pair<int, int> ii; int BIT[MAXN+1]; int a[MAXN+1]; vector< ii > temp; int countSmallerRight[MAXN+1]; int read(int idx) { int sum = 0; while (idx > 0) { sum += BIT[idx]; idx -=

Length of longest chain of balanced parentheses in given ranges

萝らか妹 提交于 2019-12-11 09:31:53
问题 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: )( ())( Given a string of parentheses (length <= 1,000,000) and a list of range queries, find the maximum length of balanced parentheses within each of the ranges for each of the <= 100,000 queries (using 0-indexing for the ranges) Ex: ()))()(()) Range: [0,3] -

Maximum Sum of a non decreasing sub-sequence in an array using fenwick tree or BIT

孤街浪徒 提交于 2019-12-06 08:30:00
问题 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). 回答1: Maximum sum may be found using a dynamic programming algorithm. Scan the array and for each element add its value to the largest sub-sequence sum which is valid (sub-sequence ends with a value not greater than this element). Efficient implementation needs some way to quickly find

How to do a range update in Binary Indexed Tree or Fenwick Tree?

烂漫一生 提交于 2019-12-06 02:20:26
问题 I am trying to solve this problem in UVA with Binary Indexed Tree: Problem H Ahoy, Pirates! Input: Standard Input Output: Standard Output In the ancient pirate ages, the Pirate Land was divided into two teams of pirates, namely, the Buccaneer and the Barbary pirates. Each pirate’s team was not fixed, sometimes the opponent pirates attacked and he was taken away to the other pirate team. All on a sudden a magician appeared in the Pirate Land, where he was making transition of pirates from

Maximum Sum of a non decreasing sub-sequence in an array using fenwick tree or BIT

▼魔方 西西 提交于 2019-12-04 14:01:51
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). Maximum sum may be found using a dynamic programming algorithm. Scan the array and for each element add its value to the largest sub-sequence sum which is valid (sub-sequence ends with a value not greater than this element). Efficient implementation needs some way to quickly find maximum value in given sub-range. Augmented binary search tree may be used to do it. Fenwick tree is just an

How to adapt Fenwick tree to answer range minimum queries

空扰寡人 提交于 2019-12-03 13:34:03
问题 Fenwick tree is a data-structure that gives an efficient way to answer to main queries: add an element to a particular index of an array update(index, value) find sum of elements from 1 to N find(n) both operations are done in O(log(n)) time and I understand the logic and implementation. It is not hard to implement a bunch of other operations like find a sum from N to M. I wanted to understand how to adapt Fenwick tree for RMQ. It is obvious to change Fenwick tree for first two operations.