Dynamic maximum sub-segments and

Source: Internet
Author: User

#include <bits/stdc++.h>using namespace Std;const int Maxn=5e5+10;inline int read () {int X=0,f=1;char ch; Do{ch=getchar (); if (ch== '-') f=-1;}    while (!isdigit (CH)); do{x=x*10+ch-' 0 '; Ch=getchar ();}    while (IsDigit (CH)); return f*x;}    int n,m,a[maxn];struct node{#define Lson t[k].lc #define Rson t[k].rc int LC,RC; int Sum,lmax,rmax,dat;}    T[maxn<<1];int tot=1;inline void pushup (int k) {t[k].sum=t[lson].sum+t[rson].sum;    T[k].lmax=max (T[lson].lmax,t[lson].sum+t[rson].lmax);    T[k].rmax=max (T[rson].rmax,t[rson].sum+t[lson].rmax); T[k].dat=max (Max (T[lson].dat,t[rson].dat), T[lson].rmax+t[rson].lmax);}        void build (int k,int l,int R) {if (l==r) {t[k].sum=t[k].lmax=t[k].rmax=t[k].dat=a[l];    Return    } int mid=l+r>>1;    Lson=++tot,build (Lson,l,mid);    Rson=++tot,build (RSON,MID+1,R); Pushup (k);}        void Modify (int k,int l,int r,int pos,int val) {if (l==r) {t[k].sum=t[k].lmax=t[k].rmax=t[k].dat=val;    Return } int mid=l+r>>1;    if (pos<=mid) modify (Lson,l,mid,pos,val);    else modify (Rson,mid+1,r,pos,val); Pushup (k);}    node query (int k,int l,int r,int x,int y) {if (l==x&&r==y) return t[k];    int mid=l+r>>1;    if (y<=mid) return query (lson,l,mid,x,y);    else if (x>mid) return query (rson,mid+1,r,x,y);        else{node Ls=query (lson,l,mid,x,mid);        Node Rs=query (rson,mid+1,r,mid+1,y);        node res;        Res.sum=ls.sum+rs.sum;        Res.lmax=max (Ls.lmax,ls.sum+rs.lmax);        Res.rmax=max (Rs.rmax,rs.sum+ls.rmax);        Res.dat=max (Max (Ls.dat,rs.dat), Ls.rmax+rs.lmax);    return res;    }}void Read_and_parse () {n=read (), M=read ();    for (int i=1;i<=n;i++) a[i]=read (); Build (1,1,n);}        void Solve () {while (m--) {int k=read (), X=read (), Y=read ();            if (k==1) {if (x>y) swap (x, y);        printf ("%d\n", Query (1,1,n,x,y). dat);    }else Modify (1,1,n,x,y);    }}int Main () {read_and_parse ();    Solve (); return 0;}

Dynamic maximum child segments and

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.