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的更多相关文章

  1. [LeetCode] Range Module 范围模块

    A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the f ...

  2. [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 ...

  3. 715. Range Module

    A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the f ...

  4. [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  5. [LeetCode] Insert Interval 插入区间

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  6. [LeetCode] Merge Intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

  7. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  8. NodeJS-静态服务器

    静态服务器 代码 const http = require('http') const chalk = require('chalk') const conf = require('./config/ ...

  9. 合并区间 · Merge Intervals & 插入区间 · Insert Interval

    [抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...

随机推荐

  1. 再谈拍照,OPPO这次拿什么和iPhone7拼?

    ​一年一度的iPhone新机如期而至,双摄像头成为iPhone 7 Plus标配,尽管在这之前,双摄像头已有少数厂商在手机上装备,但苹果一出,市场必定全面跟进.无论各大厂商是否采用双摄像头,在手机拍照 ...

  2. 从2019-nCoV趋势预测问题,联想到关于网络安全态势预测问题的讨论

    0. 引言 在这篇文章中,笔者希望和大家讨论一个话题,即未来趋势是否可以被精确或概率性地预测. 对笔者所在的网络安全领域来说,由于网络攻击和网络入侵常常变现出随机性.非线性性的特征,因此纯粹的未来预测 ...

  3. C++扬帆远航——6(三色球)

    /* * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:sanseqiu.cpp * 作者:常轩 * 完成日期:2016 ...

  4. C++扬帆远航——14(求两个数的最大公约数)

    /* * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:gongyueshu.cpp * 作者:常轩 * 微信公众号:W ...

  5. AI:深度学习用于文本处理

    同本文一起发布的另外一篇文章中,提到了 BlueDot 公司,这个公司致力于利用人工智能保护全球人民免受传染病的侵害,在本次疫情还没有引起强烈关注时,就提前一周发出预警,一周的时间,多么宝贵! 他们的 ...

  6. py基础之无序列表

    '''dic是一个可以将两个相关变量关联起来的集合,格式是dd={key1:value1,key2:value2,key3:value3}'''d = { 'adam':95, 'lisa':85, ...

  7. Immer.js简析

    开始 在函数式编程中,Immutable这个特性是相当重要的,但是在Javascript中很明显是没办法从语言层面提供支持,但是还有其他库(例如:Immutable.js)可以提供给开发者用上这样的特 ...

  8. 必备技能三、render渲染函数

    Vue 推荐使用在绝大多数情况下使用 template 来创建你的 HTML.然而在一些场景中,你真的需要 JavaScript 的完全编程的能力,这就是 render 函数,它比 template ...

  9. js中(event)事件对象

    事件对象 • 什么是事件对象? • 就是当你触发了一个事件以后,对该事件的一些描述信息 • 例如: ° 你触发一个点击事件的时候,你点在哪个位置了,坐标是多少 ° 你触发一个键盘事件的时候,你按的是哪 ...

  10. Eureka停更了?试试Zookpper和Consul

    在Spring Cloud Netflix中使用Eureak作为注册中心,但是Eureka2.0停止更新,Eureka1.0 进入了维护状态.就像win7一样,同样可以用,但是官方对于新出现的问题并不 ...