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

Range minimum Query

This is rmq using segment tree. But i am not getting correct output can anybody tell me where i am wrong. ` #include #include #define inf 1e9 using namespace std; int get_mid(int a,int b){ return a+(b-a)/2; } int min(int a,int…
Rajat Sharma
  • 47
  • 1
  • 15
0
votes
1 answer

Range update in BIT with a condition

Can I perform range updates in BIT with a condition. Lets say I have an array of negative and positive frequencies A[] = {1, -3, -4, 5, 9}. And I wanted to range update the values of the array with a condition : If the update value(x) is negative,…
RAGHU RAMAN
  • 537
  • 4
  • 16
0
votes
0 answers

Error in segment tree with lazy propagation

This is the problem of the SPOJ "SEGSQRSS - Sum of Squares with Segment Tree" the link to the problem is http://www.spoj.com/problems/SEGSQRSS/ I'm trying to do it using lazy propagation but cannot find the correct solution, can anyone help where…
rap
  • 9
  • 4
0
votes
0 answers

querying sub array sum using segment tree complexity

I understand that segment trees can be used to find the sum of sub array of A. And that this can done in O(logn) time according to the tutorial here. However I'm not able to prove that the querying time is indeed O(logn). This link (and many others)…
0
votes
1 answer

Is there a particular way to delete a record from a file in c without creating a copy of it?

I am currently using fseek() in program. I have also maintained an array of bytes which keeps the track of bytes of each line that is printed inside the file.I am also using a segment tree, so that the access to a particular position is faster.…
Palash Ahuja
  • 522
  • 1
  • 5
  • 15
0
votes
1 answer

My code gets a TLE (time limit exceeded) on an online judge even though I have coded according to the editorial

This is the question link - QSET - Codechef This is the editorial link - QSET - Editorial Basically the question queries for the number of substring in some range [L, R]. I have implemented a segment tree to solve this question. I have closely…
Saket Mehta
  • 2,438
  • 2
  • 23
  • 28
0
votes
1 answer

Find sum of all elements of an array excluding index L to R

We have an array arr[0 . . . n-1]. We should be able to Find the sum of elements from index L to R where 0 <= L <= R <= n-1 . Change value of a specified element of the array arr[i] = x where 0 <= i <= n-1. This can be solved using segment tree…
0
votes
1 answer

HackerEarth Challenge-- Deepu and Array

[ The Challenge is Over ] Problem: An Array of positive elements. Deepu Wants to reduce the elements of the array. He calls a function Hit(X) which reduces the all the elements in the array which are greater than X by 1. he will call this array many…
Ritesh
  • 497
  • 4
  • 15
0
votes
1 answer

Is there simple implementation of segment tree for python?

I need segment tree for solving of my task. Should I develop my own segment tree, or there is any good implementation of such tree? I need the following operations: Segment update: tree.add(from, to, value) Segment sum: tree.sum(from, to)
Max
  • 1,803
  • 3
  • 25
  • 39
0
votes
1 answer

Query code unexpectedly modifies array instead of returning the index

In this tutorial, why does the query() function modify the content of M in the last 4 lines of code, instead of just returning the node index? I think there's a problem with his approach: the modified M array won't be able to handle future RMQ…
Ritesh
  • 497
  • 4
  • 15
0
votes
1 answer

segment tree range minimum query

I am trying to understand segment trees. This is a great tutorial that shows how to find a minimum in range. However, it is said there that "All levels of the constructed segment tree will be completely filled except the last level. Also, the tree…
Bob
  • 10,427
  • 24
  • 63
  • 71
0
votes
1 answer

Element rotation in segment tree

Given an array A with N numbers(all positive and contain less than or equal to 4 digits), 2 types of queries are to be supported. There are total M queries. Update a range given by indices L,R(both inclusive) by K. Return the maximum element in a…
0
votes
1 answer

segment tree with lazy propagation in large range

Can anyone please tell me how lazy propagation works in a segment tree if we want to update values in a range? Also, how would we do this problem using a segment tree and lazy propagation? Suppose there are 15 boys in a row, standing facing east,…
Avneet
  • 119
  • 2
  • 12
0
votes
1 answer

GSS1 -SPOJ - Segment tree TLE

I'm solving this problem by using a segment tree. I am saving the sum, max, leftmost max, and the right most max at every node. I then search the graph to find the answer to a specific interval. How could I increase the speed of this code? import…
The Bear
  • 199
  • 2
  • 8
0
votes
0 answers

Storing Vectors or Queues at a node in Segment Trees in Java

I am writing a lazy propagation code using the segment tree data structures. For doing the lazy propagation I wanted to store a list of integers at a node in the segment tree and while carrying the lazy propagation I wanted to append the list of…
user1580096
  • 487
  • 1
  • 7
  • 17