Vijos 1083:小白逛公园 (bzoj 1756)

Description

小新经常陪小白去公园玩,也就是所谓的遛狗啦…在小新家附近有一条“公园路”,路的一边从南到北依次排着n个公园,小白早就看花了眼,自己也不清楚该去哪些公园玩了。   一开始,小白就根据公园的风景给每个公园打了分-.-。小新为了省事,每次遛狗的时候都会事先规定一个范围,小白只可以选择第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的整数。

Output

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

Sample Input

5 3
1 2 -3 4 5
1 2 3
2 2 -1
1 2 3

Sample Output

2

-1


一道线段树的题目...维护左端最大,右端最大,区间最大和区间和。合并的时候想一下就好了。具体看程序


看题目那么水于是我开始做,然后就一直WA。不得已去翻题解,然后发现题目中没有规定a<b,这种地方弄个坑真是没意思...

#include<cstdio>
using namespace std;
struct tree
{
     int l,r;
     int sum;
     int lm,rm,m;
}tr[2000001];
int pl[500001];
int a[500001];
inline int max(int x,int y)
{
     if(x>y)
          return x;
     return y;
}
inline void update(int p)
{
     tr[p].m=max(max(tr[p*2].m,tr[p*2+1].m),tr[p*2].rm+tr[p*2+1].lm);
     tr[p].lm=max(tr[p*2].lm,tr[p*2].sum+tr[p*2+1].lm);
     tr[p].rm=max(tr[p*2+1].rm,tr[p*2+1].sum+tr[p*2].rm);
     tr[p].sum=tr[p*2].sum+tr[p*2+1].sum;
}
inline void build(int p,int l,int r)
{
     tr[p].l=l;
     tr[p].r=r;
     if(l!=r)
     {
          int mid=(l+r)/2;
          build(p*2,l,mid);
          build(p*2+1,mid+1,r);
          update(p);
     }
     else
     {
          tr[p].m=a[l];
          tr[p].lm=a[l];
          tr[p].rm=a[l];
          tr[p].sum=a[l];
          pl[l]=p;
     }
}
inline void change(int p,int d,int x)
{
	 if(p==0)
	      return ;
     if(p==d)
     {
          tr[p].m=x;
          tr[p].lm=x;
          tr[p].rm=x;
          tr[p].sum=x;
          change(p/2,d,x);
     }
     else
     {
          update(p);
          change(p/2,d,x);
     }
}
tree ne;
inline tree ask(int p,int l,int r)
{
     if(l<=tr[p].l&&tr[p].r<=r)
          return tr[p];
     else
     {
          int mid=(tr[p].l+tr[p].r)/2;
          tree x1,x2,x;
          x1=ne;
          x2=ne;
          x=ne;
          bool f1=false,f2=false;
          if(l<=mid)
          {
               x1=ask(p*2,l,r);
               f1=true;
          }
          if(r>mid)
          {
               x2=ask(p*2+1,l,r);
               f2=true;
          }
          if(f1)
          {
               if(f2)
               {
                    x.m=max(max(x1.m,x2.m),x1.rm+x2.lm);
                    x.lm=max(x1.lm,x1.sum+x2.lm);
                    x.rm=max(x2.rm,x2.sum+x1.rm);
                    x.sum=x1.sum+x2.sum;
               }
               else
                    x=x1;
          }
          else if(f2)
               x=x2;
          return x;
     }
}
int main()
{
     int n,m;
     scanf("%d%d",&n,&m);
     int i;
     for(i=1;i<=n;i++)
          scanf("%d",&a[i]);
     build(1,1,n);
     int s,t,k;
     for(i=1;i<=m;i++)
     {
          scanf("%d%d%d",&k,&s,&t);
          if(k==1)
          {
               if(s>t)
               {
                    int tt=s;
                    s=t;
                    t=tt;
               }
               printf("%d\n",ask(1,s,t).m);
          }
          else
               change(pl[s],pl[s],t);
     }
     return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值