当前位置:网站首页>Dynamic segment tree leetcode six hundred and ninety-nine
Dynamic segment tree leetcode six hundred and ninety-nine
2022-06-12 08:34:00 【Lu 727】
// Dynamic segment tree
public class DynamicSegTree {
private Map<Integer, Integer> tree;// Line segment tree , Represents the value of a node
private Map<Integer, Integer> lazy;// Lazy mark
private int N;// Maximum range
public DynamicSegTree() {
tree = new HashMap<Integer, Integer>();
lazy = new HashMap<Integer, Integer>();
N= (int)1e9;
}
// Interval modification
public void update(int l,int r,int val){
if(l>r) return;
update(l,r,1,N,1,val);
}
private void update( int l, int r,int curl, int curr, int node,int val) {
// Interval no intersection
if (curl>r||curr<l)
return;
// The current interval is within the target interval
if (l<=curl&&curr<=r) {
tree.put(node,val);// Update current node
if(curl<curr) lazy.put(node, val);
} else {// There is an intersection between the current interval and the target interval
int mid = (curl + curr) >> 1;
if(lazy.getOrDefault(node,0)!=0)
pushDown(node,lazy.get(node));// Pass tag down
// Split processing interval
update( l, r, curl, mid,2 * node,val);
update(l, r, mid+1, curr, 2 * node + 1,val);
// Update current node
tree.put(node,Math.max(tree.getOrDefault(2 * node, 0),
tree.getOrDefault(2 * node + 1, 0)));
}
}
// Pass the tag to the next level
private void pushDown(int node,int _lazy){
lazy.put(node*2,_lazy);
lazy.put(node*2+1,_lazy);
tree.put(node*2,_lazy);
tree.put(node*2+1,_lazy);
lazy.put(node,0);
}
// Interval query
public int query(int l,int r){
if(l>r) return -1;
return query(l,r,1,N,1);
}
private int query(int l,int r,int curl,int curr,int node){
// Not in the target range
if(curl>r||curr<l)
return 0;
// In the target range
if(l<=curl&&curr<=r)
return tree.getOrDefault(node,0);
// Some have intersections , Split interval processing
int mid=(curl+curr)>>1;
if(lazy.getOrDefault(node,0)!=0)
pushDown(node,lazy.get(node));
return Math.max(query(l,r,curl,mid,node*2),query(l,r,mid+1,curr,node*2+1));
}
}
public List<Integer> fallingSquares(int[][] positions) {
List<Integer> ans=new ArrayList<>();
DynamicSegTree t=new DynamicSegTree();
for(int[] position:positions){
int max=t.query(position[0],position[0]+position[1]-1);// Query the maximum value of the block interval
// Update block interval
t.update(position[0],position[0]+position[1]-1,max+position[1]);
ans.add(t.query(1,t.N));
}
return ans;
}边栏推荐
- 企业上线MES软件的费用真的很贵?
- Py & go programming skills: logic control to avoid if else
- 根据有效期显示距离当前还剩多少天有效期
- Error: ER_ NOT_ SUPPORTED_ AUTH_ MODE: Client does not support authentication protocol requested ... ...
- 目前MES应用很多,为什么APS排程系统很少,原因何在?
- Never use MES as a tool, or you will miss the most important thing
- Hands on deep learning -- discarding method and its code implementation
- 2022.6.11-----leetcode.926
- Hands on deep learning -- Introduction to linear regression model
- Call method and apply method
猜你喜欢

如何理解APS系统的生产排程?

Engineers learn music theory (II) scale and tendency

Where does the driving force of MES system come from? What problems should be paid attention to in model selection?
![[dynamic memory management] malloc & calloc and realloc and written test questions and flexible array](/img/d2/a6276d8415c46124920395df5651d1.png)
[dynamic memory management] malloc & calloc and realloc and written test questions and flexible array

How to understand the production scheduling of APS system?

FDA reviewers say Moderna covid vaccine is safe and effective for children under 5 years of age

报错:文件夹在另一个程序中打开无法删除怎么办

Error: clear the history in the search box in the website?
![[advanced pointer III] implement C language quick sorting function qsort & callback function](/img/f0/3729db83ba3eb15c7df0958858ece9.png)
[advanced pointer III] implement C language quick sorting function qsort & callback function

Hands on deep learning -- weight decay and code implementation
随机推荐
如何理解APS系统的生产排程?
Installation series of ROS system (II): ROS rosdep init/update error reporting solution
Model Trick | CVPR 2022 Oral - Stochastic Backpropagation A Memory Efficient Strategy
网站Colab与Kaggle
(p36-p39) right value and right value reference, role and use of right value reference, derivation of undetermined reference type, and transfer of right value reference
超全MES系统知识普及,必读此文
MYSQL中的触发器
MES系统是什么?MES系统的操作流程是怎样?
MPLS的原理与配置
ASP. Net project development practice introduction_ Item VI_ Error report (summary of difficult problems when writing the project)
企业上MES系统的驱动力来自哪里?选型又该注意哪些问题?
【新规划】
Error: ER_ NOT_ SUPPORTED_ AUTH_ MODE: Client does not support authentication protocol requested ... ...
Ankerui motor protector has the functions of overload inverse time limit, overload definite time limit, grounding, starting timeout, leakage, underload, phase failure, locked rotor, etc
JVM学习笔记:垃圾回收机制
What is the MES system? What is the operation process of MES system?
[dynamic memory management] malloc & calloc and realloc and written test questions and flexible array
【指针进阶三】实现C语言快排函数qsort&回调函数
进制GB和GiB的区别
Hands on deep learning -- Introduction to linear regression model