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

Iterative segment tree implmentation

I'm referring to this post : https://www.geeksforgeeks.org/segment-tree-efficient-implementation/ Pasting the code here for reference. My questions are below the code. #include using namespace std; // limit for array size const…
Raaj
  • 1,180
  • 4
  • 18
  • 36
-1
votes
1 answer

exception thrown while passing parameters

I am getting error Exception thrown at 0x009523B9 in Project5.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00E42ED4). in the below program test case: 5 1 2 3 4 5 1 Q 2 4 I was getting error when executing this line struct node…
akacoders
  • 29
  • 3
-1
votes
1 answer

Seg Fault due to malloc at the end of C++ program

The program uses Segment Trees to find the sum of given range query. It gives the correct answers to the input enterred. However, at the end of the program after executing all lines from the main function, it displays a segmentation fault. Link to…
-1
votes
1 answer

Divide "people" into two equal-size teams to reach max total efficiency

I'm trying to solve this task: The kingdome recruited n people. Recruit has two characteristics: ability power and strength. Recruits must be devided into two equal-size squads: warriors and wisards. Found separation with max efficiency level. …
bordus
  • 35
  • 7
-1
votes
2 answers

Explanation about querying a segment tree

I am learning segment trees (on the example of range minimum query) and have some problem understanding the reason behind the part of the algorithm for querying a segment tree. I have no problem understanding the part of construction the segment…
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
-1
votes
1 answer

Creating binary indexed tree

I read various tutorials on BIT.. topcoder etc ones, all operations are well explained in those, but m not getting the way BIT is created i.e. Given an array, 1-D, how e have to kake the corresponding BIT for that? ex. if the array is 10 8 5 9 1…
user3004790
  • 748
  • 7
  • 10
-2
votes
2 answers

(C++) How to modify/use data structures such that we can use them again and again?

I have a data structure (Circular Doubly Linked List) of N integers. I have to modify it by minimising some quantity. In some cases of arrangements of numbers, I will have to check whether the quantity is minimised by checking the modified data…
Sarthak Rout
  • 137
  • 2
  • 7
-2
votes
1 answer

Why are the values stored in memory locations changing?

I am trying to implement segment tree. In the following way: #include using namespace std; int size; int construct(int *arr,int *s,int curr,int end,int ad) { if(arr[curr]==arr[end]) { s[ad]=arr[curr]; return…
-2
votes
4 answers

How to free all the memory in a tree of pointers

I'm coding a persistent segment tree for the following problem on Codechef: https://www.codechef.com/problems/GIVEAWAY, and my struct for the node looks like this: struct node { int val; node *left, *right; node (int val, node *left,…
SinByCos
  • 613
  • 1
  • 10
  • 22
-2
votes
1 answer

Optimize sum(i,j) and update(i,value) for an arryas of integers

Given a huge array of integers, optimize the functions sum(i,j) and update(i,value), so that both the functions take less than O(n). Update Its an interview question. I have tried O(n) sum(i,j) and O(1) update(i, value). Other solution is preprocess…
Arun Chaudhary
  • 277
  • 3
  • 13
-2
votes
2 answers

Iterative code for segment tree

I wanted to code segment tree in c++ and wrote code query operation through recursion but it's slow and giving TLE. So, can Someone suggest and explain to code the following through iteration ,will be a great help.Thanks in advance. following code…
-3
votes
1 answer

What is the most optimal way of solving this problem?

I saw this coding question on an online coding competition but I couldn't find the most optimal solution. Here is the question: "You are given an array A of N integers and Q queries.Each query is of the following type : 1 pos val: Update the element…
-3
votes
1 answer

bad memory alloc error - segment tree

I am trying to create a segmented tree, Here is my struct for the node of tree: struct Node{ int x1, x2; // x coordinates int y1, y2; // y coordinates Node * v1; Node * v2; Node * v3; Node * v4; bool oBo; //check if 1 by…
-3
votes
1 answer

segment tree node structure not clear

I am trying to build a segment tree but the node structure is not clear can someone please explain the code i found struct node{ int count; node *left, *right; node(int count, node *left, node *right): count(count), left(left), right(right)…
coder
  • 41
  • 1
  • 8
-4
votes
2 answers

How to find first 2 minimum integers in array using segment tree

How to find the first two minimum elements to asking a query and updating it. In my opinion, I must use segmentry (segment-tree)
J. Doe
  • 1
  • 4
1 2 3
18
19