当前位置:网站首页>Luogu p4513 xiaobaiguang Park
Luogu p4513 xiaobaiguang Park
2022-06-26 13:57:00 【__ LazyCat__】
The question
Two operations are supported for a sequence , One is single point modification , The other is to query the maximum subsegment and .
Algorithm analysis
The complexity of a single maximal subsegment sum is O ( n ) O(n) O(n) Grade , But if you ask me many times , Use the original O ( n ) O(n) O(n) Method m That complexity O ( n m ) O(nm) O(nm) Toto explosion . In this case, the segment tree can be used to maintain the maximum subsegment sum of an interval .
specific working means
- For each tree node , Need to have an interval and w w w, The largest subsegment of an interval and v v v, Starting with the left and right endpoints of the sequence l w lw lw and r w rw rw. The first two are easy to understand , But why should the last two start with the left endpoint or the right endpoint ?
In fact, for the parent node , When inheriting from a child node , If two child nodes ( That is, two segment subsequence ) The largest sub segment of and the point without boundary , In fact, even if you don't add the following answer, it doesn't have to be a n s = max ( a Left Son Trees . w , b Right Son Trees . w ) ans=\max(a_{ The left subtree }.w,b_{ Right subtree }.w) ans=max(a Left Son Trees .w,b Right Son Trees .w) We need to consider that subsequences of two end intervals can be added together , At this time, we can take the a n s ans ans Then compare the size a n s = max ( a n s , a Left Son Trees . r w + b Right Son Trees . l w ) ans=\max(ans,a_{ The left subtree }.rw+b_{ Right subtree }.lw) ans=max(ans,a Left Son Trees .rw+b Right Son Trees .lw) It means that I can take the maximum sub segment of the left subtree that starts with the right endpoint and add the maximum sub segment of the right subtree that starts with the left endpoint , In this way, we can find the maximum sum of subsequences . - How do you get it l w lw lw and r w rw rw The value of ? All we need to do is p u s h u p pushup pushup The function is slightly modified .
For interval sum, just add it directly .
about l w lw lw Consider taking max ( a Left Son Trees . l w , a Left Son Trees . v + b Right Son Trees . l w ) \max(a_{ The left subtree }.lw,a_{ The left subtree }.v+b_{ Right subtree }.lw) max(a Left Son Trees .lw,a Left Son Trees .v+b Right Son Trees .lw), Represents either the sum of the largest sub segments of the left subtree starting with the left endpoint , Or it will be l w lw lw The sequence extends directly to the right subtree . Empathy r w rw rw You can do the same .
Finally obtain w = max ( max ( a Left Son Trees . w , b Right Son Trees . w ) , a Left Son Trees . r w + b Right Son Trees . l w ) w=\max(\max(a_{ The left subtree }.w,b_{ Right subtree }.w),a_{ The left subtree }.rw+b_{ Right subtree }.lw) w=max(max(a Left Son Trees .w,b Right Son Trees .w),a Left Son Trees .rw+b Right Son Trees .lw) - The update operation is similar to the naive segment tree .
- The query operation cannot be used directly w w w To compare the size , We are going to return a node instead of a value .
When the interval of a node is covered by the queried interval , Just go back to the node .
When the interval only involves one of the nodes ( Notice that it's a ) In the interval , Query the child node directly .
When two child nodes are involved , You can create a new answer node , It is responsible for storing the child answer nodes returned from the left and right child nodes , Then, according to the two sub answer nodes, the answer nodes are re p u s h u p pushup pushup At a time can be . Finally, return to the answer node . Finally, output the top answer node w w w that will do . - matters needing attention : The shortest subsequence is 1, So for leaf nodes, you need to assign all their values to the value of the position of the whole sequence .
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=5e5+5;
int a[maxn],n,m;
struct node{
int l,r;
int v,w,lw,rw;
}t[maxn<<2];
inline void pushup(int k)
{
t[k].v=t[k<<1].v+t[k<<1|1].v;
t[k].lw=max(t[k<<1].v+t[k<<1|1].lw,t[k<<1].lw);
t[k].rw=max(t[k<<1].rw+t[k<<1|1].v,t[k<<1|1].rw);
t[k].w=max(max(t[k<<1].w,t[k<<1|1].w),t[k<<1].rw+t[k<<1|1].lw);
}
inline void build(int k,int l,int r)
{
t[k].l=l,t[k].r=r;
if(l==r){
t[k].v=t[k].w=t[k].lw=t[k].rw=a[l];
return;
}
int mid=l+r>>1;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
pushup(k);
}
inline void update(int k,int x,int y)
{
int l=t[k].l,r=t[k].r;
if(l==r){
t[k].v=t[k].w=t[k].lw=t[k].rw=y;
return;
}
int mid=l+r>>1;
if(x<=mid)update(k<<1,x,y);
else update(k<<1|1,x,y);
pushup(k);
}
inline node query(int k,int l,int r)
{
int x=t[k].l,y=t[k].r;
if(l<=x&&y<=r)return t[k];
int mid=x+y>>1;
if(r<=mid)return query(k<<1,l,r);
else if(l>mid)return query(k<<1|1,l,r);
else{
node a=query(k<<1,l,r),b=query(k<<1|1,l,r),ans;
ans.v=a.v+b.v;
ans.lw=max(a.v+b.lw,a.lw);
ans.rw=max(b.v+a.rw,b.rw);
ans.w=max(max(a.w,b.w),a.rw+b.lw);
return ans;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
build(1,1,n);
for(int i=1;i<=m;i++){
int op,l,r;
cin>>op>>l>>r;
if(op==1){
if(l>r)swap(l,r);
node ans=query(1,l,r);
cout<<ans.w<<"\n";
}
else{
update(1,l,r);
}
}
return 0;
}
边栏推荐
- ES6 module
- 33、使用RGBD相机进行目标检测和深度信息输出
- NVM installation tutorial
- Wechat applet SetData dynamic variable value sorting
- Formal parameters vs actual parameters
- 基于PyTorch的生成对抗网络实战(7)——利用Pytorch搭建SGAN(Semi-Supervised GAN)生成手写数字并分类
- Use performance to see what the browser is doing
- Design of PHP asymmetric encryption algorithm (RSA) encryption mechanism
- 7.Consul服务注册与发现
- A primary multithreaded server model
猜你喜欢

ICML 2022 | LIMO: 一种快速生成靶向分子的新方法

ES6 module

Design of simple digital circuit traffic light

shell脚本详细介绍(四)

12 SQL optimization schemes summarized by old drivers (very practical)

Common operation and Principle Exploration of stream

Select tag - uses the default text as a placeholder prompt but is not considered a valid value

Included angle of 3D vector

Awk tools

Nexys A7开发板资源使用技巧
随机推荐
33. Use rgbd camera for target detection and depth information output
Basic type of typescript
Design of PHP asymmetric encryption algorithm (RSA) encryption mechanism
【Proteus仿真】Arduino UNO按键启停 + PWM 调速控制直流电机转速
A few lines of code can realize complex excel import and export. This tool class is really powerful!
Common operation and Principle Exploration of stream
MongoDB系列之Window环境部署配置
【HCSD应用开发实训营】一行代码秒上云评测文章—实验过程心得
shell脚本详细介绍(四)
Connection migration for DataGrid configuration
Create your own cross domain proxy server
Exercises under insect STL string
Traverse the specified directory to obtain the file name with the specified suffix (such as txt and INI) under the current directory
古瑞瓦特沖刺港交所上市:創下“多個第一”,獲IDG資本9億元投資
Bigint: handles large numbers (integers of any length)
Is it safe to open a securities account? Is there any danger
Detailed sorting of HW blue team traceability process
Insect operator overloaded a fun
Original code, inverse code and complement code
证券开户安全吗,有没有什么危险啊