当前位置:网站首页>[notes for question brushing] line segment tree
[notes for question brushing] line segment tree
2022-06-12 19:27:00 【Ctrl AC】
· Interval query
query function :
When the current node range of the segment tree is surrounded by the range to be queried, it directly returns .
Otherwise, determine the traversal direction of the left and right subtrees . The test data will not cross the boundary , No need to consider .
Supporting topics :click
#include<bits/stdc++.h>
using namespace std;
int a[100010],treea[400010],treeb[400010],ma,mi;
void build(int p,int l,int r)
{
if(l==r)
{
treea[p]=treeb[p]=a[l];
return;
}
int mid=(l+r)/2;
build(2*p,l,mid);
build(2*p+1,mid+1,r);
treea[p]=max(treea[2*p],treea[2*p+1]);
treeb[p]=min(treeb[2*p],treeb[2*p+1]);
}
void search(int p,int l,int r,int x,int y)
{
if(x<=l&&r<=y)
{
ma=max(ma,treea[p]);
mi=min(mi,treeb[p]);
return;
}
int mid=(l+r)/2;
if(y<=mid) search(2*p,l,mid,x,y);
else if(x>mid) search(2*p+1,mid+1,r,x,y);
else{
search(2*p,l,mid,x,y);
search(2*p+1,mid+1,r,x,y);
}
}
int main()
{
int n,q; cin>>n>>q;
for(int i=1;i<=n;i++) cin>>a[i];
build(1,1,n);
while(q--)
{
ma=0, mi=0x3f3f3f3f;
int x,y; cin>>x>>y;
search(1,1,n,x,y);
cout<<ma-mi<<endl;
}
return 0;
}· Build tree step by step
Take the length of the array as the dividing line to build the left and right subtrees , When l==r when , Is the location of the latest element
Supporting topics :click
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=2e5+5;
ll tree[4*MAXN],t;
void update(int node,int nodeleft,int noderight,int len,ll val){
if(nodeleft==noderight){
tree[node]=val; return;
}
int mid=(nodeleft+noderight)>>1;
if(mid>=len) update(node*2,nodeleft,mid,len,val);
else update(node*2+1,mid+1,noderight,len,val);
tree[node]=max(tree[node*2],tree[node*2+1]);
}
ll find(int node,int nodeleft,int noderight,int l,int r){
if(l<=nodeleft&&noderight<=r){
return tree[node];
}
int mid=(nodeleft+noderight)>>1;
if(r<=mid) return find(node*2,nodeleft,mid,l,r);
else if(l>mid) return find(node*2+1,mid+1,noderight,l,r);
else{
return max(find(node*2,nodeleft,mid,l,r),find(node*2+1,mid+1,noderight,l,r));
}
}
int main(){
int n,d; cin>>n>>d;
int len=0;
for(int i=0;i<n;i++){
char ch; int a;
cin>>ch>>a;
if(ch=='A'){
len++;
update(1,1,MAXN-1,len,(a+t)%d);
}
else{
t=find(1,1,MAXN-1,len-a+1,MAXN-1);
cout<<t<<endl;
}
}
return 0;
}· Interval query and change template
subject :click
#include<iostream>
#define int long long
using namespace std;
typedef long long ll;
const int N = 100005;
int w[N];
struct node {
int l, r;
ll sum;
ll add;
}tree[N<<2];
void build(int node,int nodeleft,int noderight) {
if (nodeleft == noderight) tree[node] = { nodeleft,noderight,w[nodeleft],0 };
else {
tree[node] = { nodeleft,noderight };
int mid = (nodeleft + noderight) / 2;
build(node * 2, nodeleft, mid);
build(node * 2 + 1, mid + 1, noderight);
tree[node].sum = tree[node * 2].sum + tree[node * 2 + 1].sum;
}
}
void pushdown(int node) {
struct node& root = tree[node], & left = tree[node * 2], & right = tree[node * 2 + 1];
if (root.add) {
left.add += root.add; left.sum += (left.r - left.l + 1) * root.add;
right.add += root.add; right.sum += (right.r - right.l + 1) * root.add;
root.add = 0;
}
}
void update(int node, int left, int right, int val) {
if (left <= tree[node].l && tree[node].r <= right) {
tree[node].sum += (tree[node].r - tree[node].l + 1) * val;
tree[node].add += val;
}
else {
pushdown(node);
int mid = (tree[node].l + tree[node].r) / 2;
if (left <= mid) update(node * 2, left, right, val);
if (right > mid) update(node * 2 + 1, left, right, val);
tree[node].sum = tree[node * 2].sum + tree[node * 2 + 1].sum;
}
}
int query(int node, int left, int right) {
if (left <= tree[node].l && tree[node].r <= right) return tree[node].sum;
pushdown(node);
int mid = (tree[node].l + tree[node].r) / 2;
if (right <= mid) return query(node * 2, left, right);
else if (left > mid) return query(node * 2 + 1, left, right);
else {
return query(node * 2, left, right) + query(node * 2 + 1, left, right);
}
}
signed main() {
int n, m; cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> w[i];
build(1, 1, n);
while (m--) {
char c; cin >> c;
if (c == 'C') {
int l, r, val; cin >> l >> r >> val;
update(1, l, r, val);
}
else {
int l, r; cin >> l >> r;
cout << query(1, l, r) << endl;
}
}
return 0;
}· Single point modification of interval query template
subject :click
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int num[N];
struct node {
int l, r;
int MAX;
}tree[N<<2];
void build(int node, int nodeleft, int noderight) {
if (nodeleft == noderight) tree[node] = { nodeleft,noderight,num[nodeleft] };
else {
tree[node] = { nodeleft,noderight };
int mid = (nodeleft + noderight) / 2;
build(node * 2, nodeleft, mid);
build(node * 2 + 1, mid + 1, noderight);
tree[node].MAX = max(tree[node * 2].MAX, tree[node * 2 + 1].MAX);
}
}
void update(int node, int id, int val) {
if (tree[node].l == id && tree[node].r == id) {
tree[node].MAX = val;
}
else {
int mid = (tree[node].l + tree[node].r) / 2;
if (id <= mid) update(node * 2, id, val);
else update(node * 2 + 1, id, val);
tree[node].MAX = max(tree[node * 2].MAX, tree[node * 2 + 1].MAX);
}
}
int query(int node, int left, int right) {
if (left <= tree[node].l && tree[node].r <= right) return tree[node].MAX;
else {
int mid = (tree[node].l + tree[node].r) / 2;
if (right <= mid) return query(node * 2, left, right);
else if (left > mid) return query(node * 2 + 1, left, right);
else {
return max(query(node * 2, left, right), query(node * 2 + 1, left, right));
}
}
}
int main() {
int n, m;
while (cin >> n >> m) {
for (int i = 1; i <= n; i++) cin >> num[i];
build(1, 1, n);
while (m--) {
char c; cin >> c;
int a, b; cin >> a >> b;
if (c == 'Q') {
cout << query(1, a, b) << endl;
}
else {
update(1, a, b);
}
}
}
return 0;
}边栏推荐
- Super heavy! Apache Hudi multimode index optimizes queries up to 30 times
- 王学岗room+paging3
- leetcodeSQL:602. Friend application II: who has the most friends
- How do I create my own appender in log4j- How to create my own Appender in log4j?
- Report on market demand trends and future strategic planning recommendations of the global and Chinese smart financial solutions industry 2022-2028
- 美团获得小样本学习榜单FewCLUE第一!Prompt Learning+自训练实战
- leetcode:5259. Calculate the total tax payable [simple simulation + see which range]
- VC hacon joint programming genimage3extern writeimage
- Rhca memoirs -- Introduction to cl280
- Wangxuegang room+paging3
猜你喜欢
![leetcode:6096. Success logarithm of spells and potions [sort + dichotomy]](/img/af/0d6ea1a25e65962616b2b049711510.png)
leetcode:6096. Success logarithm of spells and potions [sort + dichotomy]
![leetcode:6097. Match [set record + query one by one with the same length] after replacing characters](/img/03/d8286742bacf25b62c7dc3dcb0733e.png)
leetcode:6097. Match [set record + query one by one with the same length] after replacing characters

