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

Is there any way to find frequency of a number in a given range using segment tree?

Suppose, I've an array [1, 2, 3, 5, 5, 6, 5, 5] Now, there can be two kind of operation. One of them is "update" operation , that is increase the value in index 4[1 based indexing] by X. Other operation is "query" operation, where given a range,…
Anik Roy
  • 15
  • 5
0
votes
0 answers

DP with Segment Tree and Lazy Propagation

I read about a very simple problem, and tried to complex it further and further and included other concepts along with that. But I got stuck at one point and cannot resolve it further now. Problem 1: You are given an array, Now there are Q queries…
0
votes
1 answer

Why getting tle in this Range Minimum Query code?

I was trying to solve a very basic problem that just involved the implementation of Range Minimum Query.The link for the problem is https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/tutorial/ But I am…
Gaurav Jha
  • 161
  • 1
  • 7
0
votes
1 answer

How to answer queries on a path on a tree?

Let's say I have an array and I have to answer queries like find the sum of all elements from index i to j, now can I do this on a rooted tree, like answering such queries for path from node i to j ( On the only path that exists from i to j). I know…
anker
  • 63
  • 5
0
votes
1 answer

String query with binary indexed tree

I want to range query a string using Fenwick tree. But something is going wrong with my code. Concatenation is giving error Eror is:[Error] no match for 'operator+=' (operand types are 'std::vector >' and 'std::string {aka std::basic_string}') …
nRT
  • 13
  • 4
0
votes
1 answer

Building a segment tree in c++

I am trying to build a segment tree in c++. Following the recursive function for the same: int buildTree(int node,int start,int end,int tree[]) { // printf("Node is: %d\n",node); printf("start: %d\tend:%d\tnode:%d\t\n",start,end,node); …
learner
  • 4,614
  • 7
  • 54
  • 98
0
votes
1 answer

What are persistent segment trees and how do I use them?

I have come across a lot of problems which could be solve using persistent segment trees but I can't seem to understand this concept. I have found some resources but I still can't understand it.
anker
  • 63
  • 5
0
votes
1 answer

K-ordered blocks range query

Given a list of N numbers(1-indexed), a continuous block is K-ordered block if it has more than K same elements occurring consecutively. Example : [2,4,4,5,5,5,3,3] is having a 3-ordered block from index 4 to 6 and a 2-ordered block from 7 to 8.…
HackAround
  • 85
  • 2
  • 9
0
votes
1 answer

Updating query a[i] = max(a[i], x) and Minimum query for intervals

I want to know the fast algorithm to solve this simple problem: You are given a sequence a[0], a[1],..., a[N - 1]. You are given Q queries. All queries are query 1 or 2. Process all queries. You are given integers l, r, x. Update a[i] = max(a[i],…
square1001
  • 1,402
  • 11
  • 26
0
votes
1 answer

Finding a range satisfying a property in faster than linear time

Given an array A[] we need to find a range which has the maximum size and its minimum element is >= 1. We also need to update this range by decreasing all its elements by 1. One idea I got is to keep a segment tree for efficient updates. But how do…
Ivankovich
  • 119
  • 2
  • 8
0
votes
1 answer

C++ : Runtime error in Segment trees

I am trying to implement Segment tree for modify and range product query(product of all elements in range). Following is my code : using namespace std; const int N = 1e4; long long int n; // array size int t[2 * N]; void build() { // build the…
Buckster
  • 41
  • 12
0
votes
1 answer

Find number of items with weight k in a range (with updates and queries)

I am trying to solve the following problem: Given an array of items with integer weights (arbitrary order), we can have 2 possible operations: Query: Output the number of items that are of weight k, in the range x to y. Update: Change the…
Donald
  • 1,300
  • 2
  • 13
  • 29
0
votes
1 answer

Bug in RMQ Segment Tree

I am trying to build a Segment Tree for performing RMQ. Somehow, no matter what range I query it will return me 0. For example, my array is [ 1,2,3,4,5,6,7,8,9,10 ]. RMQ from index 3 to 5 should give 4. But my code keeps outputting 0. My…
Donald
  • 1,300
  • 2
  • 13
  • 29
0
votes
0 answers

C++: (LIS)Longest Increasing Subsequence using Segment Tree

Given an array of N elements, I need to find length of longest increasing subsequence for T different values of L and R. I tried segment trees, I'm getting Time Limit Exceeded. using namespace std; int a[1000001],ans[1000001],out; int…
Buckster
  • 41
  • 12
0
votes
1 answer

Segmentation fault after main exit

I Have tried executing the following code in gdb, but with gdb I don`t see any segmentation fault but without gdb If I run the following code in standalone mode segmentation fault occurs. The code is related to range sum query implemented using…
Bad Sector
  • 37
  • 5