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);
 */
Range Module的更多相关文章
- [LeetCode] Range Module 范围模块
		A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the f ... 
- [Swift]LeetCode715. Range 模块 | Range Module
		A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the f ... 
- 715. Range Module
		A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the f ... 
- [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流
		Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ... 
- [LeetCode] Insert Interval 插入区间
		Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ... 
- [LeetCode] Merge Intervals 合并区间
		Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ... 
- Swift LeetCode 目录 | Catalog
		请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ... 
- NodeJS-静态服务器
		静态服务器 代码 const http = require('http') const chalk = require('chalk') const conf = require('./config/ ... 
- 合并区间 · Merge Intervals & 插入区间 · Insert Interval
		[抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ... 
随机推荐
- 服务治理与RPC · 跬步
			以前写过Django中使用zerorpc的方法,但是由于我们的Django是运行在gevent下,而zeromq需要启动一个后台进程处理消息,与gevent使用的greenlet携程是冲突的. 在Ja ... 
- Vimium - 让你体验Geek般的浏览体验
			相信很多电脑高手们都会寻找一一些快捷高效的操作方式,如经常利用键盘的快速操作,让你脱离鼠标,可以让你不用花太多精力地去移动细小的指针进行操作,使得工作的效率提高许多. 不过,实际上很多时候我们还是不得 ... 
- bzoj1603: [Usaco2008 Oct]打谷机 (纱布题)
			Description Input Output Sample Input Sample Output Time Limit: 5 Sec Memory Limit: 64 MB Submit: 7 ... 
- NVARCHAR(MAX) 的最大长度
			本文使用的环境是SQL Server 2017, 主机是64位操作系统. 大家都知道,Micorosoft Docs对 max参数的定义是:max 指定最大的存储空间是2GB,该注释是不严谨的: nv ... 
- CentOS7 安装python 3.5 及 pip安装
			1.CentOS7 安装Python 的依赖包 # yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-d ... 
- ITT Corporation之“中国战略”
			前言:众所周知,中国已经成为全世界第二大经济体,并且坐拥14亿人口的庞大市场,蕴藏着巨大的市场机遇,海外高科技企业想法获得长足的发展重视和开拓中国市场成为重中之重,诸如特斯拉,google,苹果等,近 ... 
- java编写非对称加密,解密,公钥加密,私钥解密,RSA,rsa
			非对称加密已经被评为加密标准,主要包含(公钥加密私钥解密,或者私钥加密公钥解密)本文主要讲解的是如何用java生成 公钥和私钥并且 进行字符串加密 和字符串解密 //如需要代码copy如下 im ... 
- linux-TFTP服务
			1.TFTP协议简介TFTP,全称是 Trivial File Transfer Protocol(简单文件传输协议),基于 UDP 实现,该协议简单到只能从远程服务器读取数据或向远程服务器上传数据. ... 
- C语言程序设计(十) 字符串
			第十章 字符串 字符串常量是由一对双引号括起来的一个字符串序列 字符串实际就是由若干个有效数字构成且以字符'\0'作为结束的一个字符序列 C语言没有提供字符串数据类型,因此字符串的存取要用字符型数组来 ... 
- python读取文件指定行内容
			python读取文件指定行内容 import linecache text=linecache.getline(r'C:\Users\Administrator\Desktop\SourceCodeo ... 
