zoukankan      html  css  js  c++  java
  • 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);
     */
    

      

  • 相关阅读:
    strcpy的实现
    使用Highcharts生成柱状图
    使用Highcharts结合PHP与Mysql生成饼状图
    shopnc 学习笔记
    php中关于mysqli和mysql区别的一些知识点分析
    jquery捕获enter键 按enter键执行提交
    php在IE浏览器中保存SESSION
    ShopNC 商城系统开发经验分享第五篇: 缓存设计
    ShopNC 商城系统开发经验分享第二篇:ShopNC商城系统初步分析
    SHOPEX于SHOP++比较哪款更适合做二次开发,为什么?
  • 原文地址:https://www.cnblogs.com/hyserendipity/p/11564145.html
Copyright © 2011-2022 走看看