当前位置:网站首页>[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III
[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III
2022-07-07 04:39:00 【Wang Liuliu, who loves to write bugs】
Previous articles : Line segment tree analysis and topic practice written on YuQue
Line tree template :
/** * Line segment tree ( Dynamic opening point ) **/
public class SegmentTreeDynamic {
// Segment tree data structure
class Node {
Node left, right;
int val, add;
}
private int N = (int) 1e9;
// First create the first node
private Node root = new Node();
// to update
public void update(Node node, int start, int end, int l, int r, int val) {
if (l <= start && end <= r) {
node.val += (end - start + 1) * val;
node.add += val;
return ;
}
int mid = (start + end) >> 1;
pushDown(node, mid - start + 1, end - mid);
// Update left and right intervals
if (l <= mid) update(node.left, start, mid, l, r, val);
if (r > mid) update(node.right, mid + 1, end, l, r, val);
pushUp(node);
}
// Inquire about
public int query(Node node, int start, int end, int l, int r) {
if (l <= start && end <= r) return node.val;
int mid = (start + end) >> 1, ans = 0;
pushDown(node, mid - start + 1, end - mid);
if (l <= mid) ans += query(node.left, start, mid, l, r);
if (r > mid) ans += query(node.right, mid + 1, end, l, r);
return ans;
}
// Update the node value up
private void pushUp(Node node) {
node.val = node.left.val + node.right.val;
}
// Push down
private void pushDown(Node node, int leftNum, int rightNum) {
// If there are no left and right nodes, create a new
if (node.left == null) node.left = new Node();
if (node.right == null) node.right = new Node();
if (node.add == 0) return ;
node.left.val += node.add * leftNum;
node.right.val += node.add * rightNum;
// The interval is 「 Addition and subtraction 」 Update operation for , You need to add up when pushing down the lazy flag , Cannot directly cover
node.left.add += node.add;
node.right.add += node.add;
node.add = 0;
}
}
● For, expressed as 「 Interval and 」 And the interval is 「 Addition and subtraction 」 Update operation of , When updating node values 『 need ️ Left and right child interval Leaf node The number of 』; When we push down the lazy tag 『 Need to add up 』( This situation is consistent with the template ) Such as title Recent requests
● For, expressed as 「 Interval and 」 And the interval is 「 Cover 」 Update operation of , When we update the node value 『 need ️ Left and right child interval Leaf node The number of 』; When we push down the lazy tag 『 There is no need to add up 』!!( Because it is an override operation !!) Such as title 307. Area and retrieval - Array can be modified
● For, expressed as 「 The best value of the interval 」 And the interval is 「 Addition and subtraction 」 Update operation of , When we update the node value 『 Unwanted ️ The number of leaf nodes in the left and right child intervals 』; When we push down the lazy tag 『 Need to add up 』!! Such as title My schedule I、 My schedule III
Be careful :
For the title Recent requests and 307. Area and retrieval - Array can be modified Sure 「 no need ️ The number of leaf nodes in the left and right child intervals 」
Why? ?? Because these two topics are 「 Click Update 」, When introducing the update of line segment tree :「 Click Update 」 and 「 Interval update 」 Can be merged into one ,「 Click Update 」 The update length is 1 The range of .
The above two questions call Update function By :
- update(root, 1, N, t, t, 1);
- update(root, 0, N, i, i, nums[i]);
Because the interval is a point , So it must be updated to the leaf node , So you don't have to ️ The number of leaf nodes in the left and right child intervals !!
Recent requests
Code implementation :
class RecentCounter {
public RecentCounter() {
}
public int ping(int t) {
update(root, 1, N, t, t, 1);
return query(root, 1, N, Math.max(0, t - 3000), t);
}
// *************** Here is the template ***************
class Node {
// Left and right child nodes
Node left, right;
// Current node value , And the value of the lazy tag
int val, add;
}
private int N = (int) 1e9;
private Node root = new Node();
public void update(Node node, int start, int end, int l, int r, int val) {
if (l <= start && end <= r) {
node.val += val;
node.add += val;
return ;
}
int mid = (start + end) >> 1;
pushDown(node, mid - start + 1, end - mid);
if (l <= mid) update(node.left, start, mid, l, r, val);
if (r > mid) update(node.right, mid + 1, end, l, r, val);
pushUp(node);
}
public int query(Node node, int start, int end, int l, int r) {
if (l <= start && end <= r) return node.val;
int mid = (start + end) >> 1, ans = 0;
pushDown(node, mid - start + 1, end - mid);
if (l <= mid) ans = query(node.left, start, mid, l, r);
if (r > mid) ans += query(node.right, mid + 1, end, l, r);
return ans;
}
private void pushUp(Node node) {
node.val = node.left.val + node.right.val;
}
private void pushDown(Node node, int leftNum, int rightNum) {
if (node.left == null) node.left = new Node();
if (node.right == null) node.right = new Node();
if (node.add == 0) return ;
node.left.val += node.add * leftNum;
node.right.val += node.add * rightNum;
// The interval is 「 Addition and subtraction 」 Update operation for , You need to add up when pushing down the lazy flag , Cannot directly cover
node.left.add += node.add;
node.right.add += node.add;
node.add = 0;
}
}
307. Area and retrieval - Array can be modified
class NumArray {
public NumArray(int[] nums) {
N = nums.length - 1;
for (int i = 0; i <= N; i++) {
update(root, 0, N, i, i, nums[i]);
}
}
public void update(int index, int val) {
update(root, 0, N, index, index, val);
}
public int sumRange(int left, int right) {
return query(root, 0, N, left, right);
}
//*********** Here's the template ****************
class Node {
// Left and right child nodes
Node left, right;
// Current node value , And the value of the lazy tag
int val, add;
}
private int N;
private Node root = new Node();
// to update
public void update(Node node, int start, int end, int l, int r, int val) {
if (l <= start && end <= r) {
node.val = (end - start + 1) * val;
node.add = val;
return ;
}
int mid = (start + end) >> 1;
pushDown(node, mid - start + 1, end - mid);
if (l <= mid) update(node.left, start, mid, l, r, val);
if (r > mid) update(node.right, mid + 1, end, l, r, val);
pushUp(node);
}
// Inquire about
public int query(Node node, int start, int end, int l, int r) {
if (l <= start && end <= r) return node.val;
int mid = (start + end) >> 1, ans = 0;
pushDown(node, mid - start + 1, end - mid);
if (l <= mid) ans += query(node.left, start, mid, l, r);
if (r > mid) ans += query(node.right, mid + 1, end, l, r);
return ans;
}
// Up
private void pushUp(Node node) {
node.val = node.left.val + node.right.val;
}
// Down
private void pushDown(Node node, int leftNum, int rightNum) {
if (node.left == null) node.left = new Node();
if (node.right == null) node.right = new Node();
if (node.add == 0) return ;
node.left.val = node.add * leftNum;
node.right.val = node.add * rightNum;
node.left.add = node.add;
node.right.add = node.add;
node.add = 0;
}
}
My schedule III
「 The value of a node can represent more than the sum of the interval 」, just so so 「 Expressed as the maximum value of the current interval 」, The storage of this topic is interval Maximum
Code :
class MyCalendarThree {
public MyCalendarThree() {
}
public int book(int start, int end) {
// It's only used. update
update(root, 0, N, start, end - 1, 1);
// The maximum value is the value of the root node
return root.val;
}
// *************** Here is the template ***************
class Node {
// Left and right child nodes
Node left, right;
// Current node value , And the value of the lazy tag
int val, add;
}
private int N = (int) 1e9;
private Node root = new Node();
public void update(Node node, int start, int end, int l, int r, int val) {
if (l <= start && end <= r) {
node.val += val;
node.add += val;
return ;
}
pushDown(node);
int mid = (start + end) >> 1;
if (l <= mid) update(node.left, start, mid, l, r, val);
if (r > mid) update(node.right, mid + 1, end, l, r, val);
pushUp(node);
}
public int query(Node node, int start, int end, int l, int r) {
if (l <= start && end <= r) return node.val;
pushDown(node);
int mid = (start + end) >> 1, ans = 0;
if (l <= mid) ans = query(node.left, start, mid, l, r);
if (r > mid) ans = Math.max(ans, query(node.right, mid + 1, end, l, r));
return ans;
}
private void pushUp(Node node) {
// Each node stores the maximum value of the current interval
node.val = Math.max(node.left.val, node.right.val);
}
private void pushDown(Node node) {
if (node.left == null) node.left = new Node();
if (node.right == null) node.right = new Node();
if (node.add == 0) return ;
node.left.val += node.add;
node.right.val += node.add;
node.left.add += node.add;
node.right.add += node.add;
node.add = 0;
}
}
My schedule I
according to Ⅲ How to solve the problem :
class MyCalendar {
public MyCalendar() {
}
public boolean book(int start, int end) {
// First query whether the interval is 0
if (query(root, 0, N, start, end - 1) != 0) return false;
// Update the interval
update(root, 0, N, start, end - 1, 1);
return true;
}
// *************** Here is the template ***************
class Node {
// Left and right child nodes
Node left, right;
// Current node value , And the value of the lazy tag
int val, add;
}
private int N = (int) 1e9;
private Node root = new Node();
public void update(Node node, int start, int end, int l, int r, int val) {
if (l <= start && end <= r) {
node.val += val;
node.add += val;
return ;
}
pushDown(node);
int mid = (start + end) >> 1;
if (l <= mid) update(node.left, start, mid, l, r, val);
if (r > mid) update(node.right, mid + 1, end, l, r, val);
pushUp(node);
}
public int query(Node node, int start, int end, int l, int r) {
if (l <= start && end <= r) return node.val;
pushDown(node);
int mid = (start + end) >> 1, ans = 0;
if (l <= mid) ans = query(node.left, start, mid, l, r);
if (r > mid) ans = Math.max(ans, query(node.right, mid + 1, end, l, r));
return ans;
}
private void pushUp(Node node) {
// Each node stores the maximum value of the current interval
node.val = Math.max(node.left.val, node.right.val);
}
private void pushDown(Node node) {
if (node.left == null) node.left = new Node();
if (node.right == null) node.right = new Node();
if (node.add == 0) return ;
node.left.val += node.add;
node.right.val += node.add;
node.left.add += node.add;
node.right.add += node.add;
node.add = 0;
}
}
边栏推荐
- The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
- Mathematical analysis_ Notes_ Chapter 10: integral with parameters
- 軟件測試之網站測試如何進行?測試小攻略走起!
- [multi threading exercise] write a multi threading example of the producer consumer model.
- EasyCVR无法使用WebRTC进行播放,该如何解决?
- 日常工作中程序员最讨厌哪些工作事项?
- What is CGI, IIS, and VPS "suggested collection"
- JetBrain Pycharm的一系列快捷键
- ACL2022 | 分解的元学习小样本命名实体识别
- How to conduct website testing of software testing? Test strategy let's go!
猜你喜欢
In cooperation with the research team of the clinical trial center of the University of Hong Kong and Hong Kong Gangyi hospital, Kexing launched the clinical trial of Omicron specific inactivated vacc
树与图的深度优先遍历模版原理
Have you got the same "artifact" of cross architecture development praised by various industry leaders?
英特尔与信步科技共同打造机器视觉开发套件,协力推动工业智能化转型
Win11图片打不开怎么办?Win11无法打开图片的修复方法
图灵诞辰110周年,智能机器预言成真了吗?
[on automation experience] the growth path of automated testing
[team learning] [phase 34] Baidu PaddlePaddle AI talent Creation Camp
接口自动化测试实践指导(中):接口测试场景有哪些
Intel David tuhy: the reason for the success of Intel aoten Technology
随机推荐
一度辍学的数学差生,获得今年菲尔兹奖
Master the secrets of software security testing methods, and pinch the security test report with your hands
Win11玩绝地求生(PUBG)崩溃怎么办?Win11玩绝地求生崩溃解决方法
Zhou Yajin, a top safety scholar of Zhejiang University, is a curiosity driven activist
什么是Web3
【线段树实战】最近的请求次数 + 区域和检索 - 数组可修改+我的日程安排表Ⅰ/Ⅲ
Case reward: Intel brings many partners to promote the innovation and development of multi domain AI industry
Fiance donated 500million dollars to female PI, so that she didn't need to apply for projects, recruited 150 scientists, and did scientific research at ease!
VM virtual machine operating system not found and NTLDR is missing
Wechat can play the trumpet. Pinduoduo was found guilty of infringement. The shipment of byte VR equipment ranks second in the world. Today, more big news is here
一图看懂!为什么学校教了你Coding但还是不会的原因...
九章云极DataCanvas公司获评36氪「最受投资人关注的硬核科技企业」
[written to the person who first published the paper] common problems in writing comprehensive scientific and Technological Papers
Two divs are on the same line, and the two divs do not wrap "recommended collection"
日常工作中程序员最讨厌哪些工作事项?
Fix the problem that the highlight effect of the main menu disappears when the easycvr Video Square is clicked and played
[knife-4j quickly build swagger]
过气光刻机也不能卖给中国!美国无理施压荷兰ASML,国产芯片再遭打压
Introduction to the PureMVC series
高薪程序员&面试题精讲系列120之Redis集群原理你熟悉吗?如何保证Redis的高可用(上)?