private void modify(SegTreeNode root, int index, int value) { if (root.start == index && root.end == index) { root.count += value; return; } int mid = root.start + (root.end - root.start) / 2; if (root.start <= index && index <= mid) { modify(root.left, index, value); } else if (mid < index && index <= root.end) { modify(root.right, index, value); } root.count = root.left.count + root.right.count; }
private SegTreeNode build(int start, int end) { if (end < start) { return null; } SegTreeNode root = new SegTreeNode(start, end, 0); int mid = start + (end - start) / 2; if (start != end) { root.left = build(start, mid); root.right = build(mid + 1, end); root.count = root.left.count + root.right.count; } else { root.count = 0; } return root; }