当前位置:网站首页>[brother hero June training] day 24: line segment tree
[brother hero June training] day 24: line segment tree
2022-07-27 10:21:00 【If I were Wen Shuai】
Series articles
【 Brother hero training in June 】 The first 01 God : Array
【 Brother hero training in June 】 The first 02 God : character string
【 Brother hero training in June 】 The first 03 God : Sort
【 Brother hero training in June 】 The first 04 God : greedy
【 Brother hero training in June 】 The first 05 God : Double pointer
【 Brother hero training in June 】 The first 06 God : The sliding window
【 Brother hero training in June 】 The first 07 God : Hash
【 Brother hero training in June 】 The first 08 God : The prefix and
【 Brother hero training in June 】 The first 09 God : Two points search
【 Brother hero training in June 】 The first 10 God : An operation
【 Brother hero training in June 】 The first 11 God : matrix
【 Brother hero training in June 】 The first 12 God : Linked list
【 Brother hero training in June 】 The first 13 God : Double linked list
【 Brother hero training in June 】 The first 14 God : Stack
【 Brother hero training in June 】 The first 15 God : Binary tree
【 Brother hero training in June 】 The first 16 God : queue
【 Brother hero training in June 】 The first 17 God : Breadth first search
【 Brother hero training in June 】 The first 18 God : Trees
【 Brother hero training in June 】 The first 19 God : Binary tree
【 Brother hero training in June 】 The first 20 God : Binary search tree
【 Brother hero training in June 】 The first 21 God : Pile up ( Priority queue )
【 Brother hero training in June 】 The first 22 God : Ordered set
【 Brother hero training in June 】 The first 23 God : Dictionary tree
【 Brother hero training in June 】 The first 24 God : Line segment tree
List of articles
Line segment tree
Line segment tree is a binary search tree , Similar to interval trees , It divides an interval into some cell intervals , Each cell interval corresponds to a leaf node in the line segment tree .
Using line segment tree can quickly find the number of times a node appears in several line segments , The time complexity is O(logN). The unoptimized space complexity is 2N, In practical application, it usually needs to be opened 4N To avoid crossing the bounds , So sometimes discretization is needed to compress space .
One 、 My schedule 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); */
Two 、 Falling diamonds
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;
}
}
summary
边栏推荐
- Matlab-创建 MATLAB的logo
- About new_ Online_ Judge_ 1081_ Thoughts on Goldbach's conjecture
- NFT system development - Tutorial
- 线代003
- Shell integrated application cases, archiving files, sending messages
- Configuration of pytorch deep learning environment based on cuda10.0
- Is Damon partgroupdef a custom object?
- 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
- Oracle 11g manual memory management
- 3D人脸重建:Joint 3D Face Reconstruction and Dense Alignment with position Map Regression Network
猜你喜欢

Metaaploit-后渗透技知识

Oracle view hard parsing
![Shell function, system function, basename [string / pathname] [suffix] can be understood as taking the file name in the path, dirname file absolute path, and user-defined function](/img/3d/d7276d2010f1d77a3bd572cc66eced.png)
Shell function, system function, basename [string / pathname] [suffix] can be understood as taking the file name in the path, dirname file absolute path, and user-defined function

Vs2019 Community Edition Download tutorial (detailed)

Program translation and execution, from editing, preprocessing, compilation, assembly, linking to execution

Oracle resizing data files

Based on LSM tree idea Net 6.0 C # write a kV database (case version)

怎样关闭电脑开机自启动的应用

Dcgan paper improvements + simplified code

卸载CUDA11.1
随机推荐
解决ORCLE-ORA-01122 01110 01210
使用 LSM-Tree 思想基于.NET 6.0 C# 写个 KV 数据库(案例版)
Based on LSM tree idea Net 6.0 C # write a kV database (case version)
Snowflake vs. databricks who is better? The latest war report in 2022
StyleGAN论文笔记+修改代码尝试3D点云生成
【英雄哥六月集训】第 25天: 树状数组
数据库性能系列之子查询
Reason for pilot importerror: cannot import name 'pilot_ Version 'from' PIL ', how to install pilot < 7.0.0
gyp ERR! configure error. gyp ERR! stack Error: gyp failed with exit code: 1
语音识别的一些开源项目整理
pillow的原因ImportError: cannot import name ‘PILLOW_VERSION‘ from ‘PIL‘,如何安装pillow<7.0.0
超赞的卡尔曼滤波详解文章
Practice and exploration of overseas site Seata of ant group
window平台下本地连接远程服务器数据库(一)
Pytorch installation (very detailed)
WGAN、WGAN-GP、BigGAN
Decision tree principle and case application - Titanic survival prediction
Matlab-创建文字云
女粉想要找男朋友,竟是为了...
Switch port mirroring Configuration Guide