【732. 我的日程安排表 III】线段树求解
class MyCalendarThree {
public static void main(String[] args) {
MyCalendarThree myCalendarThree = new MyCalendarThree();
System.out.println( myCalendarThree.book(10,20) );
System.out.println(myCalendarThree.book(50,60));
System.out.println(myCalendarThree.book(10,40));
System.out.println(myCalendarThree.book(5,15));
System.out.println(myCalendarThree.book(5,10));
System.out.println(myCalendarThree.book(25,55));
}
class Seg{
int cl,cr;
Seg left,right;
int cnt;
int max;
public Seg(int l,int r){
this.cl = l;
this.cr = r;
this.cnt = 0;
this.max = 0;
}
public void pushdown(){
int mid = (cl+cr)/2;
if( this.left == null){
this.left = new Seg(cl,mid);
}
if(this.right == null){
this.right = new Seg(mid+1,cr);
}
if( this.cnt == 0){
return;
}
this.left.max += this.cnt;
this.right.max += this.cnt;
this.left.cnt += this.cnt;
this.right.cnt += this.cnt;
this.cnt = 0;
}
public void add(int l,int r){
if(cl == l &&cr == r){
this.cnt++;
this.max++;
return;
}
pushdown();
int mid = (cl+cr)/2;
if( r<=mid){
this.left.add(l,r);
}else{
if(l>mid){
this.right.add(l,r);
}else{
this.left.add(l,mid);
this.right.add(mid+1,r);
}
}
//pushup 啥也不做
pushup();
}
public void pushup(){
this.max = Math.max(this.left.max,this.right.max);
}
public int search(int l,int r){
if(cl ==l &&cr == r){
return this.max;
}
pushdown();
int mid = (cl+cr)/2;
int ans ;
if(r<=mid){
ans = this.left.search(l,r);
}else{
if(l>mid){
ans = this.right.search(l,r);
}else{
int val1 = this.left.search(l,mid);
int val2 = this.right.search(mid+1,r);
ans = Math.max(val1,val2);
}
}
pushup();
return ans;
}
}
Seg seg;
int k ;
public MyCalendarThree() {
seg = new Seg(0,1000000000);
k = 0;
}
public int book(int start, int end) {
seg.add(start,end-1);
int val = seg.search(start,end-1);
if(val > k){
k = val;
}
return k;
}
}
/**
* Your MyCalendarThree object will be instantiated and called as such:
* MyCalendarThree obj = new MyCalendarThree();
* int param_1 = obj.book(start,end);
*/
【732. 我的日程安排表 III】线段树求解的更多相关文章
- Java实现 LeetCode 732 我的日程安排表 III(暴力 || 二叉树)
732. 我的日程安排表 III 实现一个 MyCalendar 类来存放你的日程安排,你可以一直添加新的日程安排. MyCalendar 有一个 book(int start, int end)方法 ...
- 【Codeforces717F】Heroes of Making Magic III 线段树 + 找规律
F. Heroes of Making Magic III time limit per test:3 seconds memory limit per test:256 megabytes inpu ...
- HDU 5266 pog loves szh III (线段树+在线LCA转RMQ)
题目地址:HDU 5266 这题用转RMQ求LCA的方法来做的很easy,仅仅须要找到l-r区间内的dfs序最大的和最小的就能够.那么用线段树或者RMQ维护一下区间最值就能够了.然后就是找dfs序最大 ...
- SPOJ GSS3 Can you answer these queries III ——线段树
[题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...
- HDU 5266 pog loves szh III 线段树,lca
Pog and Szh are playing games. Firstly Pog draw a tree on the paper. Here we define 1 as the root of ...
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- spoj 1557 GSS3 - Can you answer these queries III 线段树
题目链接 给出n个数, 2种操作, 一种是将第x个数改为y, 第二种是询问区间[x,y]内的最大连续子区间. 开4个数组, 一个是区间和, 一个是区间最大值, 一个是后缀的最大值, 一个是前缀的最大值 ...
- SP1716 GSS3 - Can you answer these queries III 线段树
问题描述 [LG-SP1716](https://www.luogu.org/problem/SP1716] 题解 GSS 系列的第三题,在第一题的基础上带单点修改. 第一题题解传送门 在第一题的基础 ...
- C#LeetCode刷题-线段树
线段树篇 # 题名 刷题 通过率 难度 218 天际线问题 32.7% 困难 307 区域和检索 - 数组可修改 42.3% 中等 315 计算右侧小于当前元素的个数 31.9% 困难 4 ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
随机推荐
- Linux反空闲的设置和关闭
有一定工作经验的运维人基本都会遇到这样的场景,某个窗口自动断开了,提示超时: [oracle@jystdrac1 ~]$ timed out waiting for input: auto-logou ...
- Oracle-报错信息显示问号或者乱码(Oracle 19c)
问题描述: 通过sqlplus登录Oracle19c时,执行SQL出现报错时,显示错误码出现问号: 解决方法: su - oracle vi .bash_profile 添加如下一行内容: ex ...
- 一份55页Java性能调优PPT分享
提起"肖桦"这个人,相信很多小伙伴对他比较陌生.除去现任唯品会资深技术专家头衔外,他更为技术圈所熟知的是他的著名开源项目:SpringSide. SpringSide是以sprin ...
- react 新旧生命周期有什么区别?新增了哪些钩子?废弃了哪些钩子?为什么废弃?
壹 ❀ 引 在日常面试中,若对于了解react的同学而言,多多少少会被问到生命周期相关的问题,比如大致阐述生命周期的运作流程,以及每个钩子函数大致的作用,而我在两位出去面试的同事那里了解到,他们都遇到 ...
- NVME(学习笔记三)—PMR
PMR(Persistent Memory Region)持久性内存区域 NVM Express在2019年完成了NVMe 1.4规范的制定,新的NVMe协议带来了大量的全新特性,尤其在纠错.强化性能 ...
- Python中Try/Except/else/final语句
python中的try/except/else/finally语句 与其他语言相同,在python中,try/except语句主要是用于处理程序正常执行过程中出现的一些异常情况,如语法错误(pyt ...
- npm无法安装node-sass 的问题
安装 node-sass 的问题呈现:4.9.0版本无法下载 Downloading binary from https://github.com/sass/node-sass/releases/do ...
- OneCloud记录
配置信息 S805, 1G RAM, 8G ROM, USB2.0 * 2, 1GB LAN, SD Cardreader S805参数: 32-bit, ARMv7-A, Cortex-A5, 1. ...
- oracle FGAC(细粒度访问控制)介绍
在ORACLE中,RLS有时也叫做虚拟私有数据库(VPD)或者细粒度访问控制(FGAC). RLS由8i引进,利用这一特性我们可以对表定义安全策略(并且指明对表的操作类型),实现对用户可以看到或者修改 ...
- 记一次 QT VS Tools 无法配置 version 的问题
问题概述: 在 QT VS Tools 拓展工具中添加多个 qt 版本的静态库时,发现输入完 Name 和 Path 之后点击 OK,新输入的 version 路径并没有保存成功 测试的 QT VS ...