Skip to content

Commit c8cf016

Browse files
AbraaoCFabranhe
authored andcommitted
Create BinaryIndexedTree2d.cpp
1 parent a967b45 commit c8cf016

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Binary Indexed Tree of sum in 2d array
2+
// AbraaoCF - UFCG
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
#define maxx 1100
6+
int bit[maxx][maxx];
7+
void update(int idx, int idy, int value)
8+
{
9+
while(idx <= maxx)
10+
{
11+
int idy_temp = idy;
12+
while(idy_temp <= maxx)
13+
{
14+
bit[idx][idy_temp] += value;
15+
idy_temp += (idy_temp &-idy_temp);
16+
}
17+
idx += (idx&-idx);
18+
}
19+
return;
20+
}
21+
int query(int idx,int idy)
22+
{
23+
int sum = 0;
24+
while(idx > 0)
25+
{
26+
int idy_temp = idy;
27+
while(idy_temp > 0)
28+
{
29+
sum += bit[idx][idy_temp];
30+
idy_temp -= (idy_temp &-idy_temp);
31+
}
32+
idx -= (idx&-idx);
33+
}
34+
return sum;
35+
}

0 commit comments

Comments
 (0)