SlideShare a Scribd company logo
Segment Tree
1
Presented BY:
 Shohanur Rahman
 ID# 1321368042
 CSE 225 (Data Structure)
 North South University
 Email: shohan.nsu.cse@gmail.com
2
Segment Tree
Definition:
In computer science, a segment tree is
a tree data structure for storing intervals,
or segments. It allows querying which of
the stored segments contain a given point.
It can be implemented as a
dynamic structure.
3
Segment Trees
4
Sample Problem:
 You are given n (1<=n<=100000) integers
& 100000 query. Each query you have to
change a value of an element Or you
have to given the minimum value of a
range.
5
Function Of segment Tree
6
Initial Functions:
We start with a segment arr[1 . . . n].
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 corresponding node.
7
Initial Functions:
Initial Functions( in tree look):
9
Initial Functions(Algorithm):
#define mx 100001
int arr[mx];
int tree[mx*3];
void init(int node,int b,int e)
{
if(b==e)
{
tree[node]=arr[b];
return;
}
int Left=node*2;
int Right=node*2+1;
int mid=(b+e)/2;
init(Left,b,mid);
init(Right,mid+1,e);
tree[node]=tree[Left]+tree[Right];
} 10
Query Functions:
11
Query Functions:
 Once the tree is initialized, how to get
the sum using the initialized segment
tree.Now,we use Query function.Here 3
types of cases can be happening.
12
Query Functions:
13
Query Functions:
case A:
if(b>=i&&e<=j);then the segment is :Releavent
segment.
case B:
if (i > e || j < b);means current segment is
outside of i-j,then its valuless.
case C:
if case A and case B are not true and some of
the parts is included in i-j , then we need to
break the segment and take some of the parts.
14
Query Functions(Algorithm):
int query(int node,int b,int e,int i,int j)
{
if (i > e || j < b) return 0;
if (b >= i && e <= j) return tree[node];
int Left=node*2;
int Right=node*2+1;
int mid=(b+e)/2;
int p1=query(Left,b,mid,i,j);
int p2=query(Right,mid+1,e,i,j);
return p1+p2;
}
15
Update Function:
 Like tree initialization and query operations,
update can also be done recursively. We are
given an index which needs to update.
Let new value be the value to be added. We
start from root of the segment tree, and
add new value to all nodes which have given
index in their range. If a node doesn’t have
given index in its range, we don’t make any
changes to that node.
16
Update Function:
17
Update Function(Algorithm):
void update(int node,int b,int e,int i,int newvalue) {
if (i > e || i < b) return;
if (b >= i && e <= i) {
tree[node]=newvalue;
return;
}
int Left=node*2;
int Right=node*2+1;
int mid=(b+e)/2;
update(Left,b,mid,i,newvalue);
update(Right,mid+1,e,i,newvalue);
tree[node]=tree[Left]+tree[Right];
} 18
Time Complexity:
Function Name: Time:
1.Initial Function O(n log n)
2.Query Function O(log n)
3.Update Function O(log n)
19
Applications:
 Using segment trees we get an <O(N),
O(log N)> algorithm. Segment trees are
very powerful, not only because they can be
used for RMQ(Range Minimum Query). They
are a very flexible data structure, can
solve even the dynamic version of RMQ
problem, and have numerous applications in
range searching problems.
20
21
Time to run code……..
22
Thank You !
23

More Related Content

What's hot

Matlab HTI summer training course_Lecture2
Matlab HTI summer training course_Lecture2Matlab HTI summer training course_Lecture2
Matlab HTI summer training course_Lecture2Mohamed Awni
 
Array in c#
Array in c#Array in c#
Array in c#
Prem Kumar Badri
 
C Programming : Pointers and Arrays, Pointers and Strings
C Programming : Pointers and Arrays, Pointers and StringsC Programming : Pointers and Arrays, Pointers and Strings
C Programming : Pointers and Arrays, Pointers and Strings
Selvaraj Seerangan
 
Arrays
ArraysArrays
Arrays
shillpi29
 
Manual for the MATLAB program to solve the 2D truss
Manual for the MATLAB program to solve the 2D trussManual for the MATLAB program to solve the 2D truss
Manual for the MATLAB program to solve the 2D trussMohammaderfan Zandieh
 
Efficient Sparse Coding Algorithms
Efficient Sparse Coding AlgorithmsEfficient Sparse Coding Algorithms
Efficient Sparse Coding Algorithms
Anshu Dipit
 
