当前位置:网站首页>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;
}
边栏推荐
- character constants
- A few lines of code can realize complex excel import and export. This tool class is really powerful!
- hands-on-data-analysis 第三单元 模型搭建和评估
- 7-1 n queen problem
- HW蓝队溯源流程详细整理
- 【MySQL从入门到精通】【高级篇】(二)MySQL目录结构与表在文件系统中的表示
- 33. Use rgbd camera for target detection and depth information output
- MongoDB系列之Window环境部署配置
- VTK 圆柱体的生成与渲染
- In insect classes and objects
猜你喜欢

Tips for using nexys A7 development board resources

Mediapipe gestures (hands)

AGCO AI frontier promotion (6.26)

计算两点之间的距离(二维、三维)

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

Es snapshot based data backup and restore

Thinking caused by the error < note: candidate expectations 1 argument, 0 provided >

Variable declaration of typescript
![[MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system](/img/03/a1885e4740bbfdbdee2446e3dd81d0.png)
[MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system

Create your own cross domain proxy server
随机推荐
Aesthetic experience (episode 238) Luo Guozheng
【MySQL从入门到精通】【高级篇】(二)MySQL目录结构与表在文件系统中的表示
Es snapshot based data backup and restore
Is expression of D
Guruiwat rushed to the Hong Kong stock exchange for listing: set "multiple firsts" and obtained an investment of 900million yuan from IDG capital
三维向量的夹角
Firewall introduction
2021-10-09
Insect operator overloaded a fun
awk工具
网络远程访问的方式使用树莓派
Stack, LIFO
同花顺股票开户选哪个证券公司是比较好,比较安全的
Thinking caused by the error < note: candidate expectations 1 argument, 0 provided >
PHP非对称加密算法(RSA)加密机制设计
Gurivat sprint Harbour Exchange listed: created “multiple first”, received 900 million yuan Investment from IDG capital
MySQL configuration improves data insertion efficiency
Exercise set 1
CVPR 2022文档图像分析与识别相关论文26篇汇集简介
Es6: iterator