From db95b02de89180572d7b21b584399d1c3b400e04 Mon Sep 17 00:00:00 2001 From: Ayush Shah Date: Mon, 5 Oct 2020 08:46:43 +0530 Subject: [PATCH 1/2] Create Segment_Tree_Template.cpp --- Data-Structures/Segment_Tree_Template.cpp | 69 +++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Data-Structures/Segment_Tree_Template.cpp diff --git a/Data-Structures/Segment_Tree_Template.cpp b/Data-Structures/Segment_Tree_Template.cpp new file mode 100644 index 0000000..5adabd6 --- /dev/null +++ b/Data-Structures/Segment_Tree_Template.cpp @@ -0,0 +1,69 @@ +// Name : Segment tree for range sum queries and point updates. +// Time complexity : build : nlogn +// query : logn +// update : logn +// Space Complexity : n + +struct segtree { + vector arr; + int sz = 1; + void init(int n) { + while(sz < n) sz *= 2; + arr.assign(2*sz,0); + } + void update(int pos, int val, int curr_node, int l, int r) { + if(r-l == 1) { + // here the curr_node will become = to i; + + + arr[curr_node] = val; + return; + } + // now decide where to go - left subtree or right subtree + + + int m = (l+r)/2; + if(pos < m) { + // go left + + update(pos,val,2*curr_node+1,l,m); + + // curr_node becomes the left child of x i.e 2*x+1; + } else { + // go right + + update(pos,val,2*curr_node+2,m,r); + + // curr_node becomes the right child of x i.e 2*x+2; + } + // and here the sum of curr_node = sum of left child and right child + + + arr[curr_node] = arr[2*curr_node+1]+arr[2*curr_node+2]; + return; + } + + void update(int pos, int val) { + update(pos,val,0,0,sz); + + + // 0 means the root; we start update with the root + } + + /*------------ + this is where all the changes will be made for point updates; + -------------*/ + + + int sum(int l, int r, int curr_node, int currL, int currR) { + if(currL >= r || currR <= l) return 0; // outside the segment + if(currL >= l && currR <= r) return arr[curr_node]; // totally inside the segment + int m = (currL+currR)/2; + int ans = sum(l,r,2*curr_node+1,currL,m) + sum(l,r,2*curr_node+2,m,currR); + return ans; + } + + int sum(int l, int r) { + return sum(l,r,0,0,sz); + } +}; From 5607ce177e44d51115f3dfd80c658a1550880835 Mon Sep 17 00:00:00 2001 From: Ayush Shah Date: Mon, 5 Oct 2020 09:00:11 +0530 Subject: [PATCH 2/2] gcd of 2 numbers in logarithmic time --- Algebra-and-Numerical-Methods/GCD.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Algebra-and-Numerical-Methods/GCD.cpp diff --git a/Algebra-and-Numerical-Methods/GCD.cpp b/Algebra-and-Numerical-Methods/GCD.cpp new file mode 100644 index 0000000..dd79e61 --- /dev/null +++ b/Algebra-and-Numerical-Methods/GCD.cpp @@ -0,0 +1,23 @@ +// Name : GCD of two numbers in log(max(a,b)) time. +// Time Complexity : log(max(a,b)); +// Space Complexity : constant + +#include +#define int long long +using namespace std; + +int gcd(int a, int b) { + // use the result that gcd(a,b) = gcd(b%a,a) recursively; + if(b == 0) return a; + if(a == 0) return b; + return gcd(b%a,a); +} + +signed main() +{ + int a, b; + // Enter the 2 numbers to calculate the gcd; + cin>>a>>b; + cout<