0% found this document useful (0 votes)
15 views40 pages

Segment Trees - TLE 4

Uploaded by

iitjee.prime
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views40 pages

Segment Trees - TLE 4

Uploaded by

iitjee.prime
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

① Lazy Propogation

⑰ Basics
-
-

② Generic sagment Trees

Range Queries >1800


-

Segment Trees
-

!
Range Queries

● Example problem: Given an array A of N (N<=1e5) elements and Q (Q<=1e5)
queries, In each query you will be given two indices L and R, you need to output
the sum of values of the array A from index L to R.

(358] 2

● Example:
N=4, Q=1, A[]={3, 5, 2, 8}
130 10 10]
Query 1: 2 3
-
⑤ ⑤
=>
>
-

Output: 7
prefix(r) 10,1,2,3, --- r]
-
-

prefin (0-1) (0,1,2,3 --,d- 1]


② Given a is
away containing everywhere
headle the
following queries (a queries)

#.
01 01 0
(Neuments)
15125101010

I


-
After
processing all the

1, 4, 5
2,3,7 the final
queries give
-
-

array
Different
-

- -
Array
1
l01
01 01

2
-
M101-101
-> o

1 ,
-

Add a to 10101-
Add -n to R+1
16
0 10101 0

-
·

101010 0 (n)
(2,4,2) 141-1010 0(1)
23.7, 0s Ml13101010 0(a)
15(401-1310/0 0(1)
-5,6,4
millis/21270 ocul

<01-1181-2 oly
-
13713/2/210

es
then once
at and

the final array is


required

I is
O(1 for every update

o(n) for final


array
as
Arrange
Array

andatanene
+

Differenc Aray
0(a) + 0(n) 0(a) +
OCN)
Range Queries

● This can be solved using a prefix sum array. But what if we have an update
operation in the problem?

E
● New statement: Given an array A of N (N<=1e5) elements and Q (Q<=1e5)
queries, In each query, you have to do one of two types of operations.
Operation 1: You will be given two indices L and R, you need to output the
sum of values of the array A from index L to R.
Operation 2: You will be given index Pos and a value Val, you have to change
the value at index Pos to the value Val. That is set A[Pos]=Val.
Range Queries
5
218
● Example:
N=4, Q=3, A[]={3,5,2,8}
Query 1:O
123
15,210
-

Output: 7 -
Query 2:0
2 2 10
8 URUUURRRUURR
Query 3:⑧
123

Output: 12 -

How to do this?
I
PointUpdate
Range Quesies

Brutt ·(1)
0
(n)
Forc
-

Prefin 0(1 o(n)


Sum

0 (loyn) 0 (logn)
-
Segment Trel
Let’s Learn Segment Trees!

● A segment tree can be used to solve this kind of problems!

● Segment Tree: Segment Tree is basically a binary tree used for storing
intervals or segments. Each node in the Segment Tree represents an interval.

Leaf Nodes are the elements of the input array. Each internal node represents
some merging of the leaf nodes. For this problem merging will mean addition.
·
1T12
TM121s 8
X
·
1 12 1311 2131112 4,4
-
·
112311213 1/2 2,2,2,2
-

2 3 1 2 3 I 2
1

Update the value at inden=> 2


-
to
-
10
-
-
leaf
Ican
node in
reach the
olloy it
#
/s/T1230
112(311213(1/2 o(az) +02)
-

=o(v)
112311213 112
o(n)
1 2
123 I 23
o(u)
-

Space right now = 0


(height)
· of-pay at away Grell
0 (H.N)
0
=>

(nlogu)
-
-
⑮( o(n)
-⑲ -


1 1
③ B ⑭ ⑤
X -11
②&
① 0 B

#
(113/213)

-40)
⑯ ⑰ (5,0)
(1,4)
- -
③ ④ O
-* ** **
①& & D & D
21,13 [2,2) (3,3) (4,4) (5,5)(0,0) ki) (8,0)
U(2) 10
=

- 40)
- -

-
0
14
0 (5,0)
7
⑯ (1,4)
-
-
-
0 11
③ D O
T - *
⑩& D & D
21,13 [2,2) (3,3) (4,4) (5,5)(0,0) ki) (8,0)
find the value
by going
update values
down, the

