SlideShare a Scribd company logo
1 of 23
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
 
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 StringsSelvaraj Seerangan
 
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 AlgorithmsAnshu Dipit
 
Data structures using C
Data structures using CData structures using C
Data structures using CPdr Patnaik
 
Nural network ER.Abhishek k. upadhyay
Nural network  ER.Abhishek k. upadhyayNural network  ER.Abhishek k. upadhyay
Nural network ER.Abhishek k. upadhyayabhishek upadhyay
 
Preparation Data Structures 01 introduction
Preparation Data Structures 01 introductionPreparation Data Structures 01 introduction
Preparation Data Structures 01 introductionAndres Mendez-Vazquez
 
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 factorizationhyunsung 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 paperKuntal 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

Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithmK 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 notesGOKULKANNANMMECLECTC
 
Java Foundations: Arrays
Java Foundations: ArraysJava Foundations: Arrays
Java Foundations: ArraysSvetlin Nakov
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithmspppepito86
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0BG Java EE Course
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 DsQundeel
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 DsQundeel
 
Data Structure
Data StructureData Structure
Data Structuresheraz1
 
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).pptxAbhishek 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).pptxGauravPandey43518
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDev Chauhan
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structureShakil Ahmed
 
Virtusa questions placement preparation guide
Virtusa questions placement preparation guideVirtusa questions placement preparation guide
Virtusa questions placement preparation guideThalaAjith33
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfUmarMustafa13
 
unit1Intro_final.pptx
unit1Intro_final.pptxunit1Intro_final.pptx
unit1Intro_final.pptxDEEPAK948083
 
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 mysoreambikavenkatesh2
 

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
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
 
Data Structure
Data StructureData Structure
Data Structure
 
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

Databricks Generative AI FoundationCertified.pdf
Databricks Generative AI FoundationCertified.pdfDatabricks Generative AI FoundationCertified.pdf
Databricks Generative AI FoundationCertified.pdfVinayVadlagattu
 
Independent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging StationIndependent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging Stationsiddharthteach18
 
01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...
01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...
01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...AshwaniAnuragi1
 
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...Amil baba
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptjigup7320
 
Diploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfDiploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfJNTUA
 
Basics of Relay for Engineering Students
Basics of Relay for Engineering StudentsBasics of Relay for Engineering Students
Basics of Relay for Engineering Studentskannan348865
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailingAshishSingh1301
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Ramkumar k
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsMathias Magdowski
 
CLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference ModalCLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference ModalSwarnaSLcse
 
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024EMMANUELLEFRANCEHELI
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...josephjonse
 
DBMS-Report on Student management system.pptx
DBMS-Report on Student management system.pptxDBMS-Report on Student management system.pptx
DBMS-Report on Student management system.pptxrajjais1221
 
Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...IJECEIAES
 
What is Coordinate Measuring Machine? CMM Types, Features, Functions
What is Coordinate Measuring Machine? CMM Types, Features, FunctionsWhat is Coordinate Measuring Machine? CMM Types, Features, Functions
What is Coordinate Measuring Machine? CMM Types, Features, FunctionsVIEW
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxMustafa Ahmed
 
Databricks Generative AI Fundamentals .pdf
Databricks Generative AI Fundamentals  .pdfDatabricks Generative AI Fundamentals  .pdf
Databricks Generative AI Fundamentals .pdfVinayVadlagattu
 
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfInstruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfEr.Sonali Nasikkar
 

Recently uploaded (20)

Databricks Generative AI FoundationCertified.pdf
Databricks Generative AI FoundationCertified.pdfDatabricks Generative AI FoundationCertified.pdf
Databricks Generative AI FoundationCertified.pdf
 
Independent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging StationIndependent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging Station
 
01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...
01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...
01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...
 
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) ppt
 
Diploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfDiploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdf
 
Basics of Relay for Engineering Students
Basics of Relay for Engineering StudentsBasics of Relay for Engineering Students
Basics of Relay for Engineering Students
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailing
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility Applications
 
CLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference ModalCLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference Modal
 
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
DBMS-Report on Student management system.pptx
DBMS-Report on Student management system.pptxDBMS-Report on Student management system.pptx
DBMS-Report on Student management system.pptx
 
Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...
 
Signal Processing and Linear System Analysis
Signal Processing and Linear System AnalysisSignal Processing and Linear System Analysis
Signal Processing and Linear System Analysis
 
What is Coordinate Measuring Machine? CMM Types, Features, Functions
What is Coordinate Measuring Machine? CMM Types, Features, FunctionsWhat is Coordinate Measuring Machine? CMM Types, Features, Functions
What is Coordinate Measuring Machine? CMM Types, Features, Functions
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptx
 
Databricks Generative AI Fundamentals .pdf
Databricks Generative AI Fundamentals  .pdfDatabricks Generative AI Fundamentals  .pdf
Databricks Generative AI Fundamentals .pdf
 
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfInstruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
 

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