Echo Function
Echo FunctionEcho Function
Echo Function
IRJET Journal
 
Data structures using C
Data structures using CData structures using C
Data structures using C
Pdr Patnaik
 
Nural network ER.Abhishek k. upadhyay
Nural network  ER.Abhishek k. upadhyayNural network  ER.Abhishek k. upadhyay
Nural network ER.Abhishek k. upadhyay
abhishek upadhyay
 
Preparation Data Structures 01 introduction
Preparation Data Structures 01 introductionPreparation Data Structures 01 introduction
Preparation Data Structures 01 introduction
Andres Mendez-Vazquez
 
Stack and Queue
Stack and QueueStack and Queue
Stack and Queue
Selvaraj Seerangan
 
Section5 Rbf
Section5 RbfSection5 Rbf
Section5 Rbfkylin
 
Catching co occurrence information using word2vec-inspired matrix factorization
Catching co occurrence information using word2vec-inspired matrix factorizationCatching co occurrence information using word2vec-inspired matrix factorization
Catching co occurrence information using word2vec-inspired matrix factorization
hyunsung lee
 
Neural collaborative filtering-발표
Neural collaborative filtering-발표Neural collaborative filtering-발표
Neural collaborative filtering-발표
hyunsung lee
 
Class test 1 question paper
Class test 1 question paperClass test 1 question paper
Class test 1 question paper
Kuntal Bhowmick
 

What's hot (18)

Matlab HTI summer training course_Lecture2
Matlab HTI summer training course_Lecture2Matlab HTI summer training course_Lecture2
Matlab HTI summer training course_Lecture2
 
Array in c#
Array in c#Array in c#
Array in c#
 
C Programming : Pointers and Arrays, Pointers and Strings
C Programming : Pointers and Arrays, Pointers and StringsC Programming : Pointers and Arrays, Pointers and Strings
C Programming : Pointers and Arrays, Pointers and Strings
 
Arrays
ArraysArrays
Arrays
 
Lesson 36
Lesson 36Lesson 36
Lesson 36
 
Manual for the MATLAB program to solve the 2D truss
Manual for the MATLAB program to solve the 2D trussManual for the MATLAB program to solve the 2D truss
Manual for the MATLAB program to solve the 2D truss
 
Efficient Sparse Coding Algorithms
Efficient Sparse Coding AlgorithmsEfficient Sparse Coding Algorithms
Efficient Sparse Coding Algorithms
 
Echo Function
Echo FunctionEcho Function
Echo Function
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Nural network ER.Abhishek k. upadhyay
Nural network  ER.Abhishek k. upadhyayNural network  ER.Abhishek k. upadhyay
Nural network ER.Abhishek k. upadhyay
 
Preparation Data Structures 01 introduction
Preparation Data Structures 01 introductionPreparation Data Structures 01 introduction
Preparation Data Structures 01 introduction
 
Stack and Queue
Stack and QueueStack and Queue
Stack and Queue
 
Zoooooohaib
ZoooooohaibZoooooohaib
Zoooooohaib
 
Lesson 39
Lesson 39Lesson 39
Lesson 39
 
Section5 Rbf
Section5 RbfSection5 Rbf
Section5 Rbf
 
Catching co occurrence information using word2vec-inspired matrix factorization
Catching co occurrence information using word2vec-inspired matrix factorizationCatching co occurrence information using word2vec-inspired matrix factorization
Catching co occurrence information using word2vec-inspired matrix factorization
 
Neural collaborative filtering-발표
Neural collaborative filtering-발표Neural collaborative filtering-발표
Neural collaborative filtering-발표
 
Class test 1 question paper
Class test 1 question paperClass test 1 question paper
Class test 1 question paper
 

Similar to Segment Tree

Segment tree
Segment treeSegment tree
Segment tree
shohanjh09
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
K Hari Shankar
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
GOKULKANNANMMECLECTC
 
Java Foundations: Arrays
Java Foundations: ArraysJava Foundations: Arrays
Java Foundations: Arrays
Svetlin Nakov
 
Segment tree
Segment treeSegment tree
Segment tree
Sindhuja Kumar
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
pppepito86
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
BG Java EE Course
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 DsQundeel
 
Data Structure
Data StructureData Structure
Data Structuresheraz1
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 DsQundeel
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
Abhishek Tirkey
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
GauravPandey43518
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
Dev Chauhan
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
Shakil Ahmed
 
