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

Update one item in segment tree

A part of a problem I'm solving involves getting the minimum in a range of an array (RMQ), so I implemented a segment tree and it works fine so far. Then I want to update one item in the original array (There are no updates with more than one) and…
Ayman El Temsahi
  • 2,600
  • 2
  • 17
  • 27
0
votes
2 answers

Number of points in a rectangle

I have N points denoted by (xi,yi). 1<=i<=N I have Q queries of the following form : Given a rectangle (aligned with x,y axes) defined by the points x1, y1, x2, y2 where (x1, y1) is the lower left corner and (x2, y2) is the upper right corner,…
habaddu arya
  • 119
  • 1
  • 1
  • 8
0
votes
0 answers

Can Lazy propogation be implemented for division queries?

How can lazy propogation be implemented for segment tree in which we have two types of queries, one for finding maximum element in a given range and other is to divide elements in a given range by a number( not necessarily same for all elements like…
swap96
  • 75
  • 1
  • 1
  • 11
0
votes
2 answers

Unexpected Compilation Error in Java

This is a small code that if the length of input array is power of two then the size of segment array is 2*(input_array.length)-1 else 2*(next power of 2 after input_array.length)-1 . public static int ByLogs(int n) { double y =…
Soumya Kanti Naskar
  • 1,021
  • 3
  • 16
  • 29
0
votes
0 answers

Updating values at indices which are factors of a number

Given an array with n integers. The following q queries of 2 types have to be handled : 1) Given k and c, add c to all indices which are factors of k 2) Output value at a given index Whats an approach to handle each query with O(logn)/O(1)…
Rohit
  • 1
0
votes
2 answers

(python)how to change the key conparation in dictionary?

I want to build a segment dictionary like segment tree. I mean I want to use sequence (11, 22) as key, if the input value like 11 it should be use the same key. how to do it. for example the dictionary is {(11, 22): 35, (44, 45):12}. I want to write…
user504909
  • 9,119
  • 12
  • 60
  • 109
0
votes
0 answers

Sum of all subsets in the range

Given an array A consisting of N elements and Q queries of type [l,r]. Print the sum of all possible subsets in the range [l,r]. Example: A[]= { 1, 2, 3 } & l= 1, r= 3; Sol: print {1}, {2}, {3}, {1+2}, {1+3}, {2+3}, {1+2+3} P.S I am using Segment…
suraj bora
  • 61
  • 1
  • 11
0
votes
0 answers

TimeLimitExceeded in finding maximum sum of a subarray

This is my solution for finding out the maximum sum of a subarray for a given array using Segment Tree (http://www.spoj.com/problems/GSS1/) but I am getting TLE (Time Limit Exceeded error) for this. Can anyone suggest more optimizations for this…
Pindaari
  • 408
  • 4
  • 9
0
votes
0 answers

Node Information for Segment Tree

Problem : Think about cars in a race as points on a line. All the cars start at the same point with an initial speed of zero. All of them move in the same direction. There are N cars in total, numbered from 1 to N. You will be given two…
Sarvagya
  • 89
  • 8
0
votes
2 answers

Segment Tree to compute frequencies

Is there any way to use a Segment Tree structure to compute the frequency of a given value in an array? Suppose there is an array A of size N, and every element A[i] of the array contains the value 0, 1 or 2. I want to perform the following…
Lucas Sampaio
  • 306
  • 1
  • 7
0
votes
1 answer

Lazy update on Segment tree

Im having a problem which need a structure that can handle 2 operations: Change values of nodes from position x to position y to newValue. Get the sum of values from position a to b. The number of nodes is 50000 and number of queries is 50000. Im…
0
votes
1 answer

How to use segment tree and scanline

Given 300000 segments. Consider any pair of segments: a = [l1,r1] and b = [l2,r2]. If l2 >= l1 and r2 <= r1 , it is "good" pair. If a == b, it is "bad" pair. Overwise, it is "bad" pair. How to find number of all "good" pairs among given segments…
0
votes
1 answer

Josephus Flavius on segment tree

I would like to solve Joseph Flavius problem using segment tree. I'm almost sure that simple simulation (i.e. lined list) is O(n^2). What I want to achieve is jumping on array for particular distance, taken from segment tree. In other words segment…
Michocio
  • 503
  • 3
  • 19
0
votes
1 answer

Finding maximum using segment tree giving wrong results

I am having this segment tree implementation to find maximum element in range : int TREE[10000000]={0}; int arr[100000]; int RMQ(int ss, int se, int qs, int qe, int index) { if (qs <= ss && qe >= se) return TREE[index]; if (se < qs || ss > qe) …
mat7
  • 297
  • 3
  • 11
0
votes
2 answers

Optimize a segment tree for range maximum queries?

So I need some help again.I recently started doing medium level problems on codechef and hence I am getting TLE quite a lot. So basically the question is to find the sum of of multiple maximum range queries given in the question.The initial range is…
gospelslide
  • 179
  • 2
  • 2
  • 12