Last active
June 17, 2018 22:48
-
-
Save sazid/d329b4d65a7fe2ecce01f4fcf33d4317 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| int query(int node, int b, int e, int l, int r) { | |
| // no overlap | |
| if (b > r || e < l) return 0; | |
| // total overlap | |
| if (b >= l && e <= r) return tree[node]; | |
| // partial overlap | |
| int left = 2*node; | |
| int right = 2*node + 1; | |
| int mid = (b + e)/2; | |
| int p1 = query(left, b, mid, l, r); | |
| int p2 = query(right, mid + 1, e, l, r); | |
| return p1 + p2; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment