首页 > 其他 > 详细

Range Module

时间:2019-09-21 19:11:44      阅读:109      评论:0      收藏:0      [点我收藏+]

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);
 */

  

 

Range Module

原文:https://www.cnblogs.com/hyserendipity/p/11564145.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!