-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Segment_tree with lazy propagation for range sum problem. #148
Open
Cryptedknight
wants to merge
2
commits into
pirataspinhal:master
Choose a base branch
from
Cryptedknight:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
data_structures/segment_tree/C++/segment_Tree_with_lazyprop.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
#include<bits/stdc++.h> | ||
#define MAX 1000 | ||
using namespace std; | ||
int tree[MAX] = {0}; // To store segment tree | ||
int lazy[MAX] = {0}; // To store pending updates | ||
|
||
void updateRangeUtil(int si, int ss, int se, int us, | ||
int ue, int diff) | ||
{ | ||
if (lazy[si] != 0) | ||
{ | ||
tree[si] += (se-ss+1)*lazy[si]; | ||
|
||
if (ss != se) | ||
{ | ||
lazy[si*2 + 1] += lazy[si]; | ||
lazy[si*2 + 2] += lazy[si]; | ||
} | ||
lazy[si] = 0; | ||
} | ||
|
||
if (ss>se || ss>ue || se<us) | ||
return ; | ||
if (ss>=us && se<=ue) | ||
{ | ||
tree[si] += (se-ss+1)*diff; | ||
if (ss != se) | ||
{ | ||
lazy[si*2 + 1] += diff; | ||
lazy[si*2 + 2] += diff; | ||
} | ||
return; | ||
} | ||
|
||
int mid = (ss+se)/2; | ||
updateRangeUtil(si*2+1, ss, mid, us, ue, diff); | ||
updateRangeUtil(si*2+2, mid+1, se, us, ue, diff); | ||
tree[si] = tree[si*2+1] + tree[si*2+2]; | ||
} | ||
|
||
void updateRange(int n, int us, int ue, int diff) | ||
{ | ||
updateRangeUtil(0, 0, n-1, us, ue, diff); | ||
} | ||
|
||
int getSumUtil(int ss, int se, int qs, int qe, int si) | ||
{ | ||
if (lazy[si] != 0) | ||
{ | ||
tree[si] += (se-ss+1)*lazy[si]; | ||
if (ss != se) | ||
{ | ||
// Since we are not yet updating children os si, | ||
// we need to set lazy values for the children | ||
lazy[si*2+1] += lazy[si]; | ||
lazy[si*2+2] += lazy[si]; | ||
} | ||
|
||
// unset the lazy value for current node as it has | ||
// been updated | ||
lazy[si] = 0; | ||
} | ||
if (ss>se || ss>qe || se<qs) | ||
return 0; | ||
if (ss>=qs && se<=qe) | ||
return tree[si]; | ||
int mid = (ss + se)/2; | ||
return getSumUtil(ss, mid, qs, qe, 2*si+1) + | ||
getSumUtil(mid+1, se, qs, qe, 2*si+2); | ||
} | ||
int getSum(int n, int qs, int qe) | ||
{ | ||
// Check for erroneous input values | ||
if (qs < 0 || qe > n-1 || qs > qe) | ||
{ | ||
printf("Invalid Input"); | ||
return -1; | ||
} | ||
|
||
return getSumUtil(0, n-1, qs, qe, 0); | ||
} | ||
|
||
// A recursive function that constructs Segment Tree for | ||
// array[ss..se]. si is index of current node in segment | ||
// tree st. | ||
void constructSTUtil(int arr[], int ss, int se, int si) | ||
{ | ||
// out of range as ss can never be greater than se | ||
if (ss > se) | ||
return ; | ||
|
||
// If there is one element in array, store it in | ||
// current node of segment tree and return | ||
if (ss == se) | ||
{ | ||
tree[si] = arr[ss]; | ||
return; | ||
} | ||
|
||
// If there are more than one elements, then recur | ||
// for left and right subtrees and store the sum | ||
// of values in this node | ||
int mid = (ss + se)/2; | ||
constructSTUtil(arr, ss, mid, si*2+1); | ||
constructSTUtil(arr, mid+1, se, si*2+2); | ||
|
||
tree[si] = tree[si*2 + 1] + tree[si*2 + 2]; | ||
} | ||
|
||
/* Function to construct segment tree from given array. | ||
This function allocates memory for segment tree and | ||
calls constructSTUtil() to fill the allocated memory */ | ||
void constructST(int arr[], int n) | ||
{ | ||
constructSTUtil(arr, 0, n-1, 0); | ||
} | ||
int main() | ||
{ | ||
int n; | ||
cin>>n; | ||
int arr[n]; | ||
for(int i=0;i<n;i++)cin>>arr[i]; | ||
constructST(arr, n); | ||
cout<<"\nEnter the range for which sum is to be computed "; | ||
int l,h; | ||
cin>>l>>h; | ||
printf("Sum of values in given range = %d\n", | ||
getSum(n, l, h)); | ||
int val; | ||
cout<<"\nEnter the range for which value is to be updated and also enter the value to be added "; | ||
cin>>l>>h>>val; | ||
updateRange(n, l, h, val); | ||
printf("Updated sum of values in given range = %d\n", | ||
getSum( n, l, h)); | ||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include<bits/stdc++.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this file relate to the problem at all? Although we cannot accept the previous file, perhaps we could change your contribution to focus on this |
||
#define ll long long | ||
#define mp make_pair | ||
#define pb push_back | ||
using namespace std; | ||
const int alp_s = 26; | ||
struct Trie{ | ||
struct Trie *children[alp_s]; | ||
bool eow; | ||
}; | ||
struct Trie *getnode(){ | ||
struct Trie *pnode = new Trie; | ||
for(int i=0;i<26;i++){ | ||
pnode->children[i]=NULL; | ||
} | ||
pnode->eow=false; | ||
return pnode; | ||
} | ||
void insert(struct Trie *root, string key){ | ||
struct Trie *ppoin = root; | ||
int i,index; | ||
for(i=0;i<key.length();i++){ | ||
index = key[i] - 'a'; | ||
if(!ppoin->children[index]){ | ||
ppoin->children[index] = getnode(); | ||
} | ||
ppoin = ppoin->children[index]; | ||
} | ||
|
||
ppoin->eow = true; | ||
} | ||
bool search(struct Trie *root, string key){ | ||
struct Trie *ppoin = root; | ||
for(int i=0;i<key.length();i++){ | ||
int index = key[i] - 'a'; | ||
if(!ppoin->children[index])return false; | ||
ppoin = ppoin->children[index]; | ||
} | ||
return(ppoin!=NULL && ppoin->eow); | ||
} | ||
int main(){ | ||
int n; | ||
cin>>n; | ||
string keys[n]; | ||
for(int i=0;i<n;i++){ | ||
cin>>keys[i]; | ||
} | ||
struct Trie *root = getnode(); | ||
for(int i=0;i<n;i++){ | ||
insert(root,keys[i]); | ||
} | ||
bool res = search(root,"pprt"); | ||
if(res)cout<<"Yes"; | ||
else cout<<"No"; | ||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @Cryptedknight, thanks for the contribution! 😄
Please take a look at our rules and guidelines. I apologize, but we cannot accept this file.
Source found on: https://www.geeksforgeeks.org/lazy-propagation-in-segment-tree/