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
3
votes
3 answers

SPOJ "Card Trick": unable to understand how to apply binary index tree

Card Trick is a problem on Sphere online judge. It states that The magician shuffles a small pack of cards, holds it face down and performs the following procedure: The top card is moved to the bottom of the pack. The new top card is dealt face up…
3
votes
1 answer

Number of occurrences of a number in a particular range?

Given a large unsorted array, I need to find out the number of occurrences of a given number in a particular range. (There can be many queries) e.g. if arr[]={ 6,7,8,3,4,1,2,4,6,7,8,9} and left_range=3 and right_range=7 and number=4, then the output…
user3080029
  • 553
  • 1
  • 8
  • 19
3
votes
2 answers

(ACM) How to use segment tree to count how many elements in [a,b] is smaller than a given constant?

I am quite new to segment tree and would like to make myself busy by doing some more exercise on segment tree. The problem's actually more ACM like and have following conditions: There are n numbers and m operations, n,m<=10,000, each operation can…
shole
  • 4,046
  • 2
  • 29
  • 69
3
votes
1 answer

Efficient mapping of ranges to groups of values

I'm attempting to determine a suitable way to accomplish the following. I would like to have range -> set lookup within a particular range (say [0x0 - 0xffffffff]). Values are inserted into the range at ranges (so if we are working with T = unique…
3
votes
3 answers

Lazy propagation in segment tree?

Well, I was trying to solve this Flipping coins problem on Codechef. Am solving it with segment trees. But getting time limit exceed. I searched and found that I have to use lazy propagation. But I am unable to understand. My update function works…
dejavu
  • 3,236
  • 7
  • 35
  • 60
2
votes
1 answer

Maximum Sum Elements in Range

Given an array 'A' of size 'N' containing integers. You need to answer 'Q' queries of type [ L R X Y ]. In each of the query you need to select at least 'X' elements and at most 'Y' elements from range 'L' to 'R' of the array 'A' such that their…
raxona3dd
  • 23
  • 4
2
votes
2 answers

How can I determine quickly how many points of given set does a sphere contains?

Given a set of points in a form of (x, y, z), and a set of spheres in a form of (x, y, z, radius). The goal is, for each sphere, to count the number of points within the sphere. I compared the Euclidean distance of the center of the sphere and the…
장지훈
  • 21
  • 1
2
votes
1 answer

2D segment tree query time complexity

These sources cp-algorithms and geeksforgeeks state that query complexity (for example, submatrix sum) of 2-D segment tree is O(logN * logM), because it first descends the tree in the first coordinate, and for each traversed vertex of that tree, it…
2
votes
2 answers

XOR queries on a given array

Given an array of n integers, indexed from 1->n. The task is to perform of Q given queries, and print the sum of the array after each queries. We can perform three types of operations: 1 X: add X to the array (its index will be n+1, n+2,...) 2 Y:…
unglinh279
  • 675
  • 4
  • 24
2
votes
1 answer

How to get index of segment tree query?

I have been trying to get an index of sum range query for the array. Suppose that I have an array like: {1, 3, 5, 7, 9, 11} With a segment tree like that {36, 9, 27, 4, 5, 16, 11, 1, 3, DUMMY, DUMMY, 7, 9, DUMMY, DUMMY} Here is an image of it. How…
Raguel
  • 595
  • 8
  • 25
2
votes
1 answer

Number of subsets whose XOR contains less than two set bits

I have an Array A(size <= 10^5) of numbers(<= 10^8), and I need to answer some queries(50000), for L, R, how many subsets for elements in the range [L, R], the XOR of the subset is a number that has 0 or 1 bit set(power of 2). Also, point…
2
votes
1 answer

How to solve offline range-mex query with updates?

ATTENTION! I have read all same questions on stack and some other sites. All of them are wrong or working more than TL. Now about the task: Size of input array: N <= 5*10^5 and 0 <= array[i] <= N (The size of the array is less or equal to 5*10^5,…
mylibh
  • 153
  • 9
2
votes
2 answers

Segment tree min and max

I am trying to build a segment tree whose parent node should contain min and max values of its children nodes.Now when I am trying to implement this I am facing an error which is that one child can return an integer whereas other child can return a…
Om Sharma
  • 335
  • 1
  • 11
2
votes
1 answer

Can this be properly modeled with segment trees?

The problem I'm working on requires processing several queries on an array (the size of the array is less than 10k, the largest element is certainly less than 10^9). A query consists of two integers, and one must find the total count of subarrays…
user43389
  • 721
  • 6
  • 18
2
votes
1 answer

How to effectively answer range queries in an array of integers?

How to effectively and range queries in an array of integers? Queries are of one type only, which is, given a range [a,b], find the sum of elements that are less than x (here x is a part of each query, say of the form a b x). Initially, I tried to…
user3243499
  • 2,953
  • 6
  • 33
  • 75