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

KGSS - Maximum Sum (SPOJ)

I solved the given problem as shown in the code But I am getting wrong answer. I used the Segment tree approach. node contains max1 (maximum no in given range) and sum(max sum of two no in given range). Please…
1
vote
1 answer

How to solve weighted Activity selection with use of Segment Trees and Binary search?

Given N jobs where every job is represented by following three elements of it. 1) Start Time 2) Finish Time. 3) Profit or Value Associated. Find the maximum profit subset of jobs such that no two jobs in the subset overlap. I know a dynamic…
1
vote
1 answer

Update Segment tree if the given value is less than the current value

I have been wondering if it is possible for a segment tree to be updated only if the updated new value is less than the current value. eg. a[i] to a[j] is to updated to x if(a[k]>x) a[k]=x; i<=k<=j How can this be done? Lazy propagation is what…
user5082201
1
vote
2 answers

Using lazy propagation for queries of more than 2 types

I have a question with a lot of queries which are of four types: Add to range. Initialize a range. Multiply a range with scalar. Find running sum over a range. Since queries are in huge numbers I have to use segment tree with lazy propagation but…
user3933528
  • 123
  • 2
  • 10
1
vote
0 answers

Addition, multiplication, substitution queries

Given a one dimensional integer array A of length N. We need Q queries each of one of following types: NOTE : M is fixed to be 10^9 + 7 Query 1 : 1 x y v : It means adding v to all element from x to y in 1 base indexed array and then take them…
ms8
  • 417
  • 2
  • 13
1
vote
0 answers

Spoj - Horrible using segment trees

I'm getting incorrect results for problem HORRIBLE Here is my code : http://ideone.com/1Yb3yp node query(int root,int l_most,int r_most,int l,int r) { if(l_most >= l && r_most <= r) return tree[root]; int l_child = (root<<1) , r_child =…
wen_dy
  • 31
  • 7
1
vote
1 answer

Lazy Propagation

I was solving GSS3 in spoj using lazy propagation in segment trees.I referred to this blog : Lazy Propagation How should i proceed in this question using lazy propagation and i couldn't understand how lazy array is being used in this…
Drave
  • 27
  • 5
1
vote
2 answers

Solving a using Segment Tree

You are given a sequence A of N (N <= 50000) integers between -10000 and 10000. On this sequence you have to apply M (M <= 50000) operations: modify the i-th element in the sequence or for given x y print max{Ai + Ai+1 + .. + Aj | x<=i<=j<=y…
user4415506
  • 109
  • 1
  • 7
1
vote
1 answer

Length of union of intervals online

I need a data structure that stores a set of intervals and allows to calculate the length of their union. Suppose that one has a set of intervals on the line with integer ends between 1 and n. Initially it is empty, and one can add or remove…
se0808
  • 556
  • 3
  • 18
1
vote
2 answers

Range in range sum with point update

We are given an array A with N elements and also N ranges, each of the form [L, R]. Call the value of a range the sum of all the elements in A from index L to index R, inclusive. Example : Let Array A = [2 5 7 9 8] and a range given is [2,4] then…
somu Boy
  • 19
  • 5
1
vote
2 answers

worst case running time of segment tree

How is the range sum in segment tree O(logn) worst case?? Shouldn't it be O(n)? What if during the range sum operation,we traverse down both the left and right nodes as per the algorithm?
1
vote
2 answers

Segment tree with lazy propagation for multiple of 3

Abridged problem: You're given an array of n elements, initially they are all 0. You will receive two types of query: 0 index1 index2, in this case you have to increase by one all elements in range index1 index2(included). Second type: 1 index1…
1
vote
0 answers

Minimum/Maximum value in a Binary Indexed Tree

I know how a BIT works. But I was wondering if a BIT can be used to find the minimum/maximum element in the complete range, or more specifically, to find the minimum (or maximum) value after all the update processes have been completed. Now, I know…
Born Again
  • 2,139
  • 4
  • 25
  • 27
1
vote
0 answers

Segment Trees in Solving Stabbing Queries

Where is it possible to find reference for an implementation of segment trees in solving the stabbing problem? The stabbing problem asks to return all intervals (from a predefined set of m intervals of the form [a, b], 1 <= a <= b <= n where n is…
1
vote
1 answer

How to code 2D segment tree?

I was solving the problem http://www.spoj.com/problems/LIS2/ on spoj. I tried for days but could not come up with a solution that could pass(time wise). Then I googled and found people talking about 2D segment tree.I searched a lot but could not…
sasha sami
  • 525
  • 1
  • 10
  • 24