Shell 编程正则表达式及元字符

Meituan won the first place in fewclue in the small sample learning list! Prompt learning+ self training practice

The component style set by uniapp takes effect in H5 and app, but does not take effect in wechat applet. The problem is solved

5G R17标准冻结,主要讲了些啥?

New product launch
![[generation confrontation network learning III] reading notes of Bigan paper and its principle understanding](/img/6b/0f0815e20cdf6da28793562bcaede1.png)
[generation confrontation network learning III] reading notes of Bigan paper and its principle understanding

基于分布式数据库本身的定时备份方法

设备管理-借还模块1
随机推荐
Module 8 operation
CVPR 2022 oral Dalian Institute of technology proposed SCI: a fast and powerful low light image enhancement method
Research Report on current market situation and investment prospect of China's tobacco RFID industry 2022-2027
模块八作业
The Bean Validation API is on the classpath but no implementation could be found
WinCC7.5 SP1调整画面尺寸以适应显示分辨率的方法
5G R17标准冻结,主要讲了些啥?
Research Report on the overall scale, major manufacturers, major regions, products and application segments of lifeboats in the global market in 2022
基于微信电子书阅读小程序毕业设计毕设作品(7)中期检查报告
Reasonably configure thread pool
YOLOX网络结构详解
7:00 tonight | application of PhD debate self supervised learning in Recommendation System
数据库全量SQL分析与审计系统性能优化之旅
Mode of most elements (map, sort, random, Boyer Moore voting method)
Fault analysis | a case of MySQL remote slave database replication delay
Leetcode 474. One and zero
The solution of BiliBili video list name too long and incomplete display
Kali LAN ARP Spoofing and monitoring other hosts' Internet access records in the LAN
Dacom G150双模耳机,为爱发声,呵护孩子听力健康成长
[5gc] Introduction to three SSC (session and service continuity) modes