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] ...
随机推荐
- Linux IO多路复用
监听文件描述符的状态来进行相应的读写操作,3个函数: 123 selectpollepoll 123456789 int (int nfds, fd_set *readfds, fd_set *wri ...
- php获取远程图片并把它保存到本地
/* *功能:php多种方式完美实现下载远程图片保存到本地 *参数:文件url,保存文件名称,使用的下载方式 *当保存文件名称为空时则使用远程文件原来的名称 */ function getImage( ...
- On Fixed-Point Implementation of Log-MPA for SCMA Signals
目录 论文来源 摘要 基本概念 1.SCMA 2.SCMA编码器 研究内容 1.基于Log-MPA的SCMA解码器实现过程 论文创新点 借鉴之处 论文来源 本论文来自于IEEE WIRELESS CO ...
- 【Art】抗疫路上,温暖相伴
2020年3月. 本应是春暖花开的时节,武汉却是寒冷的,整个中国也是寒冷的. 疫情将人们逼得退无可退,只能待在家里看着电视新闻与手机上一个个数字不断跳动,等待着它们背后前线的无数命悬一线的战士的胜利讯 ...
- Jira字段配置最佳实践
在我们创建Jira时,Jira上会填写各式各样的字段,不同的字段对于不同的角色人员,使用方式也是不同的,通过这篇文章,希望大家能够对Jira使用有更深刻的认识. 为什么需要严格规范? 易于开发,测试, ...
- Mybatis-Plus代码生成器使用详解
首先创建springboot的项目(创建步骤省略) 创建好项目后要配置maven依赖,附上pom.xml: <?xml version="1.0" encoding=&quo ...
- Python中的BeautifulSoup库简要总结
一.基本元素 BeautifulSoup库是解析.遍历.维护“标签树”的功能库. 引用 from bs4 import BeautifulSoup import bs4 html文档-标签树-Beau ...
- 前端模板引擎doT.js的使用
前言 我们在做前端开发时,经常需要根据后台返回的json数据动态生成html并插入到页面中显示.最简单的方法就是通过jQuery去遍历数据拼接html,如以下: <script> var ...
- 神奇的background——绘制图形
相信大家在平时工作中少不了会被要求在某些元添加一些特殊的背景图片,这时候通常就拿起ps就是切切切.不说这种方式麻烦,有ui给你切好的情况已经不错,没有的就有自己动手.还可能有需要切一整张超大图的情况. ...
- IDEA Messages Build总是自动弹出提示错误
IDEA,总是在代码未完成时,在进行切换页面回来后会跳出Messages Build,我相信大家在写java web项目的时候,经常会遇到这个问题,接下来我就和大家说一下问题所在. 主要原因是因为我们 ...