Last active
April 12, 2017 06:13
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
struct Node | |
{ | |
int val; | |
int mark; | |
Node() : val(0), mark(0) | |
{ | |
} | |
}; | |
SegmentTree(vector<int> nums) { | |
n = nums.size(); | |
if (!n)return; | |
st = vector<Node>(3 * n); | |
createTree(nums, 0, n - 1, 0); | |
} | |
void createTree(vector<int>& nums, int lo, int hi, int i) | |
{ | |
if (lo == hi) | |
{ | |
st[i].val = nums[lo]; | |
return; | |
} | |
int mid = lo + (hi - lo) / 2; | |
createTree(nums, lo, mid, 2 * i + 1); | |
createTree(nums, mid + 1, hi, 2 * i + 2); | |
st[i].val = st[2 * i + 1].val + st[2 * i + 2].val; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment