2019-09-21 18:54:16

  • 715. Range Module

问题描述:

 

问题求解:

用线段树解决了。

class RangeModule {
    Node root;

    class Node {
        int l;
        int r;
        int m;
        Node left;
        Node right;
        boolean tracked;

        public Node(int l, int r, int m, boolean tracked) {
            this.l = l;
            this.r = r;
            this.m = m;
            this.left = null;
            this.right = null;
            this.tracked = tracked;
        }
    }

    public RangeModule() {
        root = new Node(0, (int)1e9, -1, false);
    }

    public void addRange(int left, int right) {
        root = addRange(left, right, root);
    }

    private Node addRange(int l, int r, Node root) {
        if (root.m == -1) {
            if (root.tracked) return root;
            if (root.l == l && root.r == r) root.tracked = true;
            else if (root.l == l) {
                root.m = r;
                root.left = new Node(l, r, -1, root.tracked);
                root.right = new Node(r, root.r, -1, root.tracked);
                root.left = addRange(l, r, root.left);
            }
            else {
                root.m = l;
                root.left = new Node(root.l, l, -1, root.tracked);
                root.right = new Node(l, root.r, -1, root.tracked);
                root.right = addRange(l, r, root.right);
            }
        }
        else {
            if (r <= root.m) {
                root.left = addRange(l, r, root.left);
            }
            else if (l >= root.m) {
                root.right = addRange(l, r, root.right);
            }
            else {
                root.left = addRange(l, root.m, root.left);
                root.right = addRange(root.m, r, root.right);
            }
        }
        return root;
    }

    public boolean queryRange(int left, int right) {
        return queryRange(left, right, root);
    }

    private boolean queryRange(int l, int r, Node root) {
        if (root.m == -1) {
            return root.tracked;
        }
        else {
            if (r <= root.m) return queryRange(l, r, root.left);
            else if (l >= root.m) return queryRange(l, r, root.right);
            else return queryRange(l, root.m, root.left) && queryRange(root.m, r, root.right);
        }
    }

    public void removeRange(int left, int right) {
        root = removeRange(left, right, root);
    }

    private Node removeRange(int l, int r, Node root) {
        if (root.m == -1) {
            if (!root.tracked) return root;
            if (root.l == l && root.r == r) {
                root.tracked = false;
            }
            else if (root.l == l) {
                root.m = r;
                root.left = new Node(l, root.m, -1, root.tracked);
                root.right = new Node(root.m, root.r, -1, root.tracked);
                root.left =removeRange(l, r, root.left);
            }
            else {
                root.m = l;
                root.left = new Node(root.l, root.m, -1, root.tracked);
                root.right = new Node(root.m, root.r, -1, root.tracked);
                root.right = removeRange(l, r, root.right);
            }
        }
        else {
            if (r <= root.m) root.left = removeRange(l, r, root.left);
            else if (l >= root.m) root.right = removeRange(l, r, root.right);
            else {
                root.left = removeRange(l, root.m, root.left);
                root.right = removeRange(root.m, r, root.right);
            }
        }
        return root;
    }
}

/**
 * Your RangeModule object will be instantiated and called as such:
 * RangeModule obj = new RangeModule();
 * obj.addRange(left,right);
 * boolean param_2 = obj.queryRange(left,right);
 * obj.removeRange(left,right);
 */

  

02-12 15:16