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】线段树求解的更多相关文章

  1. Java实现 LeetCode 732 我的日程安排表 III(暴力 || 二叉树)

    732. 我的日程安排表 III 实现一个 MyCalendar 类来存放你的日程安排,你可以一直添加新的日程安排. MyCalendar 有一个 book(int start, int end)方法 ...

  2. 【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 ...

  3. HDU 5266 pog loves szh III (线段树+在线LCA转RMQ)

    题目地址:HDU 5266 这题用转RMQ求LCA的方法来做的很easy,仅仅须要找到l-r区间内的dfs序最大的和最小的就能够.那么用线段树或者RMQ维护一下区间最值就能够了.然后就是找dfs序最大 ...

  4. SPOJ GSS3 Can you answer these queries III ——线段树

    [题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...

  5. 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 ...

  6. 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 ...

  7. spoj 1557 GSS3 - Can you answer these queries III 线段树

    题目链接 给出n个数, 2种操作, 一种是将第x个数改为y, 第二种是询问区间[x,y]内的最大连续子区间. 开4个数组, 一个是区间和, 一个是区间最大值, 一个是后缀的最大值, 一个是前缀的最大值 ...

  8. SP1716 GSS3 - Can you answer these queries III 线段树

    问题描述 [LG-SP1716](https://www.luogu.org/problem/SP1716] 题解 GSS 系列的第三题,在第一题的基础上带单点修改. 第一题题解传送门 在第一题的基础 ...

  9. C#LeetCode刷题-线段树

    线段树篇 # 题名 刷题 通过率 难度 218 天际线问题   32.7% 困难 307 区域和检索 - 数组可修改   42.3% 中等 315 计算右侧小于当前元素的个数   31.9% 困难 4 ...

  10. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

随机推荐

  1. CF1916

    重点在 E B 如果 \(lcm(a,b)\neq b\),则是一个答案. 否则答案是 \(b\times\frac{b}{a}\). C 先前缀和.设前缀和 \(s_i\).考虑 \(s_i\) 减 ...

  2. 什么是TDD(一)

    引子 回顾 虽然我很早以前就听说单元测试,也曾经多次在项目中引入单元测试框架和单元测试的实践为代码质量的提升带来了一丝助力. 但这种方式更多的是从软件调试的角度出发,即将单元测试作为一种测试方法可用性 ...

  3. 2023年多校联训NOIP层测试7+【LGR-149-Div.3】洛谷基础赛 #2 & qw Round -1

    普及模拟3 \(T1\) 最大生成树 \(100pts\) 简化题意:给定一个 \(n(1 \le n \le 1 \times 10^5)\) 个点的完全图,给定各点的点权 \(a_i(1 \le ...

  4. Vue DevTools 安装和浏览器跳转到编辑器指定组件

    Vue DevTools install and Open component in editor 1.在谷歌浏览器安装vue 插件 1.1下载vue插件 链接:https://pan.baidu.c ...

  5. v-html可能导致的问题

    v-html可能导致的问题 Vue中的v-html指令用以更新元素的innerHTML,其内容按普通HTML插入,不会作为Vue模板进行编译,如果试图使用v-html组合模板,可以重新考虑是否通过使用 ...

  6. MySQL日志:slow query log

    ySQL的慢查询日志可以用来找出执行时间过长的查询语句,并进行针对性的优化.   一.slow log相关参数 以下参数都是动态参数,可以在实例运行时修改. slow_query_log=1 #是否启 ...

  7. Java中的Try with Resources语句介绍

    1.介绍 从Java7诞生了try-with-resources,这家伙可以在资源使用完后实现自动关闭回收.想想我们之前打开一个文件或流对象用完咋整的,是不是finally语句块中手动close的. ...

  8. Python之正则表达式匹配电话号码和邮箱

    代码 #! python3 # phoneAndEmail.py - Finds phone numbers and email addresses on Clipboard import pyper ...

  9. 记一次 .NET某列控连锁系统 崩溃分析

    一:背景 1. 讲故事 过年喝了不少酒,脑子不灵光了,停了将近一个月没写博客,今天就当新年开工写一篇吧. 去年年初有位朋友找到我,说他们的系统会偶发性崩溃,在网上也发了不少帖子求助,没找到自己满意的答 ...

  10. Docker实践之08-使用网络

    目录 一.外部访问容器 启动容器时指定参数-P(大写P) 启动容器时指定参数-p(小写p) 二.容器互联 使用--link参数使容器互联 将容器加入自定义网络实现互联 三.为容器配置DNS 一.外部访 ...