Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f9aa509

Browse files
committed
refactor fenwick tree
1 parent cebbede commit f9aa509

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

‎Algorithms/BIT.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
* Author : joney_000[let_me_start]
88
* Algorithm : Bit / fenwick tree
99
* Platform : Codeforces
10-
* Ref : https://sanugupta.wordpress.com/2014/08/29/binary-indexed-tree-fenwick-tree/
10+
* Ref : 1.) https://sanugupta.wordpress.com/2014/08/29/binary-indexed-tree-fenwick-tree/
11+
* : 2.) https://www.topcoder.com/community/competitive-programming/tutorials/binary-indexed-trees/
1112
*/
1213

13-
public class A{
14+
public class BIT{
1415

1516
private InputStream inputStream ;
1617
private OutputStream outputStream ;
@@ -27,8 +28,8 @@ public class A{
2728
private final int INF = Integer.MAX_VALUE;
2829
private final long INF_L = Long.MAX_VALUE / 10;
2930

30-
public A(){}
31-
public A(boolean stdIO)throws FileNotFoundException{
31+
public BIT(){}
32+
public BIT(boolean stdIO)throws FileNotFoundException{
3233
// stdIO = false;
3334
if(stdIO){
3435
inputStream = System.in;
@@ -41,12 +42,13 @@ public A(boolean stdIO)throws FileNotFoundException{
4142
out = new PrintWriter(outputStream);
4243
}
4344

44-
45+
final int MAX_TREE_SIZE = 200000;
46+
4547
void run()throws Exception{
4648
int n = i(); int q = i(); int m = i();
4749
int qry[][] = new int[3][q + 1];// 0=>l, 1=> r, 2=>val
4850
int a[] = new int[n + 1];
49-
51+
initializeTree(MAX_TREE_SIZE);
5052
for(int i = 1; i <= n; i++){
5153
a[i] = i();
5254
update(tree, i, a[i]); // point update this time,
@@ -65,23 +67,31 @@ void run()throws Exception{
6567
}
6668

6769
for(int i = 1; i <= n; i++){
68-
long res = qry(tree, i); // case of point qry
70+
long res = query(tree, i); // case of point qry
6971
out.write(""+res+" ");
7072
}
7173

7274
}// end run
7375

74-
finalintMAX = 200000;
75-
longtree[] = newlong[MAX + 5];
76+
privatelong []tree;
77+
privateinttreeSize;
7678

77-
void update(long tree[], int idx, long value){ // idx to MAX
78-
while(idx < MAX){
79+
// time and space O(treeSize)
80+
private void initializeTree(int treeSize){
81+
this.treeSize = treeSize;
82+
tree = new long[treeSize];
83+
}
84+
85+
// range update, time: O(log treeSize)
86+
private void update(long tree[], int idx, long value){
87+
while(idx < this.treeSize){
7988
tree[idx] += value;
8089
idx += (idx & (-idx));
8190
}
8291
}
8392

84-
long qry(long tree[], int idx){
93+
// range sum, time: O(log treeSize)
94+
private long query(long tree[], int idx){
8595
long ret = 0;
8696
while(idx > 0){
8797
ret += tree[idx];
@@ -199,7 +209,7 @@ private void closeResources(){
199209

200210
public static void main(String[] args) throws java.lang.Exception{
201211

202-
A driver = new A(true);
212+
BIT driver = new BIT(true);
203213
driver.run();
204214
driver.closeResources();
205215
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /