当前位置:网站首页>动态线段树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);
}边栏推荐
- Drag and drop
- Preparation for wechat applet development
- Using meta analysis to drive the development of educational robot
- Analysis of technological changes in social robots
- 国信金太阳靠谱吗?开证券账户安全吗?
- Hardware creation principle of campus maker space
- Pay attention to the entrance components of official account in the applet
- Is Guoxin golden sun reliable? Is it safe to open a securities account?
- jupyter notebook的插件安装以及快捷键
- Popupwindow utility class
猜你喜欢

Request object, send request

Xgboost, lightgbm, catboost -- try to stand on the shoulders of giants

Nebula Graph学习篇3_多线程完成6000w+关系数据迁移

Review of the paper: unmixing based soft color segmentation for image manipulation

String到底能不能改变?

计组笔记 数据表示与运算 校验码部分

Multimedia elements, audio, video

经典模型——AlexNet

MySQL高级部分( 四: 锁机制、SQL优化 )

用元分析法驱动教育机器人的发展
随机推荐
[reading papers] fbnetv3: joint architecture recipe search using predictor training network structure and super parameters are all trained by training parameters
[hash table] improved, zipper hash structure - directly use two indexes to search, instead of hashing and% every time
The "eye" of industrial robot -- machine vision
【论文笔记】Learning to Grasp with Primitive Shaped Object Policies
经典模型——NiN&GoogLeNet
View of MySQL
【论文笔记】Deep Reinforcement Learning Control of Hand-Eye Coordination with a Software Retina
双碳红利+基建大年 | 图扑深耕水利水电绿色智能装备领域
经典模型——ResNet
计组笔记 数据表示与运算 校验码部分
进度条
渐变
MySQL开发环境
指南针app是正规的吗?到底安不安全
On virtual memory and oom in project development
Classic model alexnet
How Inkscape converts PNG pictures to SVG pictures without distortion
Plug in installation and shortcut keys of jupyter notebook
Upload file / text / picture, box shadow
国信金太阳靠谱吗?开证券账户安全吗?