当前位置:网站首页>动态线段树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);
}边栏推荐
- 路由跳转之点击列表的操作按钮,跳转至另一个菜单页面并激活相应的菜单
- 经典模型——ResNet
- String到底能不能改变?
- XGBoost, lightGBM, CatBoost——尝试站在巨人的肩膀上
- kotlin快速上手
- 【哈希表】改进,拉链法哈希结构——直接用两个索引查找,不用每次都hash和%一遍
- 项目部署遇到的问题-生产环境
- [hash table] a very simple zipper hash structure, so that the effect is too poor, there are too many conflicts, and the linked list is too long
- Is it safe to open a fund account? How to apply
- USB driver -debug
猜你喜欢

On virtual memory and oom in project development

Types and application methods of screen printing

Partition, column, list

解决uniapp插件robin-editor设置字体颜色和背景颜色报错的问题

Tupu software is the digital twin of offshore wind power, striving to be the first

Redux thunk simple case, advantages, disadvantages and thinking

双碳红利+基建大年 | 图扑深耕水利水电绿色智能装备领域

MySQL development environment

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

丝网印刷的种类及其应用方法
随机推荐
解析创客空间机制建设的多样化
usb peripheral 驱动 - 枚举
How Inkscape converts PNG pictures to SVG pictures without distortion
小米电视的网页和珠宝的网页
Classic model alexnet
【Appium踩坑】io.appium.uiautomator2.common.exceptions.InvalidArgumentException: ‘capabilities‘ are mand
kitti2bag 安装出现的各种错误
Nebula Graph学习篇3_多线程完成6000w+关系数据迁移
Good news | congratulations on the addition of 5 new committers in Apache linkage (incubating) community
QT compilation error: unknown module (s) in qt: script
Using meta analysis to drive the development of educational robot
Cultivate children's creativity under the concept of project steam Education
Insect structure and Deconstruction
Pay attention to the entrance components of official account in the applet
Multimedia elements, audio, video
Cliquez sur le bouton action de la liste pour passer à une autre page de menu et activer le menu correspondant
【论文笔记】Learning to Grasp with Primitive Shaped Object Policies
String到底能不能改变?
论文回顾:Unmixing-Based Soft Color Segmentation for Image Manipulation
Vulhub replicate an ActiveMQ