Skip to content
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 enhancements #56

Merged
merged 14 commits into from
Nov 22, 2015
7 changes: 7 additions & 0 deletions hackpack.bib
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ @misc{dppractice
url = "http://people.cs.clemson.edu/~bcdean/dp_practice/"
}

@misc{optimalmilking,
author = "Dr. Brian Dean",
title = "Optimal Milking",
year = "2013",
url = "http://usaco.org/index.php?page=viewproblem2&cpid=365"
}

@misc{acmsoutheastregional2013,
author = "ACM Southeast Regional",
title = "ACM Southeast Regional 2013",
Expand Down
117 changes: 37 additions & 80 deletions structures/segment-tree/array-tree.eps

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ using namespace std;
// #endif
const unsigned int N = 14;

// #ifdef hackpackpp
// build the segment tree
//
// data - source array to build segment tree from
// start - the start index of the array (should be 0)
// end - the end index of the array (size - 1)
// tree - the array to hold the segment tree sized appropriately
// st_idx - the current index of the segment tree (should be 0 when first called)
// #endif
int build_segment_tree(int* const data, const unsigned int start, const unsigned int end, int* const tree, const unsigned int st_idx)
{
// #ifdef hackpackpp
// build the segment tree from source data
//
// data - source array to build the segment tree from
// start - the start index of the array (should be 0 when first called)
// end - the end index of the array (size - 1)
// tree - the array to hold the segment tree, sized appropriately
// st_idx - the current index of the segment tree (should be 0 when first called)
// #endif
// #ifdef hackpackpp

// #ifdef hackpackpp
// if start == end, this is a leaf node and should
// have the original value from the array here
Expand Down Expand Up @@ -56,24 +58,20 @@ int build_segment_tree(int* const data, const unsigned int start, const unsigned
return tree[st_idx];
}

// #ifdef hackpackpp
// update a value in the segment tree
//
// to support range sums, this function would have to be modified to recurse
// down the tree to the leaf node before changing values; after the leaf node
// is updated, the new value should be passed back as a return value and each
// parent should update the sum they have by re-adding the values from the
// two children it has
//
// tree - the segment tree with values to be updated
// start - the start index of the _source_ array (should be 0)
// end - the end index of the _source_ array (size - 1)
// changed_idx - the index of the element that changed
// new_val - the new value of the element at changed_idx
// st_idx - the current index of the segment tree (should be 0 when first called)
// #endif

void update_segment_tree(int* const tree, const unsigned int start, const unsigned int end, const unsigned int changed_idx, const int new_val, const unsigned int st_idx)
{
// #ifdef hackpackpp
// update a value in the segment tree
//
// tree - the segment tree with values to be updated
// start - the start index of the _source_ array (should be 0)
// end - the end index of the _source_ array (size - 1)
// changed_idx - the index of the element that changed
// new_val - the new value of the element at changed_idx
// st_idx - the current index of the segment tree (should be 0 when first called)
// #endif

// #ifdef hackpackpp
// out of range; should not be counted
// #endif
Expand Down Expand Up @@ -102,19 +100,20 @@ void update_segment_tree(int* const tree, const unsigned int start, const unsign
return;
}

// #ifdef hackpackpp
// find the largest value for a range
//
// tree - the segment tree to query
// start - the start index of the _source_ array
// end - the end index of the _source_ array
// range_start - the start index of the query
// range_end - the end index of the query
// greatest - the greatest value found so far (set to zero when first calling)
// st_idx - the current index in the segtree (set to zero when first calling)
// #endif
int query_segment_tree(const int* const tree, const unsigned int start, const unsigned int end, const unsigned int range_start, const unsigned int range_end, int greatest, const unsigned int st_idx)
{
// #ifdef hackpackpp
// find the largest value for a range
//
// tree - the segment tree to query
// start - the start index of the _source_ array
// end - the end index of the _source_ array
// range_start - the start index of the query
// range_end - the end index of the query
// greatest - the greatest value found so far (set to zero when first calling)
// st_idx - the current index in the segtree (set to zero when first calling)
// #endif

// #ifdef hackpackpp
// out of range; do not continue
// #endif
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
5 3
1
2
3
4
5
5 2
2 7
1 10

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
32

Loading