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

I am getting segmentation fault for this code?

i have an 2d vector of m*3 size, of which first col : lower range, second col:upper range,third col:value.and i have and initial 1-D array of size n(m void build_tree(long long int *tree,long long int index,long long int s,long long int e) { …
user11841867
0
votes
1 answer

finding gcd over a range and update

given an array and some queries Each query can one of the following two types: 1.Calculate query, where given a range of indices, find the Greatest Common Divisor(GCD) of all the numbers in this range 2.Update query, where given a range of indices,…
0
votes
1 answer

Unable to find error sement tree : minimum in subarray

I am new to data structures and algo, and unable to find error in my code for the question Range Minimum Query Given an array A of size N, there are two types of queries on this array. q l r: In this query you need to print the minimum in the…
Rohan park
  • 61
  • 1
  • 4
0
votes
0 answers

Closest point from current point

Input: A 2-dimentional space of size MxN . K points(integer pair) in this 2 dim space. Output: A point xj, yj from set of K points such that dist(i,j) in minimum where xi, yi is the current point. dist(i,j) : is root mean square distance Given K…
ishandutta2007
  • 16,676
  • 16
  • 93
  • 129
0
votes
1 answer

segment tree correct but query output is not

I tried to implement segment tree algorithm for finding range minimum query in Java. Here is my complete java code. It builds the segment tree from an array. and then prints the minimum element in every range. the problem is, tree is correct (as far…
0
votes
0 answers

Garbage value while building segment tree

I have just started learning segment tree. I have tried to build the segment treeusing array. In each parent node, I will store sum of left and right child. Below is my code : #include using namespace std; void build(int *A, int *tree,…
Devendra Verma
  • 975
  • 2
  • 10
  • 24
0
votes
4 answers

Unable to solve a homework (ACM-training)

I have no idea how to solve this: http://acm.sgu.ru/problem.php?contest=0&problem=311 Please help me with this one I know it can be solved using segment tree but I don't know how
user182513
0
votes
1 answer

How to use segment trees to find two smallest numbers and one maximum number in a range in an unsorted array?

I am currently using segment tree to find out the smallest and largest number in a range in an unsorted array by comparing and storing the smallest and largest of sub-arrays. However, I am getting confused on what information needs to be saved…
user3243499
  • 2,953
  • 6
  • 33
  • 75
0
votes
1 answer

Why solving a range minimum query with segment tree time complexity is O(Log n)?

i was trying to solve how to find in a given array and two indexes the minimum value between these two indexes in O(Log(n)). i saw the solution of using a segment-tree but couldn't understand why the time complexity for this solution is O(Logn)…
0
votes
0 answers

Hackerrank maximizing mission points solution

I need help to debug my code for the following challenge - https://www.hackerrank.com/challenges/maximizing-mission-points/problem I've followed the lines of code from - https://www.geeksforgeeks.org/segment-tree-set-1-sum-of-given-range/. My code…
user1858851
  • 127
  • 1
  • 1
  • 12
0
votes
1 answer

Remove repeated elements and give a Range Sum

I have a question regarding already asked this question: SPOJ DQUERY : TLE Even With BIT? What if I would like to not consider the repeated element to count when I make a range query? following is an example: Input Line 1: n (1 ≤ n ≤ 10^6). Line 2:…
user9267359
0
votes
1 answer

How to access an array in all functions in my C program?

I am trying to create a segment tree for a competitive coding problem and this tree is represented using an array. I have functions namely, rangeMinQuery and updateTree which perform intermediate jobs on the array. I am unable to figure out how to…
mr_mohapatra
  • 17
  • 1
  • 9
0
votes
1 answer

Wrong Answer - Unable To Find Bug - Range Minimum Query Using Segment Trees

I am trying to learn segment tree through https://www.topcoder.com/community/data-science/data-science-tutorials/range-minimum-query-and-lowest-common-ancestor/ After Understanding the basics of segment trees I tried to solve this question. But…
Brij Raj Kishore
  • 1,595
  • 1
  • 11
  • 24
0
votes
1 answer

range update on array

You have n length array having all elements 0 initially. You have to execute 2-types of m commands. type 1: l r (l ≤ l ≤ r ≤ n) — Increase all elements of the array by one, whose indices belong to the range [l, r]. type 2: l r…
0
votes
2 answers

Why segment tree needs to be a full binary tree?

When constructing a segment tree, why it needs to be a full binary tree? I took some example input arrays and when made them to complete binary tree i am getting the same minimum in a range result. Then why make it a full binary tree when complete…
Braj
  • 588
  • 3
  • 15