小白逛线性公园
Description
小新经常陪小白去公园玩,也就是所谓的遛狗啦…在小新家附近有一条“公园路”,路的一边从南到北依次排着n个公园,小白早就看花了眼,自己也不清楚该去哪些公园玩了。
一开始,小白就根据公园的风景给每个公园打了分-.-。小新为了省事,每次遛狗的时候都会事先规定一个范围,小白只可以选择第a个和第b个公园之间(包括a、b两个公园)选择连续的一些公园玩。小白当然希望选出的公园的分数总和尽量高咯。同时,由于一些公园的景观会有所改变,所以,小白的打分也可能会有一些变化。
那么,就请你来帮小白选择公园吧。
一开始,小白就根据公园的风景给每个公园打了分-.-。小新为了省事,每次遛狗的时候都会事先规定一个范围,小白只可以选择第a个和第b个公园之间(包括a、b两个公园)选择连续的一些公园玩。小白当然希望选出的公园的分数总和尽量高咯。同时,由于一些公园的景观会有所改变,所以,小白的打分也可能会有一些变化。
那么,就请你来帮小白选择公园吧。
Input
第一行,两个整数N和M,分别表示表示公园的数量和操作(遛狗或者改变打分)总数。
接下来N行,每行一个整数,依次给出小白开始时对公园的打分。
接下来M行,每行三个整数。第一个整数K,1或2。K=1表示,小新要带小白出去玩,接下来的两个整数a和b给出了选择公园的范围(1≤a,b≤N);K=2表示,小白改变了对某个公园的打分,接下来的两个整数p和s,表示小白对第p个公园的打分变成了s(1≤p≤N)。 其中,1≤N≤500 000,1≤M≤100 000,所有打分都是绝对值不超过1000的整数。
接下来N行,每行一个整数,依次给出小白开始时对公园的打分。
接下来M行,每行三个整数。第一个整数K,1或2。K=1表示,小新要带小白出去玩,接下来的两个整数a和b给出了选择公园的范围(1≤a,b≤N);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
Hint
【数据范围】
对于30% 的数据,满足1≤N≤5000,1≤M≤10000
对于100% 的数据,满足1≤N≤500000,1≤M≤100000
对于30% 的数据,满足1≤N≤5000,1≤M≤10000
对于100% 的数据,满足1≤N≤500000,1≤M≤100000
Solution
简化维护数列。(其实是懒得再写一个线段树。。。)
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
inline int read(){
char c;int rec=0,f=1;
while((c=getchar())<'0'||c>'9')if(c=='-')f=-1;
while(c>='0'&&c<='9')rec=rec*10+c-'0',c=getchar();
return rec*f;
}
int cnt,root,c[500005];
int n,m,V;
struct Splay_Tree{
int F,s[2],val,size;
int sum,maxx,pmax[2];
inline void NewNode(int fa,int x){
F=fa;val=sum=maxx=pmax[0]=pmax[1]=x;size=1;return ;
}
}tree[500005];
inline void PMAX(int v,int f){
tree[v].pmax[f]=max(tree[tree[v].s[f]].pmax[f],
tree[tree[v].s[f]].sum+tree[v].val+max(0,tree[tree[v].s[!f]].pmax[f]));
}
inline void Pushup(int v){
tree[v].size=tree[tree[v].s[0]].size+1+tree[tree[v].s[1]].size;
tree[v].sum=tree[tree[v].s[0]].sum+tree[v].val+tree[tree[v].s[1]].sum;
tree[v].maxx=max(max(tree[tree[v].s[0]].maxx,tree[tree[v].s[1]].maxx),
max(0,tree[tree[v].s[0]].pmax[1])+tree[v].val+max(0,tree[tree[v].s[1]].pmax[0]));
PMAX(v,0);PMAX(v,1);return ;
}
inline void Rotate(int v){
int p=tree[v].F,g=tree[p].F;
int f1=(v==tree[p].s[1]),f2=(p==tree[g].s[1]),S=tree[v].s[!f1];
tree[g].s[f2]=v;tree[v].F=g;
tree[p].s[f1]=S;tree[S].F=p;
tree[v].s[!f1]=p;tree[p].F=v;
Pushup(p);return ;
}
inline void Splay(int v,int goal){
while(tree[v].F!=goal){
int p=tree[v].F,g=tree[p].F;
if(g!=goal)(v==tree[p].s[1])^(p==tree[g].s[1])?Rotate(v):Rotate(p);
Rotate(v);
}Pushup(v);if(!goal)root=v;
return ;
}
inline int Build(int L,int R,int fa){
if(L>R)return 0;
int mid=(L+R)>>1,p=++cnt;
tree[p].NewNode(fa,c[mid]);
tree[p].s[0]=Build(L,mid-1,p);
tree[p].s[1]=Build(mid+1,R,p);
Pushup(p);return p;
}
inline void Kth(int k){
int v=root;
while(tree[tree[v].s[0]].size+1!=k){
if(tree[tree[v].s[0]].size+1>k)v=tree[v].s[0];
else {k-=tree[tree[v].s[0]].size+1;v=tree[v].s[1];}
}V=v;return ;
}
inline void Pre(int x,int y){Kth(x);Splay(V,0);Kth(y);Splay(V,root);return ;}
inline void Change(int x,int y){Pre(x-1,x+1);tree[tree[V].s[0]].NewNode(V,y);Pushup(V);Pushup(root);return ;}
inline void Ask(int x,int y){Pre(x-1,y+1);cout<<tree[tree[V].s[0]].maxx<<'\n';return ;}
inline void debug(int v){
if(v==0)return ;
debug(tree[v].s[0]);
cout<<tree[v].val<<" ";
debug(tree[v].s[1]);
return ;
}
int main(){
tree[0].maxx=tree[0].pmax[0]=tree[0].pmax[1]=-0x3f3f3f3f;
n=read();m=read();
int i,f,x,y;
for(i=1;i<=n;i++)c[i]=read();
tree[++cnt].NewNode(0,-0x3f3f3f3f);
tree[++cnt].NewNode(1,-0x3f3f3f3f);
tree[1].s[1]=2;root=1;
tree[2].s[0]=Build(1,n,2);
for(i=1;i<=m;i++){
f=read();
if(f==1){
x=read();y=read();x++;y++;
if(x>y)swap(x,y);
Ask(x,y);
}
if(f==2){
x=read();y=read();x++;
Change(x,y);
}
}
return 0;
}