Questions tagged [segment-tree]

A segment tree is a heap-like data structure that can be used for making update/query operations upon array intervals in logarithmical time.

A segment tree is a balanced tree where each node corresponds to an interval. The leaves correspond to the atomic intervals according to left to right order. An internal node u corresponds to the union of the intervals corresponding to the leaves of the subtree rooted at u.

A segment tree for a set of n intervals can be constructed in O(nlogn) time.

A d-dimensional segment tree for a set of n axis-aligned rectangular boxes in R d can be built in O(n(logn)^ d ) time and takes O(n(logn)^ d ) space.

273 questions
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); …
1
vote
0 answers

What is the need to finish a segment tree with zeroes where elements are empty?

I work with segment trees now. It is known that segment trees has 2^n leaves. Let us imagine an array data that consists of 5 elements [1, 2, 3, 4, 5] and is basic for a segment tree. We also have an array tree_data containing the tree itself (the…
coder-coder
  • 323
  • 4
  • 13
1
vote
1 answer

Compute sum of (min*max) across all subarrays of the array

given N elements of an array compute the sum (min*max) across all the subarrays of the array. e.g. N = 5 Array: 5 7 2 3 9 output: 346 (5*5 + 7*7 + 2*2 + 3*3 + 9*9 + 5*7 + 2*7 + 2*3 + 3*9 + 2*7+2*7 + 2*9 + 2*7 + 2*9 + 2*9) here is the complete…
keemahs
  • 780
  • 11
  • 14
1
vote
2 answers

Sum of all numbers less than k in a range using segment tree

I have an array A with elements size <= 10^6. I want to implement a data structure which gives me sum of all elements less thank k in a particular range say l to r. I know it can be solved using segment tree but dont know how to maintain segment…
alex
  • 39
  • 4
1
vote
1 answer

Why is the recursion returning NoneType in the following code of segment Tree?

I tried to build segment tree using python, using which I can get sum of digits in array 'arr' from index (l,r). I am getting expected tree correct but it shows error when I call getSum(). from math import ceil,log2 def…
1
vote
2 answers

Minimum number in a range with specific property

I have a vector of pairs of length N. Now i have Q queries of type given range let say L to R, find the minimum value of second element of pair whose first element is equal to given number K. For example : vector of 6…
tempEngineer
  • 223
  • 1
  • 3
  • 13
1
vote
1 answer

Problem in implementing Persistent Segment Tree

I am trying to implement Persistent Segment Tree. The queries are of 2 types: 1 and 2. 1 ind val : update the value at ind to val in the array 2 k l r : find the sum of elements from index l to r after the kth update operation. I have implemented…
Nikhil Rathore
  • 170
  • 2
  • 14
1
vote
1 answer

C++ Segmentation fault while building a segment tree

I am trying to build a segment tree to get to calculate sub array with max sum in given interval of an array... (for each query) I am getting segmentation fault in the function "void build()" I have checked with array size....this works fine when…
1
vote
1 answer

Segment tree doesn't take care of all segments of input data. Is there an efficient date structure that does?

Segment tree is an efficient, but not always completely useful date structure. For example: if we have an array of length 8, it will take care of segments 1-2, 3-4, 5-6, 7-8, 1-4, 5-8 and 1-8. But many will be left out, such as 2-3, 2-4, 4-5, 6-7…
Hinko Pih Pih
  • 201
  • 1
  • 8
1
vote
2 answers

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 => x1≠x2, y1≠y2, z1≠z2 v1>v2 if and only if…
1
vote
1 answer

Find closest number to P in a given range of array

An array A is given to you containing N integers. You need to operate Q queries on the array. Queries are of two types. 1 U P: You need to update value of Au to P. 2 L R P: You need to find Ak such that Ak - P is minimum and Ak > P and…
Rohit
  • 41
  • 8
1
vote
1 answer

Range minimum query, dynamic array, interval tree, treap

I need an algorithm with some data structure in Python that at every step when two new elements e1, e2 are given: finds the insertion positions (conserving the order) of the first and the second given elements. finds the maximum value of the…
David
  • 33
  • 4
1
vote
3 answers

Range Queries on a path in a tree

I came across this question in a contest (which is now over) and I am not able to think of a time-efficient algorithm. You are given a rooted Tree of N ( <=10^5) nodes . Initially all nodes have value 0.There would be M updates (<=10^5) to the tree…
Alzio
  • 45
  • 9
1
vote
1 answer

maximum difference between two elements of an array in a given range

I need to efficiently find the max{S} such that S=(A[i]-A[j]) in a given range [i,j]. I am not able to find the solution other than the brute force solution where for every query iterate the array from index i to index j and find the solution. I am…
user157920
  • 43
  • 4
1
vote
1 answer

How to answer queries of type l,r,k which finds number of elements in an array in range l to r which occurs atleast k times?

How to compute answer to queries of type l,r,k which finds number of elements in an array which occurs atleast k times in range l to r ? How to use Mo's algorithm?
learner
  • 11
  • 1