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
6
votes
4 answers

What does "x += x & (-x)" mean?

I found that many people use x += x & (-x), x -= x & (-x) to solve the interval tree problem (While implementing data structures like segment tree, binary indexed tree etc). Can you explain what that equation means? For Example: void update(int m,…
6
votes
1 answer

Algorithm for finding the lowest price to get through an array

I've been thinking a lot about the following problem: We are given an array of n numbers. We start at the first index and our task is to get to the last index. Every move we can jump one or two steps forward and the number at the index we jump to…
6
votes
2 answers

How to count number of inversions in an array using segment trees

I know that this problem can be solved using modified merge sort and I have coded same. Now I want to solve this problem using Segment Tree. Basically, if we traverse from right to left array then we have to count the "how many values are greater…
Namit Rawal
  • 61
  • 1
  • 5
5
votes
3 answers

Is there a way to quickly find in a range contains a number that is in a given range?

So here is a problem, I am given an integer array, whose number is all distinct, let's say it is int[] data = {21, 34, 12, 88, 54, 73}; now that I would like to see if a subarray, or a range, contains a number is in a range(which is also given). In…
lier wu
  • 620
  • 7
  • 23
5
votes
1 answer

Count the number of ranges [L; R] which has difference between the maximum and minimum is even

Given an array n elements (n <= 10^5) Count the number of ranges [L; R] (L <= R) which has difference between the maximum and minimum is even. For example, n = 5 a[] = {4, 5, 2, 6, 3} The answer is 11: [1;1], [1;4], [1;5], [2;2], [2;4], [2;5],…
5
votes
4 answers

Segment tree memory

I have seen many implementations of segment trees that use O(4*N) memory. Is there anyway for a segment tree to use exactly 2*N-1 memory? I cannot find any implementation that does this. Even if they talk about the space complexity being actually…
Andrew
  • 1,109
  • 1
  • 15
  • 27
5
votes
4 answers

Update a value of a Array for a given Range

have given an array initially having some Max.val value , then there are queries making a update in a Range L,R, such that the value at any position is minimum. For Example: Update Range 1 3 with value 3 Array 3 3 3 Max.val Max.val Now update 2…
user4996457
  • 161
  • 1
  • 7
5
votes
1 answer

Segment tree: amount of numbers smaller than x

I'm trying to solve this problem. I found tutorial for this problem but I don't get how to build segment tree that will find amount of numbers less than x in O(log n) (x can change). In tutorial it has been omitted. Can anyone explain me how to do…
Badf
  • 325
  • 2
  • 10
5
votes
2 answers

Finding the minimum element in a given range greater than a given number

We are given N (N <= 106) points on a 2D plane and an integer D (N <= 106), we want to find two points p1,p2 (p2 to the right of p1) such that the difference between p1.y and p2.y is at least D and p2.x - p1.x is minimized. The x and y axis are in…
2147483647
  • 1,177
  • 3
  • 13
  • 33
5
votes
2 answers

Range update and querying in a 2D matrix

I don't have a scenario, but here goes the problem. This is one is just driving me crazy. There is a nxn boolean matrix initially all elements are 0, n <= 10^6 and given as input. Next there will be up to 10^5 queries. Each query can be either set…
user2040997
  • 373
  • 1
  • 4
  • 7
5
votes
2 answers

How can I implement segment trees with lazy propagation?

I am implementing a segment tree, to be able to answer quickly to the following queries in an array A: query i, j: sum of all elements in range (i,j) update i, j, k: add k to all elements in the range(i,j) Here is my implementation: typedef long…
Rontogiannis Aristofanis
  • 8,883
  • 8
  • 41
  • 58
5
votes
3 answers

How to update a range in segment tree while maintaining max and min?

I'm implementing segment tree from an array of data, and I also want to maintaining the max/min of the tree while updating a range of data. Here is my initial approach following this tutorial…
roxrook
  • 13,511
  • 40
  • 107
  • 156
4
votes
1 answer

Segment Tree with lazy propagation Time limit problem

The following is the implementation of http://www.spoj.pl/problems/LITE/ using Segment Tree's with lazy propagation. I am new to segment trees and I cannot understand why I am getting TLE. Could someone please look at it and help me correct my…
Ronzii
  • 247
  • 1
  • 3
  • 12
4
votes
4 answers

Convert a given array with all zeros to a target array

Recently I came across an interview question. I tried solving it but interviewer was looking for a better solution. The question is: Given a source array containing zeroes and target array containing numbers, return the smallest number of steps…
danish sodhi
  • 1,793
  • 4
  • 21
  • 31
4
votes
1 answer

Segment Tree Query for Maximum Number

I have given a an array A of n integer , and Q query in the form of X D for each query i have to find the maximum element in sub array [Ax , A(x-D),A(x-2D)..] For example: A = [1,2,3,4,5,6,17,8] we have query 7 2 Sub Array [17,5,3,1] Ans=17 How can…
Narendra Modi
  • 851
  • 1
  • 13
  • 29
1
2
3
18 19