Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathFCFS.java
More file actions
Latest commit
83 lines (50 loc) · 1.18 KB
/
Copy pathFCFS.java
File metadata and controls
83 lines (50 loc) · 1.18 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
importjava.util.*;
importjava.lang.*;
importjava.io.*;
classBinaryIndexedTree
{
finalstaticintMAX = 1000;
staticintBITree[] = newint[MAX];
intgetSum(intindex)
{
intsum = 0; // Iniialize result
index = index + 1;
while(index>0)
{
sum += BITree[index];
index -= index & (-index);
}
returnsum;
}
publicstaticvoidupdateBIT(intn, intindex,
intval)
{
index = index + 1;
while(index <= n)
{
BITree[index] += val;
index += index & (-index);
}
}
voidconstructBITree(intarr[], intn)
{
for(inti=1; i<=n; i++)
BITree[i] = 0;
for(inti = 0; i < n; i++)
updateBIT(n, i, arr[i]);
}
publicstaticvoidmain(Stringargs[])
{
intfreq[] = {2, 1, 1, 3, 2, 3,
4, 5, 6, 7, 8, 9};
intn = freq.length;
BinaryIndexedTreetree = newBinaryIndexedTree();
tree.constructBITree(freq, n);
System.out.println("Sum of elements in arr[0..5]"+
" is "+ tree.getSum(5));
freq[3] += 6;
updateBIT(n, 3, 6);
System.out.println("Sum of elements in arr[0..5]"+
" after update is " + tree.getSum(5));
}
}