当前位置:网站首页>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;
}
边栏推荐
- Wechat applet magic bug - choose to replace the token instead of clearing the token, wx Getstoragesync will take the old token value instead of the new token value
- Exercise set 1
- 虫子 STL string 下 练习题
- 【系统分析师之路】第十五章 复盘数据库系统(数据库案例分析)
- Exercises under insect STL string
- Bigint: handles large numbers (integers of any length)
- A collection of common tools for making we media videos
- 嵌入式virlog代码运行流程
- Pytorch based generation countermeasure Network Practice (7) -- using pytorch to build SGAN (semi supervised GaN) to generate handwritten digits and classify them
- Nlp-d60-nlp competition D29
猜你喜欢

I met the problem of concurrent programming in an interview: how to safely interrupt a running thread

Reprint - easy to use wechat applet UI component library

MediaPipe手势(Hands)

古瑞瓦特沖刺港交所上市:創下“多個第一”,獲IDG資本9億元投資

网络远程访问的方式使用树莓派

ES6 module
![[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

7.Consul服务注册与发现

Gurivat sprint Harbour Exchange listed: created “multiple first”, received 900 million yuan Investment from IDG capital

Included angle of 3D vector
随机推荐
[path of system analyst] Chapter 15 double disk database system (database case analysis)
Nlp-d60-nlp competition D29
CloudCompare——泊松重建
GO语言-管道channel
DataGrip配置的连接迁移
mysql配置提高数据插入效率
去某东面试遇到并发编程问题:如何安全地中断一个正在运行的线程
shell脚本详细介绍(四)
NVM installation tutorial
8.Ribbon负载均衡服务调用
MongoDB系列之适用场景和不适用场景
ICML 2022 | LIMO: 一种快速生成靶向分子的新方法
虫子 STL string 下 练习题
虫子 类和对象 中
Here document interaction free and expect automatic interaction
Wechat applet -picker component is repackaged and the disabled attribute is added -- above
Use performance to see what the browser is doing
Teacher Li Hang's new book "machine learning methods" is on the market! Purchase link attached
Common operation and Principle Exploration of stream
A primary multithreaded server model