by coming up
[1,719

B ·
Give me

from
the

(1,0) (1,
(1,0) ---
sum

- ⑳
0 0
-

B③⑦
L
-

6 L R

⑰5,
7
(1,4)
>
->

- -
-

- ⑧

⑬ ③ ④ O
-
①& D D & D
21,13 [2,2) (3,3) (4,4) (5,5)(0,0) ki) (8,0)
Current node contributes return
->

entirely node valu

Lorrent node contributs -> beturn

nothing ②

Current not contribut seturn


-

partially L R
-
-
+
If are at a node and
you
need answer
the of its
you

complete rage
-> directly return

value
that
If at nod and
you are a

new of its contribute


elements

to your and wit

-> directly return from


this point
lg
2 ③ none 3 complete
overlap
3 S v
X
>


Disjon't
<-]

*
[ -) Partial
[ I overlap
!oPartial
8.
partial overlap
-
·
a partial ⑧

overlap Overlap

Nant do ⑧
R2
PO

Internal 2
I D -
I R
-

Torminal
T -
R 4 -

D
● Building the segment tree:

An array representation of tree is used to represent Segment Trees. For each node at
index i, the left child is at index 2*i, right child at 2*i+1.

As all the nodes have 2 children, the segment tree will be a full binary tree with N leaf
nodes and N-1 internal Nodes. So the total number of nodes in the segment tree will be
2*N-1.

But if N is not a power of two, there will be some unused nodes. In that case, the
approximate size of the segment tree is 4*N. So the segment tree array will be an array of
size 4*N.

We start with the segment A[0 . . . n-1]. And every time we divide the current segment into
two halves(if it has not yet become a segment of length 1), and then call the same
procedure on both halves, and for each such segment, we store the sum in the
corresponding node.
IQuadertt,
-> right e

Pointers

Y
Indic

2 Y

④ ⑧ G

1/11234
* uytatoin
min
s
I
=

n'2
a
ty y,o
⑧ yO 0 0 U

-
n M2 uly--. -
+
+
indem N

left
=>

= 2n

right =
In+1
Segment Tree
● Code:

● Time Complexity: O(N) (We visit all the 2*N-1 nodes once)
Segment Tree

● Querying:
To make a query on a segment tree on the range [L,R], we recurse on the tree
starting from the root and check if the interval represented by the node is
completely in the range from L to R.

If the interval represented by a node is completely in the range from L to R, return


that nodes value.
Segment Tree

● Code:

● Time Complexity: O(log(N))C


(At each level, we will visit at most 4 nodes)
Segment Tree

● Updating in a segment tree:

We can update the value of an index of the array using a segment tree. Like the
segment tree construction and query operations, the update can also be done
recursively.
Each level of a Segment Tree forms a partition of the array. Therefore an
element A[Pos] only contributes to one segment from each level. Thus
only O(log(N)) vertices need to be updated.
Segment Tree

● Updating in a segment tree:

We start the update function from the root. We recursively call the update function
with one of the two child vertices (the one that contains A[Pos] in its segment).

When we get to the leaf node, we update A[Pos] with Val and return. And after that
the vertices in the path to this leaf node recomputes their corresponding sum value,
similar to how it is done in the build function (that is as the sum of its two children).
Segment Tree

● Code:

● Time Complexity: O(log(N))


Segment Tree

● Other Queries:

Some other types of range queries can be solved using a segment tree as well.
For example, range minimum/maximum queries, range GCD queries etc.

1. Range minimum/maximum: For querying range minimum/maximum, we will


store the minimum/maximum of its two children in the corresponding node. Similarly
we will take minimum/maximum of its two children when updating and querying.
Segment Tree

● Other Queries:

2. Range GCD: For querying range GCD, we will store the GCD of its two children in the
corresponding node. Similarly we will take GCD of its two children when updating and
querying.

3. Index of minimum/maximum element query: For this we can store a pair of values for
each node, which will correspond to the minimum/maximum in that segment and the
index of that value, when merging two nodes, the pair for the current node will be the
pair among its two children which has the minimum/maximum value.
Some More Problems
● Problem 1:
Given an array A of N (N<=2e5) elements and a list of positions, your task is to
remove elements from the list at given positions and report the removed elements.
Example:
N=5, A[]={2, 6, 1 ,4 ,2}, list[]={3, 1, 3, 1, 1}
Output: 1 2 2 6 4

Any ideas?
Some More Problems
● Problem 2:
Second Maximum in a Range Queries

Any ideas?

You might also like