Line tree--small white stroll Park nkoj1316

Source: Internet
Author: User

Little white stroll around the park

Time limit:20000ms Memory limit:65536k
Case Time Limit:2000ms

Description

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.

Input

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 the small new to take the small white out to play, the next two integers a and b give the range of the selection Park (1≤a,b≤n), K=2 said that the small white changed the rating of a park, the next two integers p and s, indicating that the small white to the P Park's scores into 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

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.

Sample Input

5 31 2-3 4 51 2 32 2-11 2 3

Sample Output

2-1

Source

Vijos

Analysis: Maximum continuous and




Difficulties: Getans inside the required range riding on the head of two sons, seemingly unable to directly solve, recursion!!!

Please see the Code comment section for details:

#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include < Algorithm> #define INF 1e9using namespace Std;const int maxn=500005;int triplemax (int a,int b,int c) {return Max (A,max (b , c));} struct node{int a,b;int l1,r1,mid1,max1;int sum;}; Node Tree[maxn*4];int a[maxn];void Update (int p) {int l= (p<<1), r= (p<<1) +1;tree[p].sum=tree[l].sum+tree[r ].sum;tree[p].l1=max (TREE[L].L1,TREE[L].SUM+TREE[R].L1); Tree[p].r1=max (TREE[R].R1,TREE[R].SUM+TREE[L].R1); tree [P].max1=triplemax (TREE[L].MAX1,TREE[R].MAX1,TREE[L].R1+TREE[R].L1);} void Build_tree (int p,int x,int y) {tree[p].a=x;tree[p].b=y;if (x<y) {Build_tree (p<<1,x, (x+y) >>1); Build_tree ((p<<1) +1, ((x+y) >>1) +1,y); update (P);} ELSETREE[P].L1=TREE[P].R1=TREE[P].MID1=TREE[P].MAX1=TREE[P].SUM=A[X];} void change (int p,int k,int d) {if (tree[p].a>k| | tree[p].b<k) return;if (tree[p].a==tree[p].b) {tree[p].l1=d;tree[p].r1=d;tree[p].mid1=d;tree[p].max1=d;tree[p] . Sum=d;return;} if (tree[(p<<1)].a<=k&&k<=tree[(p<<1)].b) {Change ((p<<1), k,d);} if (tree[(p<<1) +1].a<=k&&k<=tree[(p<<1) +1].b) {Change ((p<<1) +1,k,d);} Update (P);} int getsum (int p,int x,int y) {if (tree[p].b<x| | Tree[p].a>y) return 0;if (tree[p].b<=y&&tree[p].a>=x) return Tree[p].sum;else{int total=0;total+= Getsum ((p<<1), x, y), Total+=getsum (((p<<1) +1), x, y); return total;}}    int getr (int p,int l,int r,int t) {//p number interval from right to left not exceeding T Max Int ls= (p<<1), rs= (p<<1) +1,mid= (l+r) >>1;    if (t<=l) return tree[p].r1;    Fully contained, directly returns the maximum value from the right start if (mid+1<=t) return Getr (rs,mid+1,r,t);    The right son is not included, recursively return to Max (TREE[RS].SUM+GETR (ls,l,mid,t), TREE[RS].R1); In the middle of the left son, two cases: 1, the right son from the right to the left of the maximum value 2, right son all + left son recursion down}int getl (int p,int l,int r,int t) {//p number interval from left to right not exceeding T Max Int ls= (p<<    1), rs= (p<<1) +1,mid= (l+r) >>1;    if (r<=t) return tree[p].l1;    Fully contained, directly returns the maximum value from the left if (t<=mid) return Getl (ls,l,mid,t); ZolThe child is not included, recursively return to Max (Tree[ls].sum+getl (rs,mid+1,r,t), TREE[LS].L1); In the middle of the right son, there are two cases: 1, the left son from left to right of the maximum value 2, left son all + right son recursion down}int getans (int p,int x,int y) {if (x>tree[p].b| | Y&LT;TREE[P].A) return-inf;if (x<=tree[p].a&&tree[p].b<=y) return tree[p].max1;if (Tree[p].a==tree[p] . b) Return-inf;int maxl=-inf,maxr=-inf,maxmid=-inf;if (!) ( y<tree[p].a| | tree[(p<<1)].b<x) Maxl=max (Maxl,getans ((p<<1), x, y)); y<tree[(p<<1) +1].a| | tree[p].b<x) Maxr=max (Maxr,getans ((p<<1) +1,x,y)), if (x<=tree[(p<<1)].b&&y>=tree[(p <<1) +1].a) {Maxmid=max (Maxmid,getr (p<<1), tree[(p<<1)].a,tree[(p<<1)].b,x) +getl ((p< <1) +1,tree[(p<<1) +1].a,tree[(p<<1) +1].b,y));} Return Triplemax (Maxl,maxr,maxmid);} int main () {int n,m,i,j,k;cin>>n>>m;for (i=1;i<=n;i++) scanf ("%d", &a[i]); Build_tree (1,1,n);/* cout<< "****************" <<endl;for (i=1;i<=9;i++) {cout<<i<< "number Junction (" <<tree[i].a << "," <<tree[i].b<< ") L1:" <<tree[i].l1<< "R1:" <<tree[i].r1<< "MAX1:" << tree[i].max1<< "sum" &LT;&LT;TREE[I].SUM&LT;&LT;ENDL;} cout<< "****************" <<endl;*/for (i=1;i<=m;i++) {int k,x,y;scanf ("%d%d%d", &k,&x,&y if (k==1) {if (x>y) swap (x, y);p rintf ("%d\n", Getans (1,x,y));} if (k==2) {change (1,x,y);a[x]=y;/*cout<< "****************" <<endl;for (j=1;j<=9;j++) {cout<<j << node ("<<tree[j].a<<", "<<tree[j].b<<") L1: "<<tree[j].l1<<" R1: "< <tree[j].r1<< "MAX1:" <<tree[j].max1<< "sum" &LT;&LT;TREE[J].SUM&LT;&LT;ENDL;} cout<< "****************" <<endl;*/}}}

