Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Last active April 12, 2017 06:13
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