小白逛公园

来源:互联网 发布:linux上海时间 编辑:程序博客网 时间:2024/04/27 16:52
描述
小新经常陪小白去公园玩,也就是所谓的遛狗啦…在小新家附近有一条“公园路”,路的一边从南到北依次排着n个公园,小白早就看花了眼,自己也不清楚该去哪些公园玩了。
一开始,小白就根据公园的风景给每个公园打了分-.-。小新为了省事,每次遛狗的时候都会事先规定一个范围,小白只可以选择第a个和第b个公园之间(包括a、b两个公园)选择连续的一些公园玩。小白当然希望选出的公园的分数总和尽量高咯。同时,由于一些公园的景观会有所改变,所以,小白的打分也可能会有一些变化。
那么,就请你来帮小白选择公园吧。

格式
输入格式
第一行,两个整数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的整数。

输出格式
小白每出去玩一次,都对应输出一行,只包含一个整数,表示小白可以选出的公园得分和的最大值。

样例输入1
5 3
1 2 -3 4 5
1 2 3
2 2 -1

1 2 3

样例输出1
2

-1


题解:这道题没做出来啊……虽然写了很长时间,但最后还是会崩栈,但前面的几个点是过了的。还是发上来吧。老规矩,解析在注释里。


Code:

#include<iostream>
#include<cstdio> 
#include<algorithm>
using namespace std;

struct note{
int sum,l,r,ans; //sum整个区间的和,l、r是必须包含左或右端点的最大子段,ans是这个区间的最大连续子段和
}shu[4000100]; //别问我为啥用shu,我觉得shu比tree字母少 

void change(int now,int left,int right,int p,int t)//now代表当前节点的编号,left,right左右边界,p是要修改的位置,t是要修改的值 
{
if (left==right && left==p) 
{
shu[now].l=shu[now].r=shu[now].ans=shu[now].sum=t;
return; 
        }
    
       int m=(left+right)>>1;
       if (p<=m) change(now<<1,left,m,p,t);
         else change((now<<1)+1,m+1,right,p,t); //修改操作 
    
       //维护 
       shu[now].sum=shu[now<<1].sum+shu[(now<<1)+1].sum;
       shu[now].l=max(shu[now<<1].l,shu[now<<1].sum+shu[(now<<1)+1].l);
       shu[now].r=max(shu[now<<1].r+shu[(now<<1)+1].sum,shu[(now<<1)+1].r);
       shu[now].ans=max(shu[now<<1].r+shu[(now<<1)+1].l,max(shu[now<<1].ans,shu[(now<<1)+1].ans)); 



note ask(int now,int left,int right,int a,int b)
{
        if (a<=left && b>=right) return shu[now];

int m=(left+right)>>1;
if (b<=m) return ask(now<<1,left,m,a,b);
if (a>m) return ask((now<<1)+1,m+1,right,a,b);

note ansl,ansr,ansz;//答案左,答案右,答案总
ansl=ask(now<<1,left,m,a,b); 
ansr=ask((now<<1)+1,m+1,right,a,b);
ansz.sum=ansl.sum+ansr.sum;
ansz.l=max(ansl.l,ansl.sum+ansr.l);
ansz.r=max(ansr.r,ansr.sum+ansl.r);
ansz.ans=max(ansl.r+ansr.l,max(ansl.ans,ansr.ans));
                return ansz;
}


int main()
{
int n,m;
scanf("%d%d",&n,&m);
for (int i=1; i<=n; i++) 
{
int t;
scanf("%d",&t);
change(1,1,n,i,t);//建树 
}

for (int i=1; i<=m; i++)
{
int k,a,b;
scanf("%d%d%d",&k,&a,&b);
if (k==2) change(1,1,n,a,b);
if (k==1) 
{
if (a>b) { int t=a; a=b; b=t; }
note output=ask(1,1,n,a,b); 
printf("%d\n",output.ans);
}
}
return 0; 
}

0 0
原创粉丝点击