SP1716 GSS3 - Can you answer these queries III (sub-segment interval and the maximum single-point modification +)

The meaning of problems

Given number n, q operations, two operations of: A X into y, find [l, r], and the maximum sub-segment.

n,m<=50000,-10000<=ai<=10000

answer

Range of thought with tree line maintenance, consider how to merge the interval.

When we find between the two largest sub-segment and cell, then the largest sub-segment large range and may be one of these two, it could be part of the middle two sections stitching, which is the biggest part of the right side of the left section the largest part of the composition of the left and right sections.

So we also need to record the interval and immediately to the right against the left side of the largest sub-segment and is now considered the largest sub-segment of the left maintenance and lmax, in the range of merger, lmax lmax large interval may be left section, it may be left whole interval plus the right section lmax. rmax empathy.

Pass the time interval query structure to merge information.

 

#include <cstdio> 
#include <CString>
 the using  namespace STD; 

#define LS << RT. 1
 #define RS RT. 1 << |. 1
 #define LL Long Long
 const  int MAXN = 50005 ;
 int n-, m;
 int A [MAXN ];
 struct CX { 
  LL SUM, DAT, Lmax, Rmax of; // interval and, the maximum interval and sub-segment, sub-segment, and the maximum left, right and the maximum sub-segment 
} T [MAXN << 2 ]; 

LL max (X LL , Y LL) { return X> Y? X: Y;} 

void  GET (CX & RET, CX LX, CX Ry) { 
  ret.sum = lx.sum + ry.sum; 
  ret.lmax=max(lx.lmax,lx.sum+ry.lmax);
  ret.rmax=max(ry.rmax,ry.sum+lx.rmax);
  ret.dat=max(max(lx.dat,ry.dat),lx.rmax+ry.lmax);
}

void build(int rt,int l,int r){
  if(l==r){
    t[rt]=(cx){a[l],a[l],a[l],a[l]};
    return ;
  }
  int mid=(l+r)>>1;
  build(ls,l,mid);
  build(rs,mid+1,r);
  get(t[rt],t[ls],t[rs]);
}

cx query(int rt,int l,int r,int a_l,int a_r){
  //printf("%d %d %d %d \n",l,r,a_l,a_r);
  if(a_l<=l&&r<=a_r) return t[rt];
  int mid=(l+r)>>1;
  if(a_r<=mid) return query(ls,l,mid,a_l,a_r);
  if(mid<a_l) return query(rs,mid+1,r,a_l,a_r);
  cx ret,x,y;
  x=query(ls,l,mid,a_l,a_r);
  y=query(rs,mid+1,r,a_l,a_r);
  get(ret,x,y);
  return ret;
}

void modify(int rt,int l,int r,int pos,int val){
  if(l==r){
    t[rt]=(cx){val,val,val,val};
    return ;
  }
  int mid=(l+r)>>1;
  if(pos<=mid) modify(ls,l,mid,pos,val);
  else modify(rs,mid+1,r,pos,val);
  get(t[rt],t[ls],t[rs]);
}

int main(){
  scanf("%d",&n);
  for(int i=1;i<=n;i++) scanf("%d",&a[i]);
  build(1,1,n);
  scanf("%d",&m);
  for(int i=1;i<=m;i++){
    int opt;
    scanf("%d",&opt);
    if(opt){
      int l,r;
      scanf("%d%d",&l,&r);
      printf("%lld\n",query(1,1,n,l,r).dat);
    }
    else {
      int pos,val;
      scanf("%d%d",&pos,&val);
      modify(1,1,n,pos,val);
    }
  }
}
View Code

 

Guess you like

Origin www.cnblogs.com/sto324/p/11297438.html