public class Code_IndexTree {
public static void main(String[] args) {
int N = 100;
int V = 100;
int testTime = 2000000;
IndexTree tree = new IndexTree(N);
Right test = new Right(N);
System.out.println("test begin");
for (int i = 0; i < testTime; i++) {
int index = (int) (Math.random() * N) + 1;
if (Math.random() <= 0.5) {
int add = (int) (Math.random() * V);
tree.add(index, add);
test.add(index, add);
} else {
if (tree.sum(index) != test.sum(index)) {
System.out.println("Oops!");
}
}
}
System.out.println("test finish");
}
public static class IndexTree{
private int[] tree;
private int N;
public IndexTree(int size){
N = size;
tree = new int[size + 1];
System.out.println("constructor");
}
public int sum(int index){
int ret = 0;
while(index > 0){
ret += tree[index];
index -= (index & -index);
}
return ret;
}
public void add(int index, int d){
while(index <= N){
tree[index] += d;
index += index & -index;
}
}
}
public static class Right {
private int[] nums;
private int N;
public Right(int size) {
N = size + 1;
nums = new int[N + 1];
System.out.println("constructor_right");
}
public int sum(int index) {
int ret = 0;
for (int i = 1; i <= index; i++) {
ret += nums[i];
}
return ret;
}
public void add(int index, int d) {
nums[index] += d;
}
}
}