当前位置:网站首页>[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;
}边栏推荐
- Market development planning and investment prospect analysis report of Chinese government investment and financing platform 2022-2027
- Wincc7.5 SP1 method for adjusting picture size to display resolution
- 【生成对抗网络学习 其三】BiGAN论文阅读笔记及其原理理解
- Shell programming regular expressions and metacharacters
- 王学岗room+paging3
- Software usage of Tencent cloud TDP virt viewer win client
- Cookie & session & kaptcha verification code
- vc hacon 联合编程 GenImage3Extern WriteImage
- VC Hacon Joint Programming genimage3extern writeimage
- 原生Servlet - 文件的Upload&Download
猜你喜欢
![[image denoising] image denoising based on regularization with matlab code](/img/d9/74f4d9cdb4bfe157ba781ec8f6a184.png)
[image denoising] image denoising based on regularization with matlab code
![[0008] unordered list](/img/16/7525d963e68757558dd55ff4d1a23a.png)
[0008] unordered list
![leetcode:5270. Minimum path cost in Grid [simple level DP]](/img/c5/37fd1878e92f95340926e0ea75f150.png)
leetcode:5270. Minimum path cost in Grid [simple level DP]

Rhca memoirs -- Introduction to cl280

运算器的基本结构

数据库全量SQL分析与审计系统性能优化之旅

A fruitful afternoon

VC Hacon Joint Programming genimage3extern writeimage
![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

"As a service", the future has come, starting from the present | new mode of it consumption, FOD billing on demand
随机推荐
RT-Thread 模拟器 simulator 搭建 LVGL 的开发调试环境
嵌入式开发:固件工程师的6项必备技能
On how to make digital transformation after the loan of large policy banks- Yixinhuachen
模塊八作業
Native servlet - upload & download of files
"As a service", the future has come, starting from the present | new mode of it consumption, FOD billing on demand
什么是数据驱动
3GPP RAN第一次F2F会议,都干了些啥?
The 14th five year development plan and investment prospect analysis report of China's oil and gas pipeline engineering construction 2022-2027
一种灵活注入 Istio Sidecar 的方案探索
5g R17 standard is frozen. What does it say?
BannerViewPager
Leetcode 494. Objectives and
Original publishing practice of pipeline in Jenkins docking with CMDB interface to obtain host list
What are meta-inf and WEB-INF respectively?
leetcode:6094. Company name [group enumeration + cannot repeat set intersection + product Cartesian product (repeat indicates length)]
Research Report on current market situation and investment prospect of China's tobacco RFID industry 2022-2027
I was badly hurt by the eight part essay...
The Bean Validation API is on the classpath but no implementation could be found
chrome浏览器解决跨域问题