Vijos 1083 Small white Stroll Park (line tree)

Source: Internet
Author: User

Segment tree, maximum value m, and sum, maximum prefix and LM, maximum suffix and RM for each node maintenance interval.

If the requirement interval is [A, b], then the answer max (this interval m, left son M, right son M, left son rm+ right son lm).

---------------------------------------------------------------------------------

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#define REP (i,n) for (int i=0;i<n;++i)#define CLR (x,c) memset (x,c,sizeof (x) )#define REP (I,L,R) for (int i=1;i<=r;++i)#define MID (A, b) ((a+b) >>1)#define LC (x) (x<<1)#define RC (x) (LC (x) +1)using namespace std;const int maxn=500000+10,maxnode=maxn*4;const int INF=0X7FFFFFFF; int A[MAXN];struct Segment_tree {struct Node {int l,r;int lm,rm,m,sum;node operator + (const node &x) {node O;O.lm=max (LM,SUM+X.LM);O.rm=max (X.RM,X.SUM+RM);o.sum=sum+x.sum;O.m=max (Max (M,X.M), RM+X.LM);return o;}    };node St[maxnode];inline int update (int x) {node &O=ST[X],&L=ST[LC (x)],&R=ST[RC (x)];O.lm=max (L.LM,L.SUM+R.LM);O.rm=max (r.rm,r.sum+l.rm);o.sum=l.sum+r.sum;O.m=max (Max (L.M,R.M), L.RM+R.LM);}void Build (int x,int l,int r) { node &o=st[x];o.l=l; o.r=r;int M=mid (L,R);if (l==r) o.lm=o.rm=o.m=o.sum=a[l];else {if (l<=m) Build (LC (x), l,m);if (m<r) Build (RC (x), m+1,r);update (x);}    }int p,v;void Update (int x) { node &o=st[x];int L=O.L,R=O.R;if (l==r) o.sum=o.lm=o.rm=o.m=v;else {int M=mid (L,R);if (p<=m) Update (LC (x));if (m<p) Update (RC (x));update (x);}    }inline void UPDATE (int p,int v) {this->p=p; this->v=v;Update (1);    }int QL,QR;node query (int x) {node &o=st[x];int L=O.L,R=O.R; if (ql<=l && R<=QR) return st[x];int M=mid (L,R);if (qr<=m) return query (LC (x));if (ql>m) return query (RC (x));return Query (LC (x)) +query (RC (x ));    }  inline int Query (int l,int r) {ql=l; qr=r;return Query (1). M;    }} ST;int main (){freopen ("test.in", "R", stdin);freopen ("Test.out", "w", stdout);int n,m;scanf ("%d%d", &n,&m);Rep (i,1,n) scanf ("%d", &a[i]);ST. Build (1,1,n);While (m--) {int x, y, Z;scanf ("%d%d%d", &x,&y,&z);if (x==1) printf ("%d\n", ST. Query (min (y,z), Max (y,z)));else ST. UPDATE (y,z);}return 0;}

---------------------------------------------------------------------------------

Describe

Small new often accompany small white to go to the park to play, is so-called dog walking ... In the small new home near a "Park Road", the road side from the south to the north in the row of N Park, small white long to see the eyes, I do not know which park to play.

In the beginning, the small white in accordance with the park's landscape to each park to play a score-.-。 Small new to save trouble, every time you walk the dog will be predetermined a range, small white can only choose between the first and B Park (including A, b two parks) select a number of consecutive Park play. Small white of course want to choose the park score sum as high as possible. At the same time, because some park landscape will change, so the small white score may also have some changes.

Then, please help Xiao Bai to choose the park.

Format input Format

The first line, two integers n and m, respectively, indicates the number of parks and the total number of operations (walking the dog or changing the score).

The next n rows, one integer per line, gives the park's rating at the beginning of the small white.

Next m line, three integers per line. The first integer k,1 or 2. K=1 said that Xiao Xin to take small white out to play, the next two integers a and b give the selection of the park range (1≤a,b≤n, A can be greater than b! K=2 said that the small white changed the rating of a park, and the next two integers p and s, indicating that the mark of the small white to the P Park became S (1≤p≤n).

Among them, 1≤n≤500 000,1≤m≤100 000, all scores are integers with an absolute value not exceeding 1000.

Output format

Small white every go out to play once, all correspond to output a line, contain only an integer, indicate small white can choose the park score and the maximum value.

Example 1 sample input 1[copy]
5 31 2-3 4 51 2 32 2-11 2 3
Sample output 1[Copy]
2-1
Limit

Each test point 2s

Vijos 1083 Small white Stroll Park (line tree)

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.