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

Finding bug in segment tree implementation

I was trying to solve this problem - link. Segment Tree with Lazy Propagation. But I dont know where I made mistake. Please help me find the bug. I am new to Segment Tree with Lazy Propagation. But my code seems ok. #include using…
1
vote
1 answer

Why a second depth first search?

Recently I came accross a problem called Gravity Tree I couldn't solve it on my own so I checked the editorial. The authors solution was to dfs over the vertices once and form a segment tree.where each node contains the distance from the vertice to…
1
vote
1 answer

Haskell: Frequent values

I am trying to solve frequent values using Segment Tree This blog article uses a similar approach I want to split a list into intervals as: -1 -1 1 1 1 1 3 10 10 10 becomes (0, 2) (2, 6) (6, 7), (7, 10) I have a code as: g s = map (\x->(head x,…
Zubin Kadva
  • 593
  • 1
  • 4
  • 29
1
vote
1 answer

For a given array, count the combinations from three elements in decreasing order

I'm solving one problem from past competitions in programming, so i need help, i will explain the problem in one sentence. You have an array with size of N, where N can go up to 10^5. And then in second line you have exactly N elements. So now you…
someone12321
  • 755
  • 5
  • 24
1
vote
0 answers

SegmentTree: what are the advantages of using [l, r) (open interval on the right) in the implementation?

I have seen some SegmentTree implementation uses [l, r) to represent an interval, while some others use [l, r]. So my question: why we would want to use [left, right)? Seems it adds some complexity in the code to understand if there are no obvious…
Huang C.
  • 398
  • 6
  • 17
1
vote
2 answers

check if string of parenthesis is balanced by doing certain operation on string

Given string of parenthesis we have to do 2 kinds of operation: flip- changes the i-th parenthesis into the opposite one(left->right , right->left) check- if the string is a balanced parenthesis expression length of the string is at max 30000.…
1
vote
0 answers

Spoj brackets segment tree

can anyone help me in my code. I am getting WA and m not able to rectify it plzzz my code http://ideone.com/DkrwIg problem link : http://www.spoj.com/problems/BRCKTS/ i am a bit doubtful in my modification…
Akshit Verma
  • 77
  • 2
  • 8
1
vote
2 answers

Codeforces Round 671 Div 1 C (ultimate wierdness of array)

Codeforces 671 Div 1 C (ultimate wierdness of array) Let vi be b1, b2, b3...bk. Note that our l - r must be cover at least k - 1 of this indexes. l must less or equal to b2. I was able to understand the first part of the solution, but can someone…
1
vote
1 answer

Array size in segment tree Data structure

In segment tree, we build segment tree above an array. For Example, If array size is 8 [0-7] indexing. Number of nodes in segment tree is 15 i.e., 1,2,4,8 in 1st,2nd,3rd,4th levls But in a problem, if I declare structure array size as seg tree[2*N…
Sparrow
  • 65
  • 2
  • 10
1
vote
1 answer

Querying in segment tree

I have been struggling to understand the logic of last 6 lines in query() function. This is the code for the problem GSS1 on spoj. Solution link #include #include #define MAX 70000 using namespace std; struct no { int lsum,…
Sparrow
  • 65
  • 2
  • 10
1
vote
1 answer

segment tree building method

Hi i am learning segment tree. Buildilding the segment tree using the recursive methodis clear to me , i have implemented it like this: void build( int n, int b, int e){ if(b > e) return; else if (b == e) { tree[n] = arr[b]; return } …
kshitij singh
  • 363
  • 2
  • 10
  • 19
1
vote
1 answer

Update the array

Given an array arr which is initialized to 0 i.e. arr[i]=0 where 0 < i < n two operation are performed on it update k r x Update does the following: for(i=k;i<=r;i++) { arr[i]=x; x+=x; } query k r Query computes the following…
nil96
  • 313
  • 1
  • 3
  • 12
1
vote
1 answer

Segment tree with easy shifting

I need Data Structure based on Segment Tree, but with one difference to clasical segment tree. DS should support easy shifting of elements. I mean I would like to have ds on which I could: make queries on segments (i.e. sum of elements from index…
Michocio
  • 503
  • 3
  • 19
1
vote
1 answer

Memory Limit Exceeded with Segment Tree in SPOJ Posters?

Given a horizontal section of wall , and N layers of paints applied from co-ordinates Xi to Yi , Output the distinct number of layers visible. Here is the problem link http://www.spoj.com/problems/POSTERS/ Here is my solution…
1
vote
0 answers

segment tree to update an interval with different value

I have to use segment tree(may be other data structure if possible) to calculate the minimum over an interval, more specifically, over [1,i] where 1 is the first element of the array.Now, I have some queries to update an interval (i.e. from…