forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFenwick_Tree.dart
More file actions
159 lines (141 loc) · 3.89 KB
/
Fenwick_Tree.dart
File metadata and controls
159 lines (141 loc) · 3.89 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
/**
* Fenwick Tree
* -------------------------
* Binary Indexed tree or Fenwick tree is used to solve
* range queries optimally. It has O(n) space complexity,
* O(logn) time complexity to update, O(nlog(n)) time complexity to build
* and O(logn) time complexity to find sum in a given range.
*
*/
// Importing required libraries
import 'dart:io';
// Updating the tree with respect to any updates or construction
void updateBITTree(List tree, int n, int index, int val){
index = index + 1;
while(index <= n){
tree[index] += val;
index += index & ( - index);
}
}
// Method to find the sum till a given index
int getSum(List tree, int index){
int total = 0;
index = index + 1;
while(index > 0){
total += tree[index];
index -= index & ( - index );
}
return total;
}
// Method to find sum between a range using the above method.
int findSum(List tree, int left, int right){
if(left > right){
return -1;
}
else if(left == right){
return getSum(tree, left) - getSum(tree, left - 1);
}
else{
if(left > 0){
return getSum(tree,right) - getSum(tree,left) + (getSum(tree, left) - getSum(tree, left - 1));
}
else if(left == 0){
return getSum(tree,right);
}
else{
return -1;
}
}
}
// Driver method of the program
void main(){
// Input of number of array elements
print("Enter number of elements in the array:");
var input = stdin.readLineSync();
int n = int.parse(input);
// Input of array elements
print("Enter array elements:");
input = stdin.readLineSync();
var lis = input.split(" ");
List array = lis.map(int.parse).toList();
// Fenwick tree initialization
List tree_BIT = List(n + 1);
for(int i = 0; i < n + 1; i ++){
tree_BIT[i] = 0;
}
// Tree construction
for(int i = 0; i < n; i ++){
updateBITTree(tree_BIT, n, i, array[i]);
}
// Input of number of queries
print("Enter number of queries:");
input = stdin.readLineSync();
int q = int.parse(input);
// Input of queries
print("Enter queries:");
for(int i = 0 ; i < q ; i ++){
print("Enter 1 to update, 2 to find sum:");
input = stdin.readLineSync();
int choice = int.parse(input);
switch(choice){
case 1:
// Input for updates
print("Enter index and value to update");
input = stdin.readLineSync();
lis = input.split(" ");
List choices = lis.map(int.parse).toList();
array[choices[0]] += choices[1];
// Updating value
updateBITTree(tree_BIT, n, choices[0], choices[1]);
break;
case 2:
// Input for sum queries
print("Enter range to find the sum");
input = stdin.readLineSync();
lis = input.split(" ");
List choices = lis.map(int.parse).toList();
// Finding sum
int result = findSum(tree_BIT,choices[0],choices[1]);
print(result);
break;
}
}
}
/**
*
* Sample Input
* ------------------------------------
* Enter number of elements in the array:
* 12
* Enter array elements:
* 2 1 1 3 2 3 4 5 6 7 8 9
* Enter number of queries:
* 5
* Enter 1 to update, 2 to find sum:
* 2
* Enter range to find the sum
* 0 5
* Enter 1 to update, 2 to find sum:
* 1
* Enter index and value to update
* 3 9
* Enter 1 to update, 2 to find sum:
* 2
* Enter range to find the sum
* 0 5
* Enter 1 to update, 2 to find sum:
* 2
* Enter range to find the sum
* 5 9
* Enter 1 to update, 2 to find sum:
* 2
* Enter range to find the sum
* 8 8
*
* Sample Output
* ----------------------------------------
* 12
* 21
* 25
* 6
*/