当前位置:网站首页>Dynamic segment tree leetcode seven hundred and fifteen
Dynamic segment tree leetcode seven hundred and fifteen
2022-06-26 03:41:00 【Lu 727】
private Map<Integer, Integer> tree;// Line segment tree , Represents the value of a node
private Map<Integer, Integer> lazy;// Lazy mark
private int N;
public RangeModule() {
tree = new HashMap<Integer, Integer>();
lazy = new HashMap<Integer, Integer>();
N= (int)1e9;
}
// Interval modification ( increase )
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) {
int sum=tree.getOrDefault(node, 0);// Current interval and
if(val==1&&sum<(curr-curl+1)*val){// Some nodes in the current interval are not tracked
tree.put(node,(curr-curl+1)*val);// Update current node
if(curl<curr) lazy.put(node, 1);
}else if(val==-1&&sum>0) {// There are tracked nodes in the current interval
tree.put(node, 0);// Update current node
if (curl < curr) lazy.put(node, -1);
}
} 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,curr-curl+1);
update( l, r, curl, mid,2 * node,val);
update(l, r, mid+1, curr, 2 * node + 1,val);
tree.put(node, tree.getOrDefault(2 * node, 0)+tree.getOrDefault(2 * node + 1, 0));
}
}
// Pass the tag to the next level
private void pushDown(int node,int len){
int _lazy=lazy.get(node);
int sum1=tree.getOrDefault(node*2,0);
int sum2=tree.getOrDefault(node*2+1,0);
if(_lazy==1){
// Untracked nodes exist in the interval
if(sum1<(len-len/2)*_lazy){
lazy.put(node*2,1);
tree.put(node*2,(len-len/2)*_lazy);
}
if(sum2<len/2*_lazy){
lazy.put(node*2+1,1);
tree.put(node*2+1,len/2*_lazy);
}
}else{
// There are tracked nodes in the interval
if(sum1>0){
lazy.put(node*2,-1);
tree.put(node*2,0);
}
if(sum2>0){
lazy.put(node*2+1,-1);
tree.put(node*2+1,0);
}
}
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,curr-curl+1);
return query(l,r,curl,mid,node*2)+query(l,r,mid+1,curr,node*2+1);
}
public void addRange(int left, int right) {
update( left, right - 1, 1);
}
public boolean queryRange(int left, int right) {
return query(left, right - 1) == right - left;
}
public void removeRange(int left, int right) {
update(left, right - 1, -1);
}
边栏推荐
- Plug in installation and shortcut keys of jupyter notebook
- Classic model – RESNET
- Deletelater Usage Summary in QT
- todolist未完成,已完成
- 面试阿里测开岗失败后,被面试官在朋友圈吐槽了......(心塞)
- Do you want to add a key to the applet or for sequence?
- [appium stepping pit] io appium. uiautomator2. common. exceptions. InvalidArgumentException: ‘capabilities‘ are mand
- Graphics card, GPU, CPU, CUDA, video memory, rtx/gtx and viewing mode
- Is Guoxin golden sun reliable? Is it safe to open a securities account?
- GStreamer allocator and pool
猜你喜欢
随机推荐
经典模型——AlexNet
丝网印刷的种类及其应用方法
Problems encountered in project deployment - production environment
点击事件
进程之间的通信方式
General operations of asynctask
培育项目式Steam教育理念下的儿童创造力
多媒体元素,音频、视频
USB driver -debug
论文回顾:Unmixing-Based Soft Color Segmentation for Image Manipulation
[paper notes] supersizing self supervision: learning to grasp from 50K tries and 700 robot hours
MySQL数据库基础
360 second understanding of smartx hyper converged infrastructure
【哈希表】改进,拉链法哈希结构——直接用两个索引查找,不用每次都hash和%一遍
Uni app custom navigation bar component
Analysis of technological changes in social robots
Group note data representation and operation check code
“再谈”协议
【读点论文】FBNetV3: Joint Architecture-Recipe Search using Predictor Pretraining 网络结构和超参数全当训练参数给训练了
Uni app Baidu cloud realizes OCR ID card recognition