• Range Module


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

      

  • 相关阅读:
    Roce ofed 环境搭建与测试
    Ubuntu 1804 搭建NFS服务器
    Redhat 8.0.0 安装与网络配置
    Centos 8.1 安装与网络配置
    SUSE 15.1 系统安装
    VSpare ESXi 7.0 基本使用(模板、iso、SRIOV)
    VSpare ESXi 7.0 服务器安装
    open SUSE leap 15.1 安装图解
    KVM虚拟机网卡连接网桥
    GitHub Action一键部署配置,值得拥有
  • 原文地址:https://www.cnblogs.com/hyserendipity/p/11564145.html
Copyright © 2020-2023  润新知