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. TableViewComponent v2

    Unity UGUI 自带的 ScrollView 控件不支持复用滚动内容,在数量大的情况下,界面容易卡顿 借鉴其他游戏控件,写了个可复用的滚动组件,扩展.优化了ScrollView TableVie ...

  2. 当鼠标hover的时候,使用tip将overflow:hidden隐藏的文字显示完全

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. Oracle字段

    pl/sql查看当前数据库编码语句:select userenv('language') from dual; 测试结果:数据库字符集编码是utf-8 1.nvarchar2(size) ,size不 ...

  4. 网络编程之C10K

    网络编程之C10K 虽然在过去的十几年里C10K问题已经可以很好的解决,但学习网络编程时研究C10K问题仍然价值巨大,因为技术的发展都是有规律和线索可循的,了解C10K问题及其解决思路,通过举一反三, ...

  5. Haproxy 使用block 阻止域名访问到某个子目录报403

    配置教程如下: acl is_https_com hdr_beg(host) www.baidu.com #定义规则域名 acl api_block_url_web url_dir -i /web/ ...

  6. opencv +数字识别

    现在很多场景需要使用的数字识别,比如银行卡识别,以及车牌识别等,在AI领域有很多图像识别算法,大多是居于opencv 或者谷歌开源的tesseract 识别. 由于公司业务需要,需要开发一个客户端程序 ...

  7. 一篇带你看懂Flutter叠加组件Stack

    注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 Stack Stack组件可以将子组件叠加显示,根据子组件 ...

  8. Parcel上手——又一个打包工具

    Parcel是什么? 极速零配置Web应用打包工具 说到打包工具,大多人应该都用过Webpack,Parcel也是这一类工具. Parcel相比Webpack有什么优势? 配置简单 打包速度快 以下是 ...

  9. 《即时消息技术剖析与实战》学习笔记11——IM系统如何保证服务高可用:流量控制和熔断机制

    IM 系统的不可用主要有以下两个原因: 一是无法预测突发流量,即使进行了服务拆分.自动扩容,但流量增长过快时,服务已经不可用了: 二是业务中依赖的这些接口.资源不可用或变慢时,比如发消息可能需要依赖& ...

  10. [转帖]RSYNC 的核心算法

    RSYNC 的核心算法 https://coolshell.cn/articles/7425.html rsync是unix/linux下同步文件的一个高效算法,它能同步更新两处计算机的文件与目录,并 ...