Skip to content
Snippets Groups Projects
Commit 1e114540 authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei
Browse files

Fix segment trees

parent 8c2ff920
No related branches found
No related tags found
No related merge requests found
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* Complexity (Space): O(n) * Complexity (Space): O(n)
*/ */
int N, tree[2 * MAX], lazy[2 * MAX], v[MAX]; int N, tree[4 * MAX], lazy[4 * MAX], v[MAX];
// Build tree with elements from v // Build tree with elements from v
void build_tree(int node = 1, int a = 0, int b = N) { void build_tree(int node = 1, int a = 0, int b = N) {
...@@ -63,7 +63,7 @@ void update_tree(int i, int j, int val, int node = 1, int a = 0, int b = N) { ...@@ -63,7 +63,7 @@ void update_tree(int i, int j, int val, int node = 1, int a = 0, int b = N) {
tree[node] = tree[node * 2] + tree[node * 2 + 1]; tree[node] = tree[node * 2] + tree[node * 2 + 1];
} }
// Return sum from i to j // Return sum of [i,j]
int query_tree(int i, int j, int node = 1, int a = 0, int b = N) { int query_tree(int i, int j, int node = 1, int a = 0, int b = N) {
if (a > b || a > j || b < i) if (a > b || a > j || b < i)
return 0; return 0;
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* Complexity (Space): O(n) * Complexity (Space): O(n)
*/ */
int N, tree[2 * MAX], v[MAX]; int N, tree[4 * MAX], v[MAX];
// Build tree with elements from v // Build tree with elements from v
void build_tree(int node = 1, int a = 0, int b = N) { void build_tree(int node = 1, int a = 0, int b = N) {
...@@ -42,7 +42,7 @@ void update_tree(int idx, int val, int node = 1, int a = 0, int b = N) { ...@@ -42,7 +42,7 @@ void update_tree(int idx, int val, int node = 1, int a = 0, int b = N) {
tree[node] = tree[node * 2] + tree[node * 2 + 1]; tree[node] = tree[node * 2] + tree[node * 2 + 1];
} }
// Return sum from i to j // Return sum of [i,j]
int query_tree(int i, int j, int node = 1, int a = 0, int b = N) { int query_tree(int i, int j, int node = 1, int a = 0, int b = N) {
if (a > b || a > j || b < i) if (a > b || a > j || b < i)
return 0; return 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment