当前位置:网站首页>2022.6.20-----leetcode.715
2022.6.20-----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);
}
边栏推荐
- 面试阿里测开岗失败后,被面试官在朋友圈吐槽了......(心塞)
- 丝网印刷的种类及其应用方法
- Question about SQL: SQL question -- SQL code for multiple account logins
- HL7Exception: Can‘t XML-encode a GenericMessage. Message must have a recognized struct
- Qt编译出错ERROR: Unknown module(s) in QT: script
- Tupu software is the digital twin of offshore wind power, striving to be the first
- 解析少儿编程的多元评价体系
- Is it safe for individuals to buy stocks with flush software? How to buy stocks
- Types and application methods of screen printing
- 经典模型——NiN&GoogLeNet
猜你喜欢
项目部署遇到的问题-生产环境
Add an "open search description" to the site to adapt to the browser's "site search"“
Solve the problem that the uniapp plug-in Robin editor reports an error when setting the font color and background color
Graphics card, GPU, CPU, CUDA, video memory, rtx/gtx and viewing mode
Run multiple main functions in the clion project
【哈希表】很简单的拉链法哈希结构,以至于效果太差,冲突太多,链表太长
TiFlash 函数下推必知必会丨十分钟成为 TiFlash Contributor
Group counting notes - instruction pipeline of CPU
Todolist incomplete, completed
Wealth freedom skills: commercialize yourself
随机推荐
计组笔记——CPU的指令流水
Question about SQL: SQL question -- SQL code for multiple account logins
How Inkscape converts PNG pictures to SVG pictures without distortion
Inkscape如何将png图片转换为svg图片并且不失真
经典模型——NiN&GoogLeNet
On virtual memory and oom in project development
Multimedia elements, audio, video
Cloud Computing Foundation -0
Qixia fire department carries out fire safety training on construction site
进度条
【Appium踩坑】io.appium.uiautomator2.common.exceptions.InvalidArgumentException: ‘capabilities‘ are mand
Group note data representation and operation check code
进程之间的通信方式
工业机器人之“慧眼”——机器视觉
2022年挖财证券开户安全嘛?
Classic model – RESNET
Route jump: click the operation button of the list to jump to another menu page and activate the corresponding menu
MySQL stored procedure
360 second understanding of smartx hyper converged infrastructure
HL7Exception: Can‘t XML-encode a GenericMessage. Message must have a recognized struct