Virtusa questions placement preparation guide
Virtusa questions placement preparation guideVirtusa questions placement preparation guide
Virtusa questions placement preparation guide
ThalaAjith33
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdf
UmarMustafa13
 
unit1Intro_final.pptx
unit1Intro_final.pptxunit1Intro_final.pptx
unit1Intro_final.pptx
DEEPAK948083
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2Mohamed Ahmed
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysore
ambikavenkatesh2
 

Similar to Segment Tree (20)

Segment tree
Segment treeSegment tree
Segment tree
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
 
Java Foundations: Arrays
Java Foundations: ArraysJava Foundations: Arrays
Java Foundations: Arrays
 
Segment tree
Segment treeSegment tree
Segment tree
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
 
Data Structure
Data StructureData Structure
Data Structure
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
Virtusa questions placement preparation guide
Virtusa questions placement preparation guideVirtusa questions placement preparation guide
Virtusa questions placement preparation guide
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdf
 
unit1Intro_final.pptx
unit1Intro_final.pptxunit1Intro_final.pptx
unit1Intro_final.pptx
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysore
 

Recently uploaded

AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
veerababupersonal22
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
aqil azizi
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
ChristineTorrepenida1
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
ssuser7dcef0
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTSHeap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Soumen Santra
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 

Recently uploaded (20)

AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTSHeap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 

Segment Tree

  • 2. Presented BY:  Shohanur Rahman  ID# 1321368042  CSE 225 (Data Structure)  North South University  Email: shohan.nsu.cse@gmail.com 2
  • 3. Segment Tree Definition: In computer science, a segment tree is a tree data structure for storing intervals, or segments. It allows querying which of the stored segments contain a given point. It can be implemented as a dynamic structure. 3
  • 5. Sample Problem:  You are given n (1<=n<=100000) integers & 100000 query. Each query you have to change a value of an element Or you have to given the minimum value of a range. 5
  • 7. Initial Functions: We start with a segment arr[1 . . . n]. 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 corresponding node. 7
  • 9. Initial Functions( in tree look): 9
  • 10. Initial Functions(Algorithm): #define mx 100001 int arr[mx]; int tree[mx*3]; void init(int node,int b,int e) { if(b==e) { tree[node]=arr[b]; return; } int Left=node*2; int Right=node*2+1; int mid=(b+e)/2; init(Left,b,mid); init(Right,mid+1,e); tree[node]=tree[Left]+tree[Right]; } 10
  • 12. Query Functions:  Once the tree is initialized, how to get the sum using the initialized segment tree.Now,we use Query function.Here 3 types of cases can be happening. 12
  • 14. Query Functions: case A: if(b>=i&&e<=j);then the segment is :Releavent segment. case B: if (i > e || j < b);means current segment is outside of i-j,then its valuless. case C: if case A and case B are not true and some of the parts is included in i-j , then we need to break the segment and take some of the parts. 14
  • 15. Query Functions(Algorithm): int query(int node,int b,int e,int i,int j) { if (i > e || j < b) return 0; if (b >= i && e <= j) return tree[node]; int Left=node*2; int Right=node*2+1; int mid=(b+e)/2; int p1=query(Left,b,mid,i,j); int p2=query(Right,mid+1,e,i,j); return p1+p2; } 15
  • 16. Update Function:  Like tree initialization and query operations, update can also be done recursively. We are given an index which needs to update. Let new value be the value to be added. We start from root of the segment tree, and add new value to all nodes which have given index in their range. If a node doesn’t have given index in its range, we don’t make any changes to that node. 16
  • 18. Update Function(Algorithm): void update(int node,int b,int e,int i,int newvalue) { if (i > e || i < b) return; if (b >= i && e <= i) { tree[node]=newvalue; return; } int Left=node*2; int Right=node*2+1; int mid=(b+e)/2; update(Left,b,mid,i,newvalue); update(Right,mid+1,e,i,newvalue); tree[node]=tree[Left]+tree[Right]; } 18
  • 19. Time Complexity: Function Name: Time: 1.Initial Function O(n log n) 2.Query Function O(log n) 3.Update Function O(log n) 19
  • 20. Applications:  Using segment trees we get an <O(N), O(log N)> algorithm. Segment trees are very powerful, not only because they can be used for RMQ(Range Minimum Query). They are a very flexible data structure, can solve even the dynamic version of RMQ problem, and have numerous applications in range searching problems. 20
  • 21. 21
  • 22. Time to run code…….. 22