当前位置:网站首页>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 - bind and prevent event bubble catch
- character constants
- Gurivat sprint Harbour Exchange listed: created “multiple first”, received 900 million yuan Investment from IDG capital
- GC is not used in D
- 【MySQL从入门到精通】【高级篇】(二)MySQL目录结构与表在文件系统中的表示
- MySQL configuration improves data insertion efficiency
- 7-3 minimum toll
- 虫子 类和对象 上
- Stack, LIFO
- 12 SQL optimization schemes summarized by old drivers (very practical)
猜你喜欢

Awk tools

ICML 2022 | limo: a new method for rapid generation of targeted molecules

Es sauvegarde et restauration des données par instantané

Reprint - easy to use wechat applet UI component library

Network remote access using raspberry pie

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

Create your own cross domain proxy server

创建一个自己的跨域代理服务器

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

Input text to automatically generate images. It's fun!
随机推荐
GO语言-管道channel
In insect classes and objects
Lamp compilation and installation
HW蓝队溯源流程详细整理
Range of types
CVPR 2022文档图像分析与识别相关论文26篇汇集简介
On insect classes and objects
PHP非对称加密算法(RSA)加密机制设计
8.Ribbon负载均衡服务调用
Variable declaration of typescript
Gurivat sprint Harbour Exchange listed: created “multiple first”, received 900 million yuan Investment from IDG capital
Stream常用操作以及原理探索
Is expression of D
Nexys A7开发板资源使用技巧
Embedded virlog code running process
A primary multithreaded server model
Wechat applet - bind and prevent event bubble catch
7.Consul服务注册与发现
ES中索引别名(alias)的到底有什么用
古瑞瓦特沖刺港交所上市:創下“多個第一”,獲IDG資本9億元投資