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

Modify lazy propogation in segment tree

I recently read about lazy propogation in segment tree and coded it too.But i got stuck when suppose instead of adding value(=val) i need to divide by value.How to do it ? Please help My update function is as follow : void update_tree(int node, int…
0
votes
1 answer

Segment Tree Codechef TLE

I am trying to solve this CodeChef problem: There are N coins kept on the table, numbered from 0 to N - 1. Initially, each coin is kept tails up. You have to perform two types of operations: Flip all coins numbered between A and B inclusive.…
Abhiroop Sarkar
  • 2,251
  • 1
  • 26
  • 45
0
votes
1 answer

Difficulty in Implementation in Segment tree with lazy propogation

I am having trouble in implementing segment tree with lazy propagation. I just read about segment trees and tried to do a simple question (http://www.codechef.com/problems/FLIPCOIN) using it but I am getting wrong answer. Please help me with the…
user2179293
  • 425
  • 9
  • 22
0
votes
0 answers

Large values in segment tree for product range queries

I wrote code for firing product range queries on an array. Note: This question not a duplicate to Multiplication in a range. My problem is something different I wrote the code for this, // Program to show segment tree operations like construction,…
user650521
  • 583
  • 1
  • 9
  • 23
0
votes
1 answer

Generic Segment Tree implementation using C++ Templates

I am trying to make a generic Segment Tree Class for updates and range queries. Instead of assuming that the elements would just be integers and the operation to be done over a range of elements would be their sum or product, i would want the user…
0
votes
3 answers

Optimization or New Algorithm to solve this?

I am trying to solve this problem : Little girl has an array of n elements (the elements of the array are indexed starting from 1). Also, there are "q" queries, each one is defined by a pair of integers li, ri (1 ≤ li ≤ ri ≤ n). You need to find for…
user2485865
0
votes
1 answer

Issue in implementation of Segment Trees

I am trying to implement Segment Trees for extracting min value from a given interval in an array.[Reference - http://www.geeksforgeeks.org/segment-tree-set-1-range-minimum-query/ ] The following is the code for the same. //This code inplements…
Shobhit
  • 773
  • 7
  • 20
0
votes
1 answer

Simple code in python causing unexpected behaviour

I am trying to implement a segment tree class in python. Segment trees allow queries on ranges in logarithmic time (more about segment trees). In my implementation I hold the necessary information to be able to answer the sum of the elements in a…
0
votes
1 answer

Clear range (set range to zero) lazy segment tree modification

I'd like to add a function to the lazy propagation implementation in the link below that sets a range to 0. There is currently an update_tree function in there which increments a range, but I don't know how to modify it so that it would set a range…
0
votes
1 answer

Segment Tree, Lazy Propagation

I have a good idea on how this structure works and how to update it, however when it comes to work with Lazy Propagation I don't know what to do, as many many many problems requires this to pass in competitions I want to know how to make it work. I…
0
votes
0 answers

Performance issues : segment tree, update function

I update segment tree with such function. Profiling says here's the bottleneck: void update (int tree[], int root, int left, int right, int pos, double val) { if (left == right) { data[tree[root]] = val; } else { …
Ben Usman
  • 7,969
  • 6
  • 46
  • 66
0
votes
2 answers

Segment tree : Lazy propagation

In an integer array (size 10^5) the operations are like these... Do bitwise xor operation with every element from index L to R by a particular number X Find the sum of the numbers from index L to R. How can i do it with segment tree and lazy…
palatok
  • 1,022
  • 5
  • 20
  • 30
0
votes
2 answers

SPOJ GSS1 WA - Segment tree

I am trying to solve SPOJ problem GSS1 (Can you answer these queries I) using segment tree. I am using 'init' method to initialise a tree and 'query' method to get maximum in a range [i,j]. Limits |A[i]| <= 15707 and 1<=N (Number of…
Marquee777
  • 53
  • 1
  • 7
-1
votes
1 answer

Segment tree built on "light bulbs"

I have encountered following problem: There are n numbers (0 or 1) and there are 2 operations. You can swich all numbers to 0 or 1 on a specific range(note that switching 001 to 0 is 000, not 110) and you can also ask about how many elements are…
dispenomi
  • 27
  • 5
-1
votes
1 answer

Why/When do we use segment trees for range minimum query?

I read that for implementation of range minimum queries using segment trees, it takes O(n) time for preprocessing and O(log(n)) time to find the solution for each query. It requires extra space too. If instead we simply, find the minimum for a…