Skip to content

Instantly share code, notes, and snippets.

@Jangwa
Created January 29, 2014 16:15
Show Gist options
  • Save Jangwa/8691352 to your computer and use it in GitHub Desktop.
Save Jangwa/8691352 to your computer and use it in GitHub Desktop.
2D BIT BIT can be used as a multi-dimensional data structure. Suppose you have a plane with dots (with non-negative coordinates). You make three queries: set dot at (x , y) remove dot from (x , y) count number of dots in rectangle (0 , 0), (x , y) - where (0 , 0) if down-left corner, (x , y) is up-right corner and sides are parallel to x-axis an…
void update(int x , int y , int val){
while (x <= max_x){
updatey(x , y , val);
// this function should update array tree[x]
x += (x & -x);
}
}
void updatey(int x , int y , int val){
while (y <= max_y){
tree[x][y] += val;
y += (y & -y);
}
}
in one function
-------------------
void update(int x , int y , int val){
int y1;
while (x <= max_x){
y1 = y;
while (y1 <= max_y){
tree[x][y1] += val;
y1 += (y1 & -y1);
}
x += (x & -x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment