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
2
votes
1 answer

Given an interval, Find all intervals In a list of Intervals

Let say I have a list of ranges like this [[1,3], [2,5], [4,6], [8,10], [12,15], [13,17]] Now I want to find a range say [3,11] falls in. My algorithm should give me all the ranges this range falls to. For example the output for this should…
Muthu Rg
  • 632
  • 1
  • 6
  • 18
2
votes
0 answers

Segment tree lazy propagation max query when update is non uniform

I am facing a problem in lazy propagation of segment tree. I have an array A, of length N ,of smaller arrays (of max length 20). I also have an array of indices B, referring to the index i am currently pointing to in the array Ai. There are 2…
Rajarshi basu
  • 330
  • 1
  • 10
2
votes
1 answer

Range minimum queries when array is dynamic

I have an array say A(0 indexed) of size 1. I want to find minimum in array A between indexes k1 (k1>=0) and A.size()-1(i.e the last element). Then I would insert the value : (minimum element in given range + some "random" constant) at the end of…
2
votes
2 answers

How can worst case complexity of quad tree O(N)?

As I read from sources,I learnt that worst case complexity of Quad tree is O(N) when the 2D matrix has only one dimension.I am not able to understand the reason for this. For eg. When matrix is just 1xm,we'll keep dividing it into two halves and…
2
votes
2 answers

Building of segment tree

Given an integer array A of n element and m query each query contain an integer x i have to answer number of element in a array less than x. 0 < A[i] < 10^6 && x < 10^6 example: A[]={105,2,9,3,8,5,7,7} query 6 8 104 answer 3…
puja
  • 31
  • 3
2
votes
1 answer

Lazy Propagation - HORRIBLE spoj

I am attempting the problem HORRIBLE from spoj and here's the link: http://www.spoj.com/problems/HORRIBLE/ I am trying to teach myself lazy propagation with segment tree. Following is the code I have written, I have tried to make it as concise and…
2
votes
3 answers

Segment tree with lazy propagation - Multiply all values in a range

I have the following code which for segment tree with lazy propagation which I could manage to write. This code does not work for multiplying all numbers in a range with a value, say x. I think I am doing something incorrect in the update_mult…
Aakash Anuj
  • 3,773
  • 7
  • 35
  • 47
2
votes
4 answers

Fast algorithm for adding a specific value to array elements in a range?

In a range update question, I want to add a value to array elements in a range. It can be assumed that the array (or some data structure) is sorted. A naive algorithm would be to loop through the starting element to the ending element and add a…
maregor
  • 777
  • 9
  • 32
2
votes
1 answer

Is it possible to perform a range addition update, adding a linear function to a max-segment tree?

I was thinking about this and was encountering a lot of bugs while trying to do this. Is it possible?
2
votes
1 answer

Interval tree of an an array with update on array

Given a array of size N, and an array of intervals also of size N, each a contiguous segment of the first array, I need to handle Q queries that update the elements of the array and that ask for the sum of an segment in the second array (sum of the…
2
votes
2 answers

Better than O(log(N)) base 2

I am solving Segment tree and Quad Tree related problems; while I noticed that in segment tree we split the 1D array into 2 (2^1) segments and recursively do this until base case arrives. Similarly, in Quad tree We subdivide the 2D grid into 4 (2^2)…
Kaidul
  • 15,409
  • 15
  • 81
  • 150
2
votes
0 answers

how a segment in a segment tree be deleted in O(log n) time?

I just finished reading about segment trees, the proof for insertion with time complexity O(log n) is quite convincing, but I was not able to figure out how deletion can be carried out with same complexity. Also I tried searching for the paper in…
S. Aditya
  • 35
  • 4
2
votes
1 answer

Segment tree implementation

I was learning segment tree from this page : http://letuskode.blogspot.com/2013/01/segtrees.html I am finding trouble to understand various fragments of code.I will ask them one by one.Any help will be appreciated. Node declaration: struct node{ …
aroup
  • 315
  • 3
  • 13
2
votes
3 answers

How to use large array size?

I was trying this problem on spoj. www.spoj.com/problems/RRANGE.It requires segment tree.But the problem is with the size of array.Here (1 <= N <= 1,000,000,000).Any way to work around this problem? Here is my implementation(gives correct answer for…
techriften
  • 421
  • 2
  • 16
2
votes
1 answer

How to find the kth smallest element within an interval of an array

Suppose I have an unsorted integer array a[] with length N. Now I want to find the k-th smallest integer within a given interval a[i]-a[j] (1 <= i <= j <= N). Ex: I have an array a[10]={10,15,3,8,17,11,9,25,38,29}. Now I want to find the 3-rd…
Dharsam1990
  • 121
  • 10