-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFenwickTree.cpp
More file actions
192 lines (154 loc) · 3.32 KB
/
FenwickTree.cpp
File metadata and controls
192 lines (154 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <bits/stdc++.h>
using namespace std;
//Construction of Fenwick Tree (Also Called : Binary Indexed Tree)===============================================
vector<long long> farr;
long long prefixSum(int idx)
{
long long sum = 0;
while (idx > 0)
{
sum += farr[idx];
idx -= (idx & -idx);
}
return sum;
}
long long query(int l, int r)
{
return prefixSum(r) - prefixSum(l - 1);
}
void update(int idx, int delta)
{
while (idx < farr.size())
{
farr[idx] += delta;
idx += (idx & -idx);
}
}
void construct(vector<long long> &arr)
{
for (int i = 1; i < arr.size(); i++)
{
update(i, arr[i]);
}
}
void fenwickTree(vector<long long> &arr)
{
int n = arr.size();
farr.resize(n);
construct(arr);
for (int ele : farr)
cout << ele << " ";
cout << endl;
int q;
cin >> q;
while (q-- > 0)
{
char ch;
cin >> ch;
if (ch == 'q')
{
int l, r;
cin >> l >> r;
long long ans = query(l, r);
cout << ans << endl;
}
else
{
int idx, delta;
cin >> idx >> delta;
update(idx, delta);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<long long> arr(n + 1);
for (int i = 1; i <= n; i++)
cin >> arr[i];
fenwickTree(arr);
return 0;
}
//Leetcode Question=============================================
// 303. Range Sum Query - Immutable
/*
Approach : Prefix Sum
Preprocessing: O(n), O(n)
Query : O(1) (Range Sum)
*/
class NumArray {
public:
vector<int> prefixSum;
NumArray(vector<int>& nums) {
int n = nums.size();
prefixSum.resize(n);
prefixSum[0] = nums[0];
for(int i = 1; i < n; i++)
prefixSum[i] = prefixSum[i-1] + nums[i];
}
int sumRange(int left, int right) {
if(left == 0) return prefixSum[right];
return prefixSum[right] - prefixSum[left-1];
}
};
// 307. Range Sum Query - Mutable
/*
Approach : Fenwick Tree
Preprocessing: O(nlogn), O(n)
Query : O(logn) (Range Sum)
Update : O(logn)
*/
class NumArray
{
public:
vector<int> farr;
vector<int> nums;
void construct(vector<int> &arr)
{
for (int i = 1; i < arr.size(); i++)
{
updateFenwickArray(i, arr[i]);
}
}
void updateFenwickArray(int idx, int delta)
{
while (idx < farr.size())
{
farr[idx] += delta;
idx += (idx & -idx);
}
}
int prefixSum(int idx)
{
int sum = 0;
while (idx > 0)
{
sum += farr[idx];
idx -= (idx & -idx);
}
return sum;
}
NumArray(vector<int> &nums)
{
int n = nums.size();
this->nums = nums;
vector<int> arr(n + 1);
farr.resize(n + 1);
for (int i = 1; i <= n; i++)
arr[i] = nums[i - 1];
construct(arr);
}
void update(int index, int val)
{
int delta = val - nums[index];
nums[index] = val;
updateFenwickArray(index + 1, delta);
}
int sumRange(int left, int right)
{
return prefixSum(right + 1) - prefixSum(left - 1 + 1);
}
};