Description
小新经常陪小白去公园玩,也就是所谓的遛狗啦…在小新家附近有一条“公园路”,路的一边从南到北依次排着n个公园,小白早就看花了眼,自己也不清楚该去哪些公园玩了。
一开始,小白就根据公园的风景给每个公园打了分-.-。小新为了省事,每次遛狗的时候都会事先规定一个范围,小白只可以选择第a个和第b个公园之间(包括a、b两个公园)选择连续的一些公园玩。小白当然希望选出的公园的分数总和尽量高咯。同时,由于一些公园的景观会有所改变,所以,小白的打分也可能会有一些变化。
那么,就请你来帮小白选择公园吧。
Input
第一行,两个整数N和M,分别表示表示公园的数量和操作(遛狗或者改变打分)总数。
接下来N行,每行一个整数,依次给出小白 开始时对公园的打分。
接下来M行,每行三个整数。第一个整数K,1或2。K=1表示,小新要带小白出去玩,接下来的两个整数a和b给出了选择公园的范围(1≤a,b≤N, a可以大于b!);K=2表示,小白改变了对某个公园的打分,接下来的两个整数p和s,表示小白对第p个公园的打分变成了s(1≤p≤N)。
其中,1≤N≤500 000,1≤M≤100 000,所有打分都是绝对值不超过1000的整数。
Output
小白每出去玩一次,都对应输出一行,只包含一个整数,表示小白可以选出的公园得分和的最大值。
Sample Input
5 3
1 2 -3 4 5
1 2 3
2 2 -1
1 2 3
Sample Output
2
-1
Key To Problem
和山海经神似,只是加了一个单点修改。
CODE
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 500010
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ls rt<<1
#define rs rt<<1|1
using namespace std;
struct node
{
int cnt,lcnt,rcnt;
};
node f[N<<2];
int sum[N<<2];
int n,m;
void PushUp(node &a,node &b,node &s,int rt)
{
sum[rt]=sum[ls]+sum[rs];
s.lcnt=max(a.lcnt,sum[ls]+b.lcnt);
s.cnt=max(a.rcnt+b.lcnt,max(a.cnt,b.cnt));
s.rcnt=max(b.rcnt,sum[rs]+a.rcnt);
}
void build(int l,int r,int rt)
{
if(l==r)
{
scanf("%d",&sum[rt]);
f[rt].cnt=f[rt].lcnt=f[rt].rcnt=sum[rt];
return ;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
PushUp(f[ls],f[rs],f[rt],rt);
}
void fix(int u,int c,int l,int r,int rt)
{
if(l==r)
{
sum[rt]=c;
f[rt].cnt=f[rt].lcnt=f[rt].rcnt=sum[rt];
return ;
}
int mid=(l+r)>>1;
if(mid>=u)
fix(u,c,lson);
else
fix(u,c,rson);
PushUp(f[ls],f[rs],f[rt],rt);
}
node query(int L,int R,int l,int r,int rt)
{
if(l>=L&&r<=R)
return f[rt];
int mid=(l+r)>>1;
node a,b,s;
if(mid>=L)
a=query(L,R,lson);
if(mid<R)
b=query(L,R,rson);
if(mid>=R)
return a;
if(mid<L)
return b;
PushUp(a,b,s,rt);
return s;
}
int main()
{
scanf("%d%d",&n,&m);
build(1,n,1);
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
if(x==1)
{
if(y>z)
swap(y,z);
node ans=query(y,z,1,n,1);
printf("%d\n",ans.cnt);
}else
fix(y,z,1,n,1);
}
return 0;
}