SPOJ-GSS3 Can you answer these queries III(线段树)

Can you answer these queries III

题目链接

You are given a sequence A of N N N ( N ≤ 50000 N \leq 50000 N50000) integers between − 10000 -10000 10000 and 10000 10000 10000. On this sequence you have to apply M M M ( M ≤ 50000 M \leq 50000 M50000) operations:
modify the i-th element in the sequence or for given x x x y y y print max{ A i + A i + 1 + . . + A j A_i + A_{i+1} + .. + A_j Ai+Ai+1+..+Aj ∣ | x ≤ i ≤ j ≤ y x\leq i\leq j\leq y xijy }.

Input

The first line of input contains an integer N N N. The following line contains N N N integers, representing the sequence A 1 . . A N A_1..A_N A1..AN.
The third line contains an integer M M M. The next M M M lines contain the operations in following form:
0 0 0 x x x y y y: modify A x A_x Ax into y ( ∣ y ∣ |y| y<= 10000 10000 10000).
1 1 1 x x x y y y: print max{ A i + A i + 1 + . . + A j A_i + A_i+1 + .. + A_j Ai+Ai+1+..+Aj | x ≤ i ≤ j ≤ y x\leq i\leq j\leq y xijy }.

Output

For each query, print an integer as the problem required.

Example

Input:

4
1 2 3 4
4
1 1 3
0 3 -3
1 2 4
1 3 3

Output:

6
4
-3

思路

除了 l l l r r r之外,还需保存四个量: s u m , d a t , l m a x , r m a x sum,dat,lmax,rmax sum,dat,lmax,rmax

更新方法如下:

t[p].sum=t[p*2].sum+t[p*2+1].sum; t[p].lmax=max(t[p*2].lmax,t[p*2].sum+t[p*2+1].lmax); t[p].rmax=max(t[p*2+1].rmax,t[p*2+1].sum+t[p*2].rmax); t[p].dat=max(max(t[p*2].dat,t[p*2+1].dat),t[p*2].rmax+t[p*2+1].lmax);

可以把更新的代码单独写到一个 u p d a t e update update函数中,增加代码复用。

一个坑点是,查询时 l l l可能大于 r r r

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=5e4+10;
struct T{int l,r,dat,sum,lmax,rmax;}t[maxn*4];
int n,m,a[maxn],op,x,y;

void update(T &c,T &a,T &b){
    c.sum=a.sum+b.sum;
    c.lmax=max(a.lmax,a.sum+b.lmax);
    c.rmax=max(b.rmax,b.sum+a.rmax);
    c.dat=max(max(a.dat,b.dat),a.rmax+b.lmax);
}

void build(int p,int l,int r){
    t[p].l=l,t[p].r=r;
    if(l==r){t[p].sum=t[p].dat=t[p].lmax=t[p].rmax=a[l];return;}
    int mid=(l+r)/2;
    build(p*2,l,mid),build(p*2+1,mid+1,r);
    update(t[p],t[p*2],t[p*2+1]);
}

void change(int p,int x,int y){
    if(t[p].l==t[p].r){t[p].sum=t[p].dat=t[p].lmax=t[p].rmax=y;return;}
    int mid=(t[p].l+t[p].r)/2;
    if(x<=mid)  change(p*2,x,y);
    else    change(p*2+1,x,y);
    update(t[p],t[p*2],t[p*2+1]);
}

T ask(int p,int l,int r){
    if(l>r) swap(l,r);  //注意
    if(l<=t[p].l&&r>=t[p].r)    return t[p];
    int mid=(t[p].l+t[p].r)/2;
    if(r<=mid)  return ask(p*2,l,r);
    else if(l>=mid+1)   return ask(p*2+1,l,r);
    else{
        T a,b,c;
        a=ask(p*2,l,r),b=ask(p*2+1,l,r);
        update(c,a,b);
        return c;
    }
}

int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)   scanf("%d",&a[i]);
    build(1,1,n);
    scanf("%d",&m);
    while(m--){
        scanf("%d%d%d",&op,&x,&y);
        if(op==0)   change(1,x,y);
        else    printf("%d\n",ask(1,x,y).dat);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_51864047

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值