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

Multiplication in a range

I have an array to 10 numbers supprse A[10] = {1,2,3,4,5,6,7,8,9,10} and I have to compute the multiplication of numbers in a particular range but not getting correct answer, I am using segment tree and dont know how to use query operation Here is…
avinashse
  • 1,440
  • 4
  • 30
  • 55
2
votes
1 answer

Segment Tree Kth Maximum

Hi I was solving the problem on a segment tree,But I am not able to make a little modification to the query function of segment tree . Actually What I want is my query function should return the (int)(TotalArrayLength/3) th maximum element between…
user1134599
1
vote
1 answer

Proof that height of segment tree is ceil(log(n))

I was reading up on this Quora answer on memory space required by segment trees. In the second paragraph, the author assumes that the height of a segment tree is ceil(log(n)), where n is the size of the array from which the tree is being…
Pratham Yadav
  • 69
  • 1
  • 8
1
vote
1 answer

Can Fenwick Tree and Segment Tree in C++ perform insertions and deletions in logarithmic time?

I apologize in advance for what may be a really dumb question. I've been reading about fenwick tree and segment tree a lot recently (specific implementation of segment tree is here:…
JohnZ
  • 87
  • 8
1
vote
1 answer

Finding number of zeros in a changing array

The problem is pretty much what the title says. There is an n-element(n<10^5) array, which consists of n zeros. There are q operations (q<2*10^5): Each operation can be one of two below: 1. Add x to all elements on a [l,r] range(x can be also…
dispenomi
  • 27
  • 5
1
vote
1 answer

Number of nodes in segment tree

I'm just wondering how many nodes can be in segment tree. I know there is formula for it and it is: 2n-1. But for example if we have 5 data in a segment tree will there be 9 nodes in the tree or 15? So following that example whether the total number…
Jan Tuđan
  • 233
  • 3
  • 17
1
vote
0 answers

CSES Dynamic Range Minimum Queries

https://cses.fi/problemset/task/1649 I'm solving this problem using Segment Trees and the solution I've written is #include #define MAX 1000000001 using namespace std; int n; vector tree; int sum(int a, int b) { a +=…
Benny
  • 41
  • 4
1
vote
0 answers

lazy propagation in segment tree when two changes could not be combined

Suppose there are two sequences A:a1,a2,...,an and B:b1,b2,...bn ,and we need two operations: 1.sum(i,j):calculate the sum of ai,a(i+1),...,aj 2.range_add(i,j):range modify the sequence A in [i,j] , with…
1
vote
1 answer

Sum of MEX of all subarrays of the given array

Find the sum of MEX of all subarrays of the given array. The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance: The MEX of [2,2,1] is 0, because 0 does not belong to the array.…
Nitin
  • 71
  • 1
  • 6
1
vote
1 answer

How to efficiently add a node into a segment tree for minimum range query?

I have a vector of objects T and I want to find a minimum in a given range. I implemented segment tree for an efficient searching. What is the efficient way to update the tree if I know that I will alternate push_back, pop_back and query. Is it…
1
vote
1 answer

Data structure to represent multidimensional ranges with fast "open space" lookup?

So I'm looking to represent non-overlapping ranges in an N dimensional space. I think CGAL has this functionality, and facilitates fast querying of points as the example shows below. What I'm not sure of is how to extend this kind of query to find…
Mikhail
  • 7,749
  • 11
  • 62
  • 136
1
vote
1 answer

Calculating sub array sums with a Fenwick tree

I have a problem and I don't know if I can solve it with a Fenwick tree. Here's the problem: I have an original array a = [8,4,7,0]. Now I iterate through the array and at each step I am interested in the sorted sub-arrays, which look like this:…
1
vote
1 answer

Getting Wrong Answer on CSES Problem Set Hotel Queries

https://cses.fi/problemset/task/1143/ I am trying to solve this problem, I used Segment Tree for solving it. I tried every possible test cases but it is not getting accepted. Just failing in one test case which is too big. Please help me out to…
Abhishek Kumar
  • 191
  • 1
  • 11
1
vote
2 answers

Segment Tree Build?

I am getting conflicting evidence on the build time complexity for a recursive segment tree. Some sources(wikipedia) claim it's O(N*log(N)), while others claim it's O(N). My intuition says it's O(N), because we have 2N nodes and 2N-1 edges. Which…
timg
  • 381
  • 2
  • 13
1
vote
0 answers

Given array of n integers. How to get the range-sum after some range updates?

Given an array of n elements. Initially all of them are zero. We need to process three type of queries: assign value v to all elements on the segment from l to r, add v to all elements on the segment from l to r, find the sum on the segment from l…
Yeasin Mollik
  • 531
  • 6
  • 13