当前位置:网站首页>2022.6.20-----leetcode. seven hundred and fifteen
2022.6.20-----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);
}
边栏推荐
- 指南针app是正规的吗?到底安不安全
- Cliquez sur le bouton action de la liste pour passer à une autre page de menu et activer le menu correspondant
- The "eye" of industrial robot -- machine vision
- mysql存储过程
- Multimedia elements, audio, video
- Inkscape如何将png图片转换为svg图片并且不失真
- Pay attention to the entrance components of official account in the applet
- View of MySQL
- Tupu software is the digital twin of offshore wind power, striving to be the first
- Class diagram
猜你喜欢
解析少儿编程的多元评价体系
MySQL addition, deletion, query and modification (Advanced)
论文回顾:Unmixing-Based Soft Color Segmentation for Image Manipulation
Review of the paper: unmixing based soft color segmentation for image manipulation
MySQL增删查改(进阶)
云计算基础-0
Drag and drop
HL7Exception: Can‘t XML-encode a GenericMessage. Message must have a recognized struct
Qixia fire department carries out fire safety training on construction site
计组笔记——CPU的指令流水
随机推荐
Cultivate children's creativity under the concept of project steam Education
Double carbon bonus + great year of infrastructure construction 𞓜 deep ploughing into the field of green intelligent equipment for water conservancy and hydropower
Hardware creation principle of campus maker space
个人用同花顺软件买股票安全吗?怎么炒股买股票呢
MySQL addition, deletion, query and modification (Advanced)
项目部署遇到的问题-生产环境
[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
Uni app custom navigation bar component
MySQL增删查改(初阶)
Add an "open search description" to the site to adapt to the browser's "site search"“
The "eye" of industrial robot -- machine vision
MySQL数据库基础
Classic model – RESNET
Popupwindow utility class
关于#sql#的问题:SQL问题--账号多地登录的SQL代码
等保备案是等保测评吗?两者是什么关系?
Classic model - Nin & googlenet
Nebula Graph学习篇3_多线程完成6000w+关系数据迁移
Preparation for wechat applet development
Uni app QR code scanning and identification function