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] ...
随机推荐
- 创建 GPG 证书
一.什么是 GPG 以下引自维基百科: GNU Privacy Guard(GnuPG或GPG)是一种加密软件,它是PGP加密软件的满足GPL的替代物.GnuPG依照由IETF订定的OpenPGP技术 ...
- 少用 string.Format
如果你使用的是 C# 6.0 及其以上版本的话我建议你使用新增的 内插字符串 这个功能.这个功能可以更好的帮助开发人员设置字符串格式.下面我们就来看一下为什么要少用 string.Format 而要多 ...
- Linux系统发行版本及其区别
1 Linux系统组成 Linux操作系统=Linux内核+GNU软件及系统软件+必要的应用程序.下表为Linux系统各组成部分的贡献人员: Linux内核 GNU组件(gcc.bash) 其他必要应 ...
- ASP.NET Core 反向代理部署知多少
引言 最近在折腾统一认证中心,看到开源项目IdentityServer4.Admin集成了IdentityServer4和管理面板,就直接拿过来用了.在尝试Nginx部署时遇到了诸如虚拟目录映射,请求 ...
- linux入门系列16--文件共享之Samba和NFS
前一篇文章"linux入门系列15--文件传输之vsftp服务"讲解了文件传输,本篇继续讲解文件共享相关知识. 文件共享在生活和工作中非常常见,比如同一团队中不同成员需要共同维护同 ...
- 脚本化处理linux云服务器第二硬盘初始化
#!/usr/bin/bash # 可以带参数 method=$ size=$ mydir=$ [ $#== ]&&{ echo -e "Missing parameter! ...
- js案例之使用正则表达式进行验证数据正确性
#js案例之使用正则表达式进行验证数据正确性 代码上传至 "GitHub" 样例: <tr> <td>密码:</td> <td> & ...
- JZOJ 1154. 【GDOI2003】购物
1154. [GDOI2003]购物 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description GDOI商场推出优惠 ...
- 解决layui表单ajax提交回调函数不起作用问题的两种方式
最近想用layui开发一个论坛模板用的是fly-ui,才接触layui对其还不太熟悉.一个简单的登录就困扰了我很久.登录的form通过ajax提交回调函数老是不起作用.经过浪费了N多时间的调试,发现l ...
- Lambda 语法
1.java8 Lambda表达式语法简介 (此处需要使用jdk1.8或其以上版本) Lambd表达式分为左右两侧 * 左侧:Lambda 表达式的参数列表 * 右侧:Lambda 表达式中所需要执行 ...