当前位置:网站首页>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;
}
边栏推荐
- MongoDB系列之Window环境部署配置
- Embedded virlog code running process
- d检查类型是指针
- CVPR 2022文档图像分析与识别相关论文26篇汇集简介
- A collection of common tools for making we media videos
- 泰山OFFICE技术讲座:使用字体粗体的四种情形
- Wechat applet -picker component is repackaged and the disabled attribute is added -- above
- 8. Ribbon load balancing service call
- What is the use of index aliases in ES
- PHP非对称加密算法(RSA)加密机制设计
猜你喜欢

Detailed introduction to shell script (4)

MediaPipe手势(Hands)

Calculate the distance between two points (2D, 3D)

Use performance to see what the browser is doing

array

ES基于Snapshot(快照)的数据备份和还原

Included angle of 3D vector

ES中索引别名(alias)的到底有什么用

There are many contents in the widget, so it is a good scheme to support scrolling

微信小程序注册指引
随机推荐
Memory considerations under bug memory management
[proteus simulation] Arduino uno key start / stop + PWM speed control DC motor speed
Detailed introduction to shell script (4)
imagecopymerge
虫子 运算符重载的一个好玩的
Exercises under insect STL string
Create your own cross domain proxy server
Design of PHP asymmetric encryption algorithm (RSA) encryption mechanism
虫子 STL string上
古瑞瓦特沖刺港交所上市:創下“多個第一”,獲IDG資本9億元投資
7.Consul服务注册与发现
7-1 n queen problem
虫子 STL string 下 练习题
2021-10-18 character array
Stack, LIFO
爱可可AI前沿推介(6.26)
MongoDB系列之Window环境部署配置
虫子 内存管理 上
Applicable and inapplicable scenarios of mongodb series
8. Ribbon load balancing service call