@@ -0,0 +1,2014 @@
+package com.janosgyerik.utils.range;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Iterator;%0Aimport java.util.List;%0Aimport java.util.TreeSet;%0A%0A// Validated with https://leetcode.com/problems/range-module/description/%0Apublic class RangeModule %7B%0A%0A    private final TreeSet%3CRange%3E ranges = new TreeSet%3C%3E();%0A%0A    public void add(int left0, int right0) %7B%0A        int left = left0;%0A        int right = right0;%0A%0A        Iterator%3CRange%3E it = ranges.tailSet(new Range(0, left - 1)).iterator();%0A%0A        while (it.hasNext()) %7B%0A            Range range = it.next();%0A            if (right %3C range.left) %7B%0A                break;%0A            %7D%0A            left = Math.min(left, range.left);%0A            right = Math.max(right, range.right);%0A            it.remove();%0A        %7D%0A%0A        ranges.add(new Range(left, right));%0A    %7D%0A%0A    public void remove(int left, int right) %7B%0A        Iterator%3CRange%3E it = ranges.tailSet(new Range(0, left)).iterator();%0A        List%3CRange%3E parts = new ArrayList%3C%3E();%0A%0A        while (it.hasNext()) %7B%0A            Range range = it.next();%0A            if (right %3C range.left) %7B%0A                break;%0A            %7D%0A            if (range.left %3C left) %7B%0A                parts.add(new Range(range.left, left));%0A            %7D%0A            if (right %3C range.right) %7B%0A                parts.add(new Range(right, range.right));%0A            %7D%0A            it.remove();%0A        %7D%0A%0A        ranges.addAll(parts);%0A    %7D%0A%0A    public boolean contains(int left, int right) %7B%0A        Range range = ranges.higher(new Range(0, left));%0A        return range != null && range.left %3C= left && right %3C= range.right;%0A    %7D%0A%0A    private static class Range implements Comparable%3CRange%3E %7B%0A        final int left;%0A        final int right;%0A%0A        Range(int left, int right) %7B%0A            this.left = left;%0A            this.right = right;%0A        %7D%0A%0A        @Override%0A        public int compareTo(Range other) %7B%0A            if (right == other.right) return left - other.left;%0A            return right - other.right;%0A        %7D%0A    %7D%0A%7D%0A

Add RangeModule utility