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

How to determine how many elements from a range are within another given range?

I need a little help trying to figure out something: Given a sequence of unordered numbers (less than 15.000) - A - I must answer Q queries (Q <= 100000) of the form i, j, x, y that translate as the following: How many numbers in range [i,j] from A…
3
votes
2 answers

Range/Segment Tree Ruby

I am looking for a range or segment tree implementation in Ruby. I could not find any sample or gem available. Does anyone have a sample code ? Thanks,
Paul
  • 31
  • 1
3
votes
2 answers

Query the count of elements for which A[i] > a and B[i] > b given two unordered lists

Consider two arrays A and B. The element at index i in array A is associated with element at index i in array B. We can think of them as a pair. We have some queries q in form of (a, b). We need to find the count of all such elements for which A[i]…
3
votes
1 answer

Counting inversions in a segment with updates

I'm trying to solve a problem which goes like this: Problem Given an array of integers "arr" of size "n", process two types of queries. There are "q" queries you need to answer. Query type 1 input: l r result: output number of inversions in…
Arrow
  • 389
  • 2
  • 12
3
votes
0 answers

How can we calculate weighted cumulative sum of squares with prefix range updates by one?

Given an array A with n elements which starts with all 0s and another array W also with n elements (all greater than 0), we want to perform the following operation repeatedly; For a given k, increment A[0], A[1], .... A[k] each by 1, and report the…
piedpiper
  • 1,222
  • 3
  • 14
  • 27
3
votes
3 answers

Number of occurrences of each distinct integer in given ranges for an array

Given an array of n integers (n <= 1e6) [a0, a1, a2, ... an-1] (a[i] <= 1e9) and multiple queries. In each query 2 integers l and r (0 <= l <= r <= n-1) are given and we need to return the count of each distinct integer inside this range (l and r…
3
votes
2 answers

Find the largest subarray with all ones

Given a binary array (element is 0 or 1), I need to find the maximum length of sub array having all ones for given range(l and r) in the array. I know the O(n) approach to find such sub array but if there are O(n) queries the overall complexity…
efex09
  • 417
  • 2
  • 12
3
votes
2 answers

How is it possible to apply the lazy approach to update a segment tree if the update is more complex than simple addition or multiplication?

Consider this question. In this segment tree solution, we are updating all nodes of the tree in the given range. Is it possible to apply lazy propagation to this problem? Edit: Consider that in every update operation arr[i] = (1-(1-arr[i])*a),…
3
votes
1 answer

How to apply range updates in segment tree?

I have an already made a persistence segment tree but now there is a range update from some index to some index. How to update the persistence segment tree with less complexity? I am just rebuilding the persistence segment tree
Souravirus
  • 301
  • 2
  • 14
3
votes
3 answers

Perimeter of union of N rectangles

I want to know the efficient way to solve this problem: Given N rectangles that given a top-left and bottom-right corner, please find the perimeter of union of N rectangles. I only have O(N^2) algorithm and it's too slow, so please find more…
square1001
  • 1,402
  • 11
  • 26
3
votes
1 answer

Meeting scheduler algorithm. Case for segment tree?

There are n number of conference rooms available in a company for meetings. You need to book a meeting for a particular time slot. Write an algorithm to determine the number of conference rooms available for the meeting with given start time and end…
user3259176
3
votes
1 answer

Segment Tree : each query requires updating all nodes

Given an array where each element contains 2 numbers : a(i),b(i) i.e array[0] = pair(a(0),b(0))... array[i] = pair(a(i),b(i)) There are two type of operations : UPDATE i,c,d : array[i] = pair(c,d) i.e array[i] is updated to new pair QUERY x :…
MasterMind
  • 49
  • 1
  • 2
3
votes
3 answers

Sum of distinct element in a range

Consider a array (0 based index) i have to find the sum of distinct element of all possible range[i,n] where 0< i < n Example: arr={1,2,1,3} sum_range[0,3]={1,2,3}=6 sum_range[1,3]={1,2,3}=6 sum_range[2,3]={1,3}=4 sum_range[3,3]={3}=3 O(n^2)…
nil96
  • 313
  • 1
  • 3
  • 12
3
votes
3 answers

Segment tree - query complexity

I am having problems with understanding segment tree complexity. It is clear that if you have update function which has to change only one node, its complexity will be log(n). But I have no idea why complexity of query(a,b), where (a,b) is interval…
kkralj
  • 125
  • 2
  • 7
3
votes
2 answers

Closest equal numbers

Suppose you have a1..an numbers and some queries [l, k] (1 < l, k < n). The problem is to find in [l, k] interval minimum distance between two equal numbers. Examples: (interval l,k shown as |...|) 1 2 2 |1 0 1| 2 3 0 1 2 3 Answer 2 (101) 1 |2 2| 1…
Alex Hoppus
  • 3,821
  • 4
  • 28
  • 47
1 2
3
18 19