当前位置:网站首页>【英雄哥六月集训】第 24天: 线段树
【英雄哥六月集训】第 24天: 线段树
2022-07-27 10:01:00 【如果我是温帅帅】
系列文章
线段树
线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间对应线段树中的一个叶结点。
使用线段树可以快速的查找某一个节点在若干条线段中出现的次数,时间复杂度为O(logN)。而未优化的空间复杂度为2N,实际应用时一般还要开4N的数组以免越界,因此有时需要离散化让空间压缩。
一、 我的日程安排表 II
class MyCalendarTwo {
TreeMap<Integer,Integer> delta;
public MyCalendarTwo() {
delta = new TreeMap();
}
public boolean book(int start, int end) {
delta.put(start,delta.getOrDefault(start,0)+1);
delta.put(end, delta.getOrDefault(end, 0)-1);
int active = 0;
for( int d:delta.values()){
active +=d;
if(active >=3){
delta.put(start, delta.get(start)-1);
delta.put(end, delta.get(end)+1);
if(delta.get(start)==0)
delta.remove(start);
return false;
}
}
return true;
}
}
/** * Your MyCalendarTwo object will be instantiated and called as such: * MyCalendarTwo obj = new MyCalendarTwo(); * boolean param_1 = obj.book(start,end); */
二、 掉落的方块
class Solution {
class Node{
int ls,rs,val,add;
}
int N = (int)1e9,cnt=0;
Node[] tr= new Node[1000010];
void update(int u,int lc,int rc,int l,int r,int v){
if( l<= lc && rc<= r){
tr[u].val=v;
tr[u].add=v;
return ;
}
pushdown(u);
int mid= lc+rc >>1;
if(l<= mid) update(tr[u].ls,lc,mid,l,r,v);
if(r>mid) update(tr[u].rs,mid+1,rc,l,r,v);
pushup(u);
}
int query(int u,int lc,int rc,int l,int r){
if(l<=lc && rc<=r) return tr[u].val;
pushdown(u);
int mid = lc+rc >>1,ans=0;
if(l<=mid) ans = query(tr[u].ls,lc,mid,l,r);
if(r>mid) ans = Math.max(ans, query(tr[u].rs,mid+1,rc,l,r));
return ans;
}
void pushdown(int u){
if(tr[u]==null) tr[u]=new Node();
if(tr[u].ls ==0){
tr[u].ls=++cnt;
tr[tr[u].ls]=new Node();
}
if(tr[u].rs ==0){
tr[u].rs=++cnt;
tr[tr[u].rs]=new Node();
}
if(tr[u].add == 0) return ;
int add = tr[u].add;
tr[tr[u].ls].add = add;
tr[tr[u].rs].add = add;
tr[tr[u].ls].val = add;
tr[tr[u].rs].val = add;
tr[u].add=0;
}
void pushup(int u){
tr[u].val=Math.max(tr[tr[u].ls].val,tr[tr[u].rs].val);
}
public List<Integer> fallingSquares(int[][] positions) {
List<Integer> ans = new ArrayList<>();
tr[1] = new Node();
for(int[] info:positions){
int x = info[0], h = info[1], cur = query(1,1,N,x,x+h-1);
update(1,1,N,x,x+h-1,cur+h);
ans.add(tr[1].val);
}
return ans;
}
}
总结
边栏推荐
猜你喜欢

Sub query of database performance series

Leetcode.1260. 2D grid migration____ In situ violence / dimensionality reduction + direct positioning of circular array

直播倒计时 3 天|SOFAChannel#29 基于 P2P 的文件和镜像加速系统 Dragonfly

并发之线程状态转换

WGAN、WGAN-GP、BigGAN

Girl fan wants to find a boyfriend, but it's for

Shell process control (emphasis), if judgment, case statement, let usage, for ((initial value; loop control condition; variable change)) and for variable in value 1 value 2 value 3..., while loop

Shell operator, $((expression)) "or" $[expression], expr method, condition judgment, test condition, [condition], comparison between two integers, judgment according to file permission, judgment accor

PCL各模块概述(1.6)

历时一年,论文终于被国际顶会接收了
随机推荐
Anaconda安装(非常详细)
NFT system development - Tutorial
邮件服务器
Text processing tool in shell, cut [option parameter] filename Description: the default separator is the built-in variable of tab, awk [option parameter] '/pattern1/{action1}filename and awk
活体检测综述
Anchor Free检测器:CenterNet
【精选】如何完美的写 PHP 代码的呢?
Pyautogui realizes automatic office -rpa small case
Shell变量、系统预定义变量$HOME、$PWD、$SHELL、$USER、自定义变量、特殊变量$n、$#、$*、[email protected]、$?、env看所有的全局变量值、set看所有变量
Snowflake vs. Databricks谁更胜一筹?2022年最新战报
Shell的read 读取控制台输入、read的使用
并发之park与unpark说明
Live countdown 3 days sofachannel 29 P2P based file and image acceleration system Dragonfly
Understanding and code implementation of Se (sequence and exception) module
There is no CUDA option in vs2019+cuda11.1 new project
Matlab-绘制日期和持续时间图
Shell运算符、$((运算式))” 或 “$[运算式]、expr方法、条件判断、test condition、[ condition ]、两个整数之间比较、按照文件权限进行判断、按照文件类型进行判断
Pytorch installation (very detailed)
线代003
Example of ICP registration for PCL