A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the following interfaces in an efficient manner.

  • addRange(int left, int right) Adds the half-open interval [left, right), tracking every real number in that interval. Adding an interval that partially overlaps with currently tracked numbers should add any numbers in the interval [left, right) that are not already tracked.
  • queryRange(int left, int right) Returns true if and only if every real number in the interval [left, right) is currently being tracked.
  • removeRange(int left, int right) Stops tracking every real number currently being tracked in the interval [left, right).

Example 1:

addRange(10, 20): null
removeRange(14, 16): null
queryRange(10, 14): true (Every number in [10, 14) is being tracked)
queryRange(13, 15): false (Numbers like 14, 14.03, 14.17 in [13, 15) are not being tracked)
queryRange(16, 17): true (The number 16 in [16, 17) is still being tracked, despite the remove operation)

Note:

  • A half open interval [left, right) denotes all real numbers left <= x < right.
  • 0 < left < right < 10^9 in all calls to addRange, queryRange, removeRange.
  • The total number of calls to addRange in a single test case is at most 1000.
  • The total number of calls to queryRange in a single test case is at most 5000.
  • The total number of calls to removeRange in a single test case is at most 1000.
 

Approach #1: C++. [Using Vector]

class RangeModule {
public:
RangeModule() { } void addRange(int left, int right) {
vector<pair<int, int>> new_ranges;
bool inserted = false; for (const auto& it : ranges_) {
if (it.first > right && !inserted) {
new_ranges.emplace_back(left, right);
inserted = true;
}
if (it.first > right || it.second < left) {
new_ranges.push_back(it);
} else {
left = min(left, it.first);
right = max(right, it.second);
}
}
if (!inserted) new_ranges.emplace_back(left, right);
ranges_.swap(new_ranges);
} bool queryRange(int left, int right) {
int l = 0;
int r = ranges_.size() - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (ranges_[mid].first <= left && ranges_[mid].second >= right)
return true;
else if (ranges_[mid].first > right) {
r = mid - 1;
} else {
l = mid + 1;
}
}
return false;
} void removeRange(int left, int right) {
vector<pair<int, int>> new_ranges;
for (const auto& it : ranges_) {
if (it.second <= left || it.first >= right) {
new_ranges.emplace_back(it);
} else {
if (it.first < left)
new_ranges.emplace_back(it.first, left);
if (it.second > right)
new_ranges.emplace_back(right, it.second);
}
}
ranges_.swap(new_ranges);
} private:
vector<pair<int, int>> ranges_;
}; /**
* Your RangeModule object will be instantiated and called as such:
* RangeModule obj = new RangeModule();
* obj.addRange(left,right);
* bool param_2 = obj.queryRange(left,right);
* obj.removeRange(left,right);
*/

  

there are some notes about STL.

1. the difference between emplace_back and push_back.

2. emplace vs insert.

3. the method of swap in vector.

Approach #2: C++. [map]

class RangeModule {
public:
RangeModule() { } void addRange(int left, int right) {
IT l, r;
getOverLapRanges(left, right, l, r); if (l != r) {
auto last = r; last--;
left = min(left, l->first);
right = max(right, last->second);
ranges_.erase(l, r);
}
ranges_[left] = right;
} bool queryRange(int left, int right) {
IT l, r;
getOverLapRanges(left, right, l, r);
if (l == r) return false;
return l->first <= left && l->second >= right;
} void removeRange(int left, int right) {
IT l, r;
getOverLapRanges(left, right, l, r);
if (l == r) return ;
auto last = r; last--;
int start = min(left, l->first);
int end = max(right, last->second);
ranges_.erase(l, r);
if (start < left) ranges_[start] = left;
if (end > right) ranges_[right] = end;
} private:
typedef map<int, int>::iterator IT;
map<int, int> ranges_;
void getOverLapRanges(int left, int right, IT& l, IT& r) {
l = ranges_.upper_bound(left);
r = ranges_.upper_bound(right); // judge the left is the leftmost interval?
if (l != ranges_.begin()) {
if ((--l)->second < left) l++;
}
}
}; /**
* Your RangeModule object will be instantiated and called as such:
* RangeModule obj = new RangeModule();
* obj.addRange(left,right);
* bool param_2 = obj.queryRange(left,right);
* obj.removeRange(left,right);
*/

  

