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

How can I solve the problem on the segment tree

I have an array a[0..n - 1]. I have 2 types operations: sum l r: sum = a[l] * 1 + a[l+1] * 2 + ... + a[r] * (r - l) update index value: a[index] = value How can I modify standart sum segment tree for this task?
Alex
  • 23
  • 2
0
votes
1 answer

CSES range query section question salary queries problem

I am trying to solve cses salary queries (https://cses.fi/problemset/task/1144/) Question: I will make a frequency array of salaries and I will use coordinate compression but while update I have to rebuild coordinate compression and there will be a…
0
votes
0 answers

Segment Tree - Finding all subarray sums

Suppose we have an array that's like [1, 2, 3, 4], if I created a segment tree for that array we'd get something like: [null, 10, 3, 7, 1, 2, 3, 4], so all of the subarray sums would exactly what we have on the segment tree. However, if our input…
amai
  • 93
  • 1
  • 10
0
votes
1 answer

Multiplication queries in a tree

We are given a tree of N nodes (1-N) with each node having an initial value A[i]. Tree is rooted at node 1. We are given the Q queries of type : 1 V X : multiply all nodes in subtree of `X` with value `V`. 2 X : find the value of node `X`. …
0
votes
1 answer

(end-start+1)*Lazy[treeIndex]?

Please help me understand this statement in lazy propagation for segment trees. I would have expected incrementing by merely Lazy[treeIndex]: tree[treeIndex] += Lazy[treeIndex] Context: void RangeUpdate(int node, int st, int end, int l, int r, long…
0
votes
1 answer

How to determine the indexes of the leaves in a given subtree in a complete binary tree?

I have a complete binary tree in array notation (breadth first): [15, 10, 5, 3, 7, 5, 0, 1, 2, 3, 4, 5, 0, 0, 0] So the indexes of all the leaves are: 7, 8, 9, 10, 11, 12, 13, 14. For each internal node I need to return the indexes of leaves in…
Lisbeth
  • 141
  • 1
  • 6
0
votes
4 answers

How to get the minimum XOR of a given value and the value from a query of range for a given array

Given an array A of n integers and given queries in the form of range [l , r] and a value x, find the minimum of A[i] XOR x where l <= i <= r and x will be different for different queries. I tried solving this problem using segment trees but I am…
sam
  • 13
  • 4
0
votes
1 answer

Find the first index x that the value a[x]>u in range [l,r]

Given an array a[] has n non-negative elements. We have 2 types of queries: A x y v: Find the first index i (x<=i<=y) that a[i]>v B u v: Update a[u]=v; I use segment tree but it's TLE in some test. This is my code. it max(it A,it B) { it C; …
aka61bt
  • 57
  • 4
0
votes
1 answer

Insertion and Deletion on segment tree or BIT tree?

I am having a hard time in solving a variant of Leetcode 307 Range Sum Query - Mutable and Leetcode 528: Random Pick with Index such that to design a data structure so that I can add elements and each element will has a weight and pop elements based…
0
votes
0 answers

Is there an efficient algorithm for the following interval problem?

I have a list of sorted intervals and another list, which describes the index of every interval. Here's an example: intervals = [(0, 2), (2, 3), (4, 6)] // (0, 2) includes 0, but doesn't include 2 values = [2, 3, 7] Now, I want to know, how often…
0
votes
0 answers

what is the use of binary-or if using same values? (segment tree)

Can't understand the requirement of t[i<<1|1] in build() function of seg tree. isn't it equivalent to t[i<<1] ? const int N = 1e5; // limit for array size int n; // array size int t[2 * N]; void build() { // build the tree for (int i = n - 1;…
0
votes
1 answer

Why is my segment tree unable to run in time?

I'm attempting the following problem, and after multiple submissions over many hours, I am still unable to get my solution to run in time The problem https://codeforces.com/problemset/problem/339/D My solution def getInts(): return [int(s) for s…
0
votes
2 answers

Update Value at given index and also find the Kth smallest element at the same query

An array is given (indexing starts with 1) of size S and N number of queries is given by user N(i)= (M P R); 1<=i<=N. Print the Rth minimum element from the array after updating Mth index. Example: – Array: [2, 4, 6, 1, 7], S=5 Queries: N=3 2 5 3 5…
0
votes
2 answers

Static array returned by a function is being overwritten because of recursion

I am coding a segment tree algorithm in which the nodes of the tree are arrays (it's supposed to represent the frequency of indices, it doesn't actually matter for my problem). Since I need to return an array when querying the tree, I figured I had…
xedc
  • 91
  • 2
  • 9
0
votes
0 answers

Calculate the number of nodes on either side of an edge in a tree for Given queries

A tree is given with N nodes and N-1 edges, and the edges are deleted in some order. After each deletion, you need to give the number of nodes on present in each of the two newly created nodes. Note: the root is unknown; N<=300000; For Example: A…