HDU 5316 segment tree interval maximum value problem

Source: Internet
Author: User

T-Group data

n Numbers, M operations

Op=0: Find the maximum ' value ' in the L-r interval

Op=1: Change the number of a position to B

Definition of maximum ' value ': the maximal sub-sequence within the interval needs to be guaranteed that the subscript of the sub-sequence is an odd-even alternating


Each interval is recorded with a segment tree, respectively.

EE: The maximum and the end of even subscript starting with even subscript

EO: The most large and the end of an odd subscript starting with even subscript

OE: The most large and even subscript ends with an odd subscript

OO: The most large and large number of subscript starts with odd subscript end


The maximum number of 4 cases to be processed per query

Modify a single point of update can


#include "stdio.h" #include "string.h" const __int64 inf=0x3f3f3f3f3f3f3f3f;__int64 ans,a[100100];struct node{int l,r; __int64 Ee,eo,oe,oo;} Data[400010];__int64 Max (__int64 A,__int64 b) {if (a<b) return b;else return A;    void pushup (int k) {Data[k].ee=max (-inf,max (data[k*2].ee+data[k*2+1].oe,data[k*2].eo+data[k*2+1].ee));    Data[k].ee=max (data[k].ee,data[k*2].ee);    Data[k].ee=max (data[k].ee,data[k*2+1].ee);    Data[k].oo=max (-inf,max (data[k*2].oo+data[k*2+1].eo,data[k*2].oe+data[k*2+1].oo));    Data[k].oo=max (data[k].oo,data[k*2].oo);    Data[k].oo=max (data[k].oo,data[k*2+1].oo);    Data[k].eo=max (-inf,max (Data[k*2].ee+data[k*2+1].oo,data[k*2].eo+data[k*2+1].eo));    Data[k].eo=max (Data[k].eo,data[k*2].eo);    Data[k].eo=max (Data[k].eo,data[k*2+1].eo);    Data[k].oe=max (-inf,max (Data[k*2].oo+data[k*2+1].ee,data[k*2].oe+data[k*2+1].oe));    Data[k].oe=max (Data[k].oe,data[k*2].oe); Data[k].oe=max (Data[k].oe,data[k*2+1].oe);}   void build (int l,int r,int k) {int mid; Data[k].l=l;    Data[k].r=r;            if (l==r) {if (l%2==1) {data[k].oo=a[l];        Data[k].oe=data[k].eo=data[k].ee=-inf;            } else {data[k].ee=a[l];        Data[k].oe=data[k].eo=data[k].oo=-inf;    } return;    } mid= (L+r)/2;    Build (L,MID,K*2);    Build (mid+1,r,k*2+1); Pushup (k);}            void Updata (int n,int x,int k) {if (data[k].l==n && data[k].r==n) {if (data[k].l%2==1) {            Data[k].oo=x;        Data[k].eo=data[k].oe=data[k].ee=-inf;            } else {data[k].ee=x;        Data[k].eo=data[k].oe=data[k].oo=-inf;    } return;    } if (N&LT;=DATA[K*2].R) Updata (n,x,k*2);    else Updata (n,x,k*2+1); Pushup (k);}    Node search (int l,int r,int k) {node temp,t1,t2;    int mid;        if (data[k].l==l && data[k].r==r) {temp.ee=data[k].ee;        temp.oo=data[k].oo;        Temp.eo=data[k].eo; Temp.oe=data[k].oe;    return temp;    } mid= (DATA[K].L+DATA[K].R)/2;    if (R<=mid) return search (l,r,k*2);    else if (L>mid) return search (l,r,k*2+1);        else {t1=search (l,mid,k*2);        T2=search (mid+1,r,k*2+1);        Temp.ee=max (Max (Max (t1.ee+t2.oe,t1.eo+t2.ee), t1.ee), t2.ee);        Temp.eo=max (Max (Max (t1.eo+t2.eo,t1.ee+t2.oo), T1.eo), T2.eo);        Temp.oo=max (Max (Max (t1.oo+t2.eo,t1.oe+t2.oo), t1.oo), t2.oo);        Temp.oe=max (Max (Max (T1.oo+t2.ee,t1.oe+t2.oe), T1.oe), T2.oe);    return temp;    }}int Main () {int t,n,m,i,op,l,r;    Node Mark;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m);        for (i=1;i<=n;i++) scanf ("%i64d", &a[i]);        Build (1,n,1);            while (m--) {scanf ("%d%d%d", &op,&l,&r);                if (op==0) {ans=-inf;                Mark=search (l,r,1);                Ans=max (ans,mark.oo);               Ans=max (ans,mark.ee); Ans=max (Ans,mark.eo);                Ans=max (Ans,mark.oe);            printf ("%i64d\n", ans);        } else Updata (l,r,1); }} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 5316 segment tree interval maximum value problem

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.