Solution 2 by Spark:

For an interval, the following values are maintained:

Lmax: Contains the maximum continuous and the left endpoint.

Rmax: Contains the maximum continuous and the right endpoint.

Max: The maximum continuous and in the interval.

Sum: All elements of the interval.

Maintain the information after each modification.

If you divide an interval of o into the left and right two interval ls, RS, you can get a similar dynamic programming equation (think about why):

Val[o]. Sum = Val[ls]. sum+ Val[rs]. Sum;
Val[o]. Lmax = Max (Val[ls]. Lmax, Val[ls]. SUM+VAL[RS].    Lmax); Discuss whether to cross the midpoint
Val[o]. Rmax = Max (Val[rs]. Rmax, Val[rs]. Sum+val[ls]. Rmax);
Val[o]. Max =max (Val[ls]. Max, Val[rs]. Max, Val[rs]. Lmax+val[ls]. Rmax);

For ease of calculation, query operations require more information to be returned, which simplifies the code.

The code is as follows:

#include <cstdio> #include <iostream> #include <cstdlib> #include <cmath> #include <cstring > #include <queue> #include <vector> #include <algorithm> #define LL long long#define CLEAR (XXX) memset (xxx,0,sizeof (XXX)) using namespace Std;const int maxn=500000+5,inf=1e9;int n,m;inline void _read (int &x) { Char Ch=getchar (); BOOL Mark=false;for (;! IsDigit (CH); Ch=getchar ()) if (ch== '-') mark=true;for (X=0;isdigit (CH); Ch=getchar ()) x=x*10+ch-' 0 '; if (Mark) x=-x;}  inline int getmax (int x,int y,int z) {if (x>=z&&x>=y) return x;if (y>=z) return y; return z;} struct Node{int lmax,rmax,max,sum;} Val[maxn*5];int s[maxn],sum[maxn],ql,qr,p;void Maintain (int o,int l,int R) {//Maintain node information int mid= (L+R) >>1,ls=o*2,rs =o*2+1;val[o]. Sum=val[ls]. sum+ Val[rs]. Sum;val[o]. Lmax=val[ls]. Lmax;val[o]. RMAX=VAL[RS]. Rmax;val[o]. Lmax=max (Val[o]. Lmax, +val[ls]. SUM+VAL[RS]. Lmax); Val[o]. Rmax=max (Val[o]. Rmax, Val[rs]. Sum+val[ls]. Rmax); Val[o]. Max=getmax (Val[ls]. MAX,VAL[RS]. MAX,VAL[RS].Lmax+val[ls]. Rmax);} void build (int o,int l,int R) {if (l==r) Val[o]. Lmax=val[o]. Rmax=val[o]. Max=val[o]. sum=s[l];else {int mid= (l+r) >>1;build (O*2,l,mid); build (o*2+1,mid+1,r); Val[o]. Sum=sum[r]-sum[l-1];maintain (O,l,r);}} void update (int o,int l,int R) {if (l==r) Val[o]. Lmax=val[o]. Rmax=val[o]. Max=val[o]. sum=s[l];else {int mid= (l+r) >>1;if (p<=mid) update (O&LT;&LT;1,L,MID); else update (O*2+1,MID+1,R); Maintain (o   , l,r); }}node query (int o,int l,int R) {//Returns a series of answers to the range asked, including Lmax,rmax,sum,max if (ql<=l&&qr>=r) return Val[o   ]; Fully contains int mid= (L+R) >>1;  if (qr<=mid) return query (O&LT;&LT;1,L,MID);  Only the left interval else if (ql>mid) return query (O*2+1,MID+1,R);         Only the right interval else if (ql<=mid&&qr>mid) {////And the left-to interval have intersection int ls=o*2,rs=ls+1;    Node L=query (ls,l,mid);  Left node r=query (rs,mid+1,r); Right node Ans;ans. Sum=l.sum+r.sum;ans. Lmax=max (L.lmax, L.sum+r.lmax); ans. Rmax=max (R.rmax, R.sum+l.rmax); ans. Max=getmax (L.rmax+r.lmax, L.max, R.max); return ans;}} int main () {int i,k,s;_read (n); _read (m); for (i=1;i<=n;i++) {_read (s[i]); sum[i]=sum[i-1]+s[i];}  Build (1,1,n), while (m--) {_read (k); if (k==1) {_read (QL); _read (QR); if (QL&GT;QR) swap (QL,QR); Note that there is no guarantee in the title QL&LT;=QR printf ("%d\n", Query (1,1,n). MAX);} else {_read (P); _read (S); s[p]=s;update (1,1,n);}} return 0;}


Line tree--small white stroll Park nkoj1316

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.