Skip to content

Commit 309ba15

Browse files
committed
2D Fenwick Updated
1 parent a809ba9 commit 309ba15

7 files changed

+462
-71
lines changed

Data Structure/00 BIT.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ A data structure to quickly do the following -
1313
1414
Operations
1515
----------
16-
- Add value to an index : O(NlogN)
17-
- Calculate Prefix Sum upto an index : O(NlogN)
16+
- Add value to an index : O(logN)
17+
- Calculate Prefix Sum upto an index : O(logN)
1818
1919
**/
2020

Data Structure/01 BIT (POINT update , RANGE query).cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ A data structure to quickly do the following -
1313
1414
Operations
1515
----------
16-
- Add value to an index : O(NlogN)
17-
- Calculate Prefix Sum upto an index : O(NlogN)
16+
- Add value to an index : O(logN)
17+
- Calculate Prefix Sum upto an index : O(logN)
1818
1919
**/
2020

Data Structure/02 BIT (RANGE update , POINT query).cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ A data structure to quickly do the following -
1414
Operations
1515
----------
1616
17-
- Add value to an index : O(NlogN)
18-
- Calculate Prefix Sum upto an index : O(NlogN)
17+
- Add value to an index : O(logN)
18+
- Calculate Prefix Sum upto an index : O(logN)
1919
2020
Modification to do range updates
2121
--------------------------------
Lines changed: 121 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11

22
/**
33
4-
2D BIT
4+
2D Fenwick Tree
5+
===============
6+
7+
Description
8+
-----------
9+
10+
A data structure to quickly do the following -
11+
1 . Point Update
12+
2 . Prefix Sum Array Update
13+
14+
Operations
15+
----------
16+
- Add value to an index : O( (logN)^2 )
17+
- Calculate Prefix Sum upto an index : O( (logN)^2 )
518
619
**/
720

@@ -13,108 +26,155 @@ using namespace std;
1326
#define LL long long
1427
#define PII pair<int,int>
1528
#define PLL pair<LL,LL>
16-
#define MP make_pair
1729
#define F first
1830
#define S second
19-
#define INF INT_MAX
31+
32+
#define ALL(x) (x).begin(), (x).end()
33+
#define READ freopen("alu.txt", "r", stdin)
34+
#define WRITE freopen("vorta.txt", "w", stdout)
35+
36+
#ifndef ONLINE_JUDGE
37+
#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl
38+
#else
39+
#define DBG(x)
40+
#define endl "\n"
41+
#endif
42+
43+
template<class T1, class T2>
44+
ostream &operator <<(ostream &os, pair<T1,T2>&p);
45+
template <class T>
46+
ostream &operator <<(ostream &os, vector<T>&v);
47+
template <class T>
48+
ostream &operator <<(ostream &os, set<T>&v);
2049

2150
inline void optimizeIO()
2251
{
2352
ios_base::sync_with_stdio(false);
2453
cin.tie(NULL);
2554
}
2655

27-
const int nmax = 1024+5;
28-
const LL LINF = 1e17;
56+
const int nmax = 2e5+7;
2957

