https://www.luogu.org/problem/SP1716
题意翻译
nnn 个数,qqq 次操作
操作0 x y把AxA_xAx 修改为yyy
操作1 l r询问区间[l,r][l, r][l,r] 的最大子段和
感谢 @Edgration 提供的翻译
题目描述
You are given a sequence A of N (N <= 50000) integers between -10000 and 10000. On this sequence you have to apply M (M <= 50000) operations:
modify the i-th element in the sequence or for given x y print max{Ai + Ai+1 + … + Aj | x<=i<=j<=y }.
输入格式
The first line of input contains an integer N. The following line contains N integers, representing the sequence A1…AN.
The third line contains an integer M. The next M lines contain the operations in following form:
0 x y: modify Ax into y (|y|<=10000).
1 x y: print max{Ai + Ai+1 + … + Aj | x<=i<=j<=y }.
输出格式
For each query, print an integer as the problem required.
输入输出样例
输入 #1
4
1 2 3 4
4
1 1 3
0 3 -3
1 2 4
1 3 3
输出 #1
6
4
-3
思路: sum表示区间和,begl表示从区间左端点开始的最大连续子段和,begr表示从区间右端点开始的最大连续子段和,ans表示该区间的最大连续子段和。在合并左右区间时的操作: r o o t . s u m = l c . s u m + r c . s u m root.sum=lc.sum+rc.sum root.sum=lc.sum+rc.sum r o o t . b e g l = m a x ( l c . b e g l , l c . s u m + r c . b e g l ) root.begl=max(lc.begl,lc.sum+rc.begl) root.begl=max(lc.begl,lc.sum+rc.begl) r o o t . b e g r = m a x ( r c . b e g r , r c . s u m + l c . b e g r ) root.begr=max(rc.begr,rc.sum+lc.begr) root.begr=max(rc.begr,rc.sum+lc.begr) r o o t . a n s = m a x ( l c . a n s , r c . a n s , l c . b e g r + r c . b e g l ) root.ans=max(lc.ans,rc.ans,lc.begr+rc.begl) root.ans=max(lc.ans,rc.ans,lc.begr+rc.begl)
维护这些值就完事儿了。
#include
#include
#include
#include
#include
using namespace std;
const int maxn=5e4+5;
struct node
{
int l,r,sum,begl,begr,ans;
}tree[maxn<<2];
int n,m;
inline void up(int i)
{
int l=i<<1,r=i<<1|1;
tree[i].sum=tree[l].sum+tree[r].sum;
tree[i].begl=max(tree[l].begl,tree[l].sum+tree[r].begl);
tree[i].begr=max(tree[r].begr,tree[r].sum+tree[l].begr);
tree[i].ans=max(tree[l].begr+tree[r].begl,max(tree[l].ans,tree[r].ans));
}
inline void build(int i,int l,int r)
{
tree[i].l=l,tree[i].r=r;
if(l==r)
{
scanf("%d",&tree[i].sum);
tree[i].ans=tree[i].begl=tree[i].begr=tree[i].sum;
return ;
}
int mid=(l+r)>>1;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
up(i);
}
void update(int i,int p,int v)
{
if(tree[i].l==tree[i].r)
{
tree[i].ans=tree[i].sum=tree[i].begl=tree[i].begr=v;
return ;
}
int mid=(tree[i].l+tree[i].r)>>1;
if(p<=mid)
update(i<<1,p,v);
else
update(i<<1|1,p,v);
up(i);
}
node query(int i,int l,int r)
{
if(l==tree[i].l&&r==tree[i].r)
return tree[i];
int mid=(tree[i].l+tree[i].r)>>1;
if(r<=mid)
return query(i<<1,l,r);
else if(l>mid)
return query(i<<1|1,l,r);
else
{
node lc=query(i<<1,l,mid);
node rc=query(i<<1|1,mid+1,r);
node tmp;
tmp.sum=lc.sum+rc.sum;
tmp.begl=max(lc.begl,lc.sum+rc.begl);
tmp.begr=max(rc.begr,rc.sum+lc.begr);
tmp.ans=max(lc.begr+rc.begl,max(lc.ans,rc.ans));
return tmp;
}
}
inline void prework()
{
scanf("%d",&n);
build(1,1,n);
}
inline void mainwork()
{
scanf("%d",&m);
int op,u,v;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&op,&u,&v);
if(!op)
update(1,u,v);
else
printf("%d\n",query(1,u,v).ans);
}
}
int main()
{
prework();
mainwork();
return 0;
}