public SegmentTree build(int start, int end) { if (start > end) { return null; } SegmentTree root = new SegmentTree(start, end); if (start == end) { return root; } int mid = start + ((end - start) >> 1); root.left = build(start, mid); root.right = build(mid + 1, end); return root; }
public void update(SegmentTree root, int p) { if (root == null) { return; } if (root.start == p && root.end == p) { root.count++; return; } int mid = root.start + ((root.end - root.start) >> 1); if (mid >= p) { update(root.left, p); } else { update(root.right, p); } root.count = 0; if (root.left != null) { root.count += root.left.count; } if (root.right != null) { root.count += root.right.count; } }