当前位置:网站首页>动态线段树leetcode.715
动态线段树leetcode.715
2022-06-26 03:16:00 【路Lu727】
private Map<Integer, Integer> tree;//线段树,表示某节点的值
private Map<Integer, Integer> lazy;//懒标记
private int N;
public RangeModule() {
tree = new HashMap<Integer, Integer>();
lazy = new HashMap<Integer, Integer>();
N= (int)1e9;
}
//区间修改(增加)
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) {
//区间无交集
if (curl>r||curr<l)
return;
//当前区间在目标区间内
if (l<=curl&&curr<=r) {
int sum=tree.getOrDefault(node, 0);//当前区间和
if(val==1&&sum<(curr-curl+1)*val){//当前区间部分节点未跟踪
tree.put(node,(curr-curl+1)*val);//更新当前节点
if(curl<curr) lazy.put(node, 1);
}else if(val==-1&&sum>0) {//当前区间存在已跟踪节点
tree.put(node, 0);//更新当前节点
if (curl < curr) lazy.put(node, -1);
}
} else {//当前区间与目标区间存在交集
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));
}
}
//将标记向下一层传递
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){
//区间存在未跟踪节点
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{
//区间存在已跟踪节点
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);
}
//区间查询
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){
//不在目标区间
if(curl>r||curr<l)
return 0;
//在目标区间
if(l<=curl&&curr<=r)
return tree.getOrDefault(node,0);
//部分有交集,分割区间处理
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);
}
边栏推荐
- Partition, column, list
- Utonmos adheres to the principle of "collection and copyright" to help the high-quality development of traditional culture
- [paper notes] supersizing self supervision: learning to grasp from 50K tries and 700 robot hours
- 渐变
- 经典模型——AlexNet
- Qixia fire department carries out fire safety training on construction site
- 小程序或者for循序要不要加key?
- Mysql database foundation
- usb peripheral 驱动 - 枚举
- Classic model alexnet
猜你喜欢
【论文笔记】Learning to Grasp with Primitive Shaped Object Policies
Class diagram
MySQL addition, deletion, query and modification (primary level)
QT compilation error: unknown module (s) in qt: script
解析少儿编程的多元评价体系
MySQL增删查改(初阶)
Classic model alexnet
Double carbon bonus + great year of infrastructure construction 𞓜 deep ploughing into the field of green intelligent equipment for water conservancy and hydropower
解析社交机器人中的技术变革
Matlab| short term load forecasting of power system based on BP neural network
随机推荐
redux-thunk 简单案例,优缺点和思考
图扑软件数字孪生海上风电 | 向海图强,奋楫争先
kitti2bag 安装出现的各种错误
Question about SQL: SQL question -- SQL code for multiple account logins
Good news | congratulations on the addition of 5 new committers in Apache linkage (incubating) community
Butterknife unbinder uses flashback in fragment and viewpager
Graphics card, GPU, CPU, CUDA, video memory, rtx/gtx and viewing mode
【哈希表】改进,拉链法哈希结构——直接用两个索引查找,不用每次都hash和%一遍
Pay attention to the entrance components of official account in the applet
Cultivate children's creativity under the concept of project steam Education
【论文笔记】Learning to Grasp with Primitive Shaped Object Policies
Is it safe to open a fund account? How to apply
进程之间的通信方式
On virtual memory and oom in project development
Worm copy construction operator overload
Redux thunk simple case, advantages, disadvantages and thinking
Is it safe to open an account in flush online? How to open a brokerage account online
Binary search
用元分析法驱动教育机器人的发展
Deletelater Usage Summary in QT