30-
string to_str(LL x)
31-
{
32-
stringstream ss;
33-
ss<<x;
34-
return ss.str();
35-
}
36-
37-
int n;
38-
int ara[nmax][nmax];
58+
struct Fenwick2D{
3959

60+
vector<vector<LL>>BIT;
61+
int R,C;
4062

63+
Fenwick2D (int r,int c) : BIT(r+1,vector<LL>(c+1,0)) , R(r) , C(c) {}
4164

42-
/** 2D BIT / FENWICK TREE **/
43-
/*** 1 based indexing ***/
65+
inline int LSB(int x) {return x&(-x);} /// find the number with first bit set
4466

45-
int BIT[nmax][nmax];
46-
inline int lowbit(int x)
47-
{
48-
return x&(-x);
49-
}
67+
/// POINT update : adds val to index (x,y)
68+
void add(int x,int y,LL val)
69+
{
70+
for(int i = x ; i<=R ; i += LSB(i))
71+
for(int j = y ; j<=C ; j += LSB(j))
72+
BIT[i][j] += val;
73+
}
5074

51-
inline void update(int x,int y,int val) /** POINT update , adds to the current value . Point = {x,y} **/
52-
{
53-
for(int i = x; i <= n; i += lowbit(i))
54-
for(int j = y; j <= n; j += lowbit(j))
55-
BIT[i][j] += val;
56-
}
75+
/// prefix sum of [(1,1) , (x,y)] square
76+
LL pref(int x,int y)
77+
{
78+
LL sum = 0;
5779

58-
inline int query(int x,int y) /** RANGE Sum . Range = [{1,1} , {x,y}] **/
59-
{
60-
int sum = 0;
61-
for(int i = x; i > 0; i -= lowbit(i))
62-
for(int j = y; j > 0; j -= lowbit(j))
80+
for(int i = x ; i>0 ; i -= LSB(i))
81+
for(int j = y ; j>0 ; j -= LSB(j))
6382
sum += BIT[i][j];
6483

65-
return sum;
66-
}
67-
68-
int main()
69-
{
70-
//freopen("OUT.txt","w",stdout);
84+
return sum;
85+
}
7186

72-
n = 3;
87+
/// sum of [(xl,yl) , (xh,yh)] square
88+
LL sum(int xl,int yl,int xh,int yh)
89+
{
90+
LL v1 = pref(xh, yh);
91+
LL v2 = pref(xh, yl-1);
92+
LL v3 = pref(xl-1, yh);
93+
LL v4 = pref(xl-1, yl-1);
94+
return v1 - v2 - v3 + v4;
95+
}
7396

74-
int val = 0;
75-
for(int i=1; i<=n; i++)
76-
for(int j=1; j<=n; j++)
97+
void debug()
98+
{
99+
cout<<"Prefix Sum Array :\n";
100+
for(int i=1; i<=R; i++)
77101
{
78-
//cin>>val;
79-
val++;
80-
ara[i][j] = val;
81-
update(i,j,ara[i][j]); /** Initial Update **/
102+
for(int j=1; j<=C; j++)
103+
cout<<pref(i,j)<<" ";
104+
cout<<endl;
82105
}
106+
cout<<endl;
107+
}
108+
};
83109

84110

85-
int query_count;
86-
cin>>query_count;
111+
int main()
112+
{
113+
optimizeIO();
114+
115+
int r,c,q;
116+
cin>>r>>c>>q;
87117

88-
while(query_count--)
118+
Fenwick2D f(r,c);
119+
120+
for(int i=1;i<=q;i++)
89121
{
122+
f.debug();
123+
90124
int type;
91125
cin>>type;
92126

93127
if(type==1)
94128
{
95-
/** Add val to {x,y} **/
96-
int x,y,val;
129+
int x,y;
130+
LL val;
97131
cin>>x>>y>>val;
98132

99-
update(x,y,val);
133+
f.add(x,y,val); /// point update
100134
}
101-
else
135+
else if(type==2)
102136
{
103-
/** Sum of Range = [{x1,y1} , {x2,y2}] **/
104137
int x1,y1,x2,y2;
105138
cin>>x1>>y1>>x2>>y2;
106139

107-
int v1 = query(x2, y2);
108-
int v2 = query(x2, y1-1);
109-
int v3 = query(x1-1, y2);
110-
int v4 = query(x1-1, y1-1);
111-
int ans = v1 - v2 - v3 + v4;
112-
cout<<"Sum : "<<ans<<endl;
140+
cout<<f.sum(x1,y1,x2,y2)<<endl; /// range sum
113141
}
114142
}
115143

144+
return 0;
145+
}
116146

147+
/**
148+
5 6 5
149+
2 2 2 4 5 1
150+
**/
117151

118-
return 0;
152+
template<class T1, class T2>
153+
ostream &operator <<(ostream &os, pair<T1,T2>&p)
154+
{
155+
os<<"{"<<p.first<<", "<<p.second<<"} ";
156+
return os;
157+
}
158+
template <class T>
159+
ostream &operator <<(ostream &os, vector<T>&v)
160+
{
161+
os<<"[ ";
162+
for(T i:v)
163+
{
164+
os<<i<<" " ;
165+
}
166+
os<<" ]";
167+
return os;
119168
}
120169

170+
template <class T>
171+
ostream &operator <<(ostream &os, set<T>&v)
172+
{
173+
os<<"[ ";
174+
for(T i:v)
175+
{
176+
os<<i<<" ";
177+
}
178+
os<<" ]";
179+
return os;
180+
}

0 commit comments

Comments
 (0)