当前位置:网站首页>[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
边栏推荐
- Shell变量、系统预定义变量$HOME、$PWD、$SHELL、$USER、自定义变量、特殊变量$n、$#、$*、[email protected]、$?、env看所有的全局变量值、set看所有变量
- 【英雄哥六月集训】第 27天: 图
- Shell variables, system predefined variables $home, $pwd, $shell, $user, custom variables, special variables $n, $, $*, [email protected],
- Matlab/simulink sample sharing for solving differential equations
- Shell运算符、$((运算式))” 或 “$[运算式]、expr方法、条件判断、test condition、[ condition ]、两个整数之间比较、按照文件权限进行判断、按照文件类型进行判断
- 线代003
- WGAN、WGAN-GP、BigGAN
- Wind10 configure ADB command
- FSM onehot answer record
- 【英雄哥六月集训】第 24天: 线段树
猜你喜欢
随机推荐
pytorch的安装(非常详细)
Understanding and code implementation of Se (sequence and exception) module
Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量
Oracle view hard parsing
Oracle resizing data files
Local connection to remote server database under Windows platform (I)
程序的翻译和执行,从编辑、预处理、编译、汇编、链接到执行
Sub query of database performance series
Summary of binary tree exercises
Basic statement of database operation
Eslint的报错信息Module Error (from ./node_modules/[email protected]@eslint-loader/index.js)解决方法
Key points of ES6 class inheritance
Mysql database experiment training 5, data query YGGL database query (detailed)
文件上传漏洞相关
3D face reconstruction and dense alignment with position map progression network
Anaconda installation (very detailed)
FTP 服务器
Cannot start after installing MySQL 5.7.27 in CentOS 7? (Language bash)
3D人脸重建:Joint 3D Face Reconstruction and Dense Alignment with position Map Regression Network
Vs2019 Community Edition Download tutorial (detailed)