Notes:

c++::map.erase().

715. Range Module的更多相关文章

  1. Range Module

    2019-09-21 18:54:16 715. Range Module 问题描述: 问题求解: 用线段树解决了. class RangeModule { Node root; class Node ...

  2. [LeetCode] Range Module 范围模块

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

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

  4. Java实现 LeetCode 715 Range 模块(选范围)

    715. Range 模块 Range 模块是跟踪数字范围的模块.你的任务是以一种有效的方式设计和实现以下接口. addRange(int left, int right) 添加半开区间 [left, ...

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

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

  6. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  7. leetcode 学习心得 (4)

    645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...

  8. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  9. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

随机推荐

  1. 关于调用notifyDataSetChanged刷新PullToRefreshListView列表无反应解决办法

    文章转载自:关于调用notifyDataSetChanged刷新PullToRefreshListView列表无反应解决办法 | TeachCourse

  2. [证书服务器 第二篇] 基于OpenSSL 在 CentOS6 系统上 搭建自签证书服务,并应用于Web容器

    第一部分:概述 .. 第二部分:环境准备 1 操作系统 CentOS 6.x 2 安装openssl yum install -y openssl 3 安装jdk 从官网下载JDK http://ww ...

  3. 使用了Tomcat JDBC连接池不能重连的问题

    在项目中用到了tomcat 的jdbc连接池,发现一个问题是,当数据库重启时,服务没有重新的去连接数据库,需要将部署的项目重新启动才能连接到数据库.经过测试对配置做一下修改: 在配置dataSourc ...

  4. HAOI 2017 游记

    省选 2017年4月23日 流水账式游记,不喜勿喷. Day0: 准备出发,上午敲了一顿板子,板子敲完了就打小游戏,老师也不管了. 过程中各种奶,说什么今年一定考仙人掌啦,今年一定考字符串啦,今年一定 ...

  5. matlab添加当前文件夹到函数搜索目录

    pwd表示当前路径p = genpath(folderName) 返回一个包含路径名称的字符向量,该路径名称中包含 folderName 以及 folderName 下的多级子文件夹. addpath ...

  6. malloc和new的区别是什么?

    http://zhidao.baidu.com/link?url=iUDUZeJtj1o12PvUETLlJgvAMqzky5HxGCJRGnULpsO8HdWAdjKkQqGCJ9-o-aTu8NP ...

  7. 通过在classpath自动扫描方式把组件纳入spring容器中管理。

    前面的例子我们都是使用xml的bean定义来配置组件,如果组件过多很臃肿.spring2.5引入了组件自动扫描机制,在指定目录下查找标注了@Component.@Service.@Controller ...

  8. bzoj 3685

    线段树 方法一: 值域线段树,递归去写的,每次节点存出现次数. 对于几个操作, 1,2 直接加减就好 ; 3,4 操作贪心往某一个方向找 .7也很简单,主要说前驱后继怎么找.我是先找这个数第几小,根据 ...

  9. C++中的宏和const

    在C语言中使用const来定义一个变量,可以通过变量类型的指针形式来进行修改,而C++中增强了这种表现形式,使得即使通过类型变量指针也不能对变量进行修改. 在C++中const和宏是有区别的. con ...

  10. 遁入NOIP记

    回归noip啦 给自己定个小目标 500分起步 在这里列一下需要搞的东西OvO 1.算法基础 模拟 贪心 二分 分治 2.搜索 / 记忆化搜索 剪枝 对抗搜索 3.dp 状压 组合数学 树D 单队 D ...