Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Created October 17, 2017 04:33
int update(Node* curr, int row1, int row2, int col1, int col2, int row, int col, int val)
{
if(row1 == row2 && col1 == col2)
{
int add = val - curr->sum;
curr->sum += add;
return add;
}
int add = 0, rMid = row1 + (row2 - row1) / 2, cMid = col1 + (col2 - col1) / 2;
if(row <= rMid && col <= cMid)add = update(curr->children[0], row1, rMid, col1, cMid, row, col, val);
else if(row <= rMid && col > cMid)add = update(curr->children[1], row1, rMid, cMid + 1, col2, row, col, val);
else if(row > rMid && col <= cMid)add = update(curr->children[2], rMid + 1, row2, col1, cMid, row, col, val);
else add = update(curr->children[3], rMid + 1, row2, cMid + 1, col2, row, col, val);
curr->sum += add;
return add;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment