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
1
vote
1 answer

UVA - 1394 : And There Was One Algorithm

The link to the question is UVA - 1394 : And There Was One. The naive algorithm is to scan the entire array and marking the kth element on each iteration stopping at the last : this takes O(n^2) time. I have searched for an alternative algorithm and…
jemmanuel
  • 466
  • 4
  • 13
1
vote
2 answers

storage time in segment tree

I want to know how many nodes are there in a segment tree made for solving range minimum query problem. Also, how much time does the build operation take and why?
vishalgoel
  • 69
  • 4
1
vote
0 answers

Segment tree Lazy propagation & my code

I am currently solving a problem on segment tree. I think the problem needs lazy propagation concept to be solved. As I'm very new to this concept, i'm having trouble with my code. The problem in a nutshell is as follows: initially, all array…
0
votes
1 answer

What is the space complexity of a segment tree?

Some references as in link1 say there are 2n+1 nodes. Whereas some say there are roughly 4n nodes link2. To me 2n+1 seems intuitive as for a perfect binary tree with n leaves has 2n+1 nodes and when the tree is not perfect it must be less than 2n+1…
0
votes
0 answers

Design a set data structure(using tree implementation) that can "screen out" smaller pairs of integers

Given a series of pairs of integers (A_1, B_1), (A_2, B_2), ..., (A_N, B_N) and an initially empty set S, add these pairs to a set S one at a time, and for each pair (A_i, B_i), do the following two operations: 1. Check if there exists any pair…
Mike Chen
  • 31
  • 3
0
votes
0 answers

Implement B-balancing of the search tree

Got a task: to implement a dictionary based on a search tree with B-balancing, in which the data is stored in leaves. I wrote a dictionary class: B-balanced segment tree. I understand that a search tree with keys k1, k2, k3 ... kn corresponds to a…
0
votes
1 answer

C++ - How to return index of minimum element in range query?

I am trying to implement range queries on a segment tree (a binary tree). I am trying to modify the query function so that it can return the index of the minimum value over a range instead of the actual minimum value (which it is currently…
Redz
  • 324
  • 1
  • 4
  • 16
0
votes
0 answers

Range query question on fenwick/binary indexed tree

So I have an integer array in which I need to find the sum between range L to R Also I need to add some value x(int) to range L to R i.e update operation. I have solved these questions using fenwick tree range sum/update queries but this problem is…
0
votes
0 answers

Issue With struct

I am running this simple code to be used in segment trees. Shows a lot of errors. Could anyone explain where am I going wrong here? It basically has problems with declaring a vector of nodes. ` // C++ program to show segment tree operations like…
0
votes
0 answers

Ranage based read/write dependency algorithm

There are some sequences like: op_num: [start, end], type 1: [0, 10], Wrtie // op_1 will write the range [0,10] 2: [5, 10], Read // op_2 will read the range [9,10] 3: [6, 7], Read 4: [6, 7], Write Now I am going to write an algorithm to get the…
Ast
  • 1
  • 1
0
votes
1 answer

Why does this Segment tree iterative algorithm only work for perfect binary trees?

I have this algorithm which I modeled off of https://cp-algorithms.com/data_structures/segment_tree.html#structure-of-the-segment-tree. nums = [1,3,-2,8,-7] N = len(nums) tree = [0] * (N* 4) def build_iterative(): for i in range(N): …
Paul
  • 1,101
  • 1
  • 11
  • 20
0
votes
1 answer

Recurrence function to print range of segment

I am trying to implement a simple recurrent function which conceptually similar to segment tree: each node represents a range [l, r] and if current node's index is i, its left and right child will have an index of 2i and 2i+1 respectively. I…
shole
  • 4,046
  • 2
  • 29
  • 69
0
votes
1 answer

Efficient way to know region of a coordinate

everyone, I want to know if there is an efficienct way to know if a coordinate locate in a region. Just like picture below. I want to know each coordinate locate which region below table and get corresponding feature.
ruiyan hou
  • 11
  • 4
0
votes
1 answer

My segment tree update function doesn't work properly

The problem: In this task, you need to write a regular segment tree for the sum. Input The first line contains two integers n and m (1≤n,m≤100000), the size of the array and the number of operations. The next line contains n numbers a_i, the…
Learpcs
  • 282
  • 3
  • 10
0
votes
1 answer

Why segment tree has to be a binary tree only, why can’t it be ternary, or say n-ary tree, where n >2?

In segment trees , we divide the array into 2 halves and carry it on recursively. Why can’t they be divided into,,let’s day , 3 halves, representing 3 segments. Why is it a binary tree?