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
0
votes
0 answers

Tree query algorithm fails test cases

A tree is a simple graph in which every two vertices are connected by exactly one path. You are given a rooted tree with vertices and a lamp is placed on each vertex of the tree. You are given queries of the following two types: 1 v: You switch…
0
votes
1 answer

Segment Tree - visiting at most 4 vertices at every level of the tree

What is the proof or explanation behind the fact that at most 4 nodes per level are visited in a segment tree?
user12566179
0
votes
0 answers

Segment tree with a matrix

I ma trying to find the min value of rows in a matrix depending on a queries. For example: 0 1 2 3 _ _ _ _ 0 - |2|1|3|2| 1 - |1|3|5|1| 2 - |5|2|1|4| 3 - |2|4|2|1| if I was given a query for row 1, from col 1-2 the min is 3. Etc. I am…
Willdomybest18
  • 127
  • 3
  • 12
0
votes
1 answer

Why does a range query on a segment tree return at most ceil(log_2 N) nodes?

If an array A[1..N] is represented using a segment tree having sets in each interval, why does a range query [L..R] returns at most ceil(log_2 N) sets (or disjoint intervals)? If came across this statement while reading this answer. To quote: Find…
DoubtExpert
  • 71
  • 1
  • 10
0
votes
0 answers

How to build a segment tree without recursion?

I'm trying to understand the process of building a segment tree using Python. I've come up with a function like this (which works): arr = [ ... ] tree = [None] * (4*len(arr)) def build(v, vl, vr): if vl == vr: tree[v] = arr[vl] …
planetp
  • 14,248
  • 20
  • 86
  • 160
0
votes
1 answer

Lazy propagation range update

I was reading lazy propagation on GFG and it says following for range update For example consider the node with value 27 in above diagram, this node stores sum of values at indexes from 3 to 5. If our update query is for range 2 to 5, then we…
aditya rai
  • 67
  • 6
0
votes
0 answers

Why I am getting TLE for this code for GSS1 problem on SPOJ ? (Using Segment Tree)

Link to Problem:- https://www.spoj.com/problems/GSS1 I am using Segment tree. Node Information:- maxSum --> maximum sum of the segment represented by node. Sum ---> Total Sum of segment. (Although i think it's not needed) maxPrefixSum ---> maximum…
user12619063
0
votes
1 answer

What is the efficient way to solve range query other than segment tree in c++?

I am trying to understand segment trees. I tried a problem on hackerrank (link) and I tried to use the geeksforgeeks code in c++link. However, I got a dump value problem in line 19. int right = RMQUtil(st, mid+1, se, qs, qe, 2*index+2); // why this…
0
votes
0 answers

Can we use minimum segment tree to answer no of elements smaller than k within a certain range of array?

I want to use a minimum segment tree to answer no of elements which are less than k in a specific array range l to r in an unsorted array of integers. In the recursive function query I return the range of given tree element if the tree element lies…
0
votes
0 answers

Error in the problem of finding the maximum in the segment tree

When compiling the program throws this error: Unhandled exception at 0x000418B9 в laba6c++.exe: 0xC00000FD: Stack overflow (characteristic: 0x00000001, 0x00402FFC). I think the error is due to too many recursions of the maxim function, but I don't…
0
votes
1 answer

Find the longest common subarray with the same element in range l to r for multiple queries

Given an array, we need to find the longest contiguous subarray that has the same element in range l to r for multiple queries. For example, ar[] = {1,2,2,2,4,3,1,1,3}. Query 1: l=1,r=5, element=2, output will be 3 Query 2: l=1,r=5, element=1,…
Pratims10
  • 68
  • 6
0
votes
1 answer

Having difficulty in understanding recursion for building a segment tree

Suppose there's an array with n = 5 int [] arr = {1,2,3,4,5}; and int [] segmentTree = new int[n*4]; Here's the code for building this segmentTree buildSegmentTree(a , 1 , 0 , n-1); void buildSegmentTree(int [] a , int vertex , int leftLimit , int…
Zarc
  • 11
  • 2
0
votes
1 answer

Do I need to check if lo > hi when building a segment tree

The code below is a slightly modified version of popular segment tree code. My question is why do we need to do the lo > hi check when recursively building the tree, I cannot think of an example where lo will ever be greater than hi since at any…
0
votes
1 answer

How can I reduce the time complexity here?

The question states that we are given an empty list and 10^5 queries.For each query, we have two integers a and b.a can only be 1, 2 or 3. if a=1, then append element b in the list. if a=2, then add value b to every element of the list. if a=3, then…
Madfish
  • 85
  • 6
0
votes
1 answer

Divide an array into subarrays so that sum of product of their length and XOR is minimum

We have an array of "n" numbers. We need to divide it in M subarray such that the cost is minimum. Cost = (XOR of subarray) X ( length of subarray ) Eg: array = [11,11,11,24,26,100] M = 3 OUTPUT => 119 Explanation: Dividing into subarrays as =…
learner-coder
  • 307
  • 1
  • 10