当前位置:网站首页>[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;
}边栏推荐
- BannerViewPager
- Have a meal, dry pot, fat intestines + palm treasure!
- Analysis report on market demand and investment strategy of China's re guarantee industry 2022-2028
- Details of thansmitablethreadlocal
- Wireshark basic commands
- 3GPP RAN第一次F2F会议,都干了些啥?
- 设备管理-借还模块1
- How does Eldon's ring of the law get lune quickly? Introduction to the fastest and safest method for obtaining lune
- API call display, detailed API of Taobao, tmall and pinduoduo commodity pages, and return of APP side original data parameters
- Detailed explanation of yolox network structure
猜你喜欢

In 2021, the global revenue of electro-optical modulator (EOM) is about USD 360.3 million, and it is expected to reach USD 704.4 million in 2028

chrome浏览器解决跨域问题

Shell 数组和函数

美团获得小样本学习榜单FewCLUE第一!Prompt Learning+自训练实战

In 2021, the global fire pump drive power revenue is about $381million, and it is expected to reach $489.3 million in 2028
![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

RHCA回忆录---CL280介绍

【观察】华为下一代数据中心,为广西低碳高质量发展“添动能”

选电子工程被劝退,真的没前景了?

一种灵活注入 Istio Sidecar 的方案探索
随机推荐
[today in history] June 12: the United States entered the era of digital television; Mozilla's original developer was born; 3com merges with American Robotics
5g R17 standard is frozen. What does it say?
5G R17标准冻结,主要讲了些啥?
The Bean Validation API is on the classpath but no implementation could be found
负数取余问题
The component style set by uniapp takes effect in H5 and app, but does not take effect in wechat applet. The problem is solved
Report on the development status of China's asset appraisal industry and suggestions for future strategic planning 2022-2027
基於分布式數據庫本身的定時備份方法
Fault analysis | a case of MySQL remote slave database replication delay
模塊八作業
[matrix theory & graph theory] final exam review mind map
基于FPGA的VGA协议实现
vc hacon 聯合編程 GenImage3Extern WriteImage
负数取余问题
DACOM G150 dual-mode earphones make sound for love and protect the healthy growth of children's hearing
基于微信电子书阅读小程序毕业设计毕设作品(8)毕业设计论文模板
Lua record
How does Eldon's ring of the law get lune quickly? Introduction to the fastest and safest method for obtaining lune
China's asset management market demand and future competitive trends outlook report 2022-2028
New product launch