"Line Tree" bzoj1756 Vijos1083 small white Stroll Park

Source: Internet
Author: User

We know that the maximum sub-segment of a sequence is an O (n), but this is obviously timed out.

We need a data structure to support the modification and calculation of the operation, for this kind of modification of a query interval problem, consider using line tree.

In the segment tree, in addition to the left endpoint, right end point, left son pointer, right son pointer, new open 4 field--max,maxl,maxr,sum, where sum is of that interval and Max is the maximum sub-segment on that interval, and MAXL must contain the largest child of the left endpoint and, MAXR is the maximum child segment and that must contain the right endpoint.

And then just ... You can use line tree to count, note that the maximum number of sub-segments and the minimum number of 1 elements, so there is a sample output negative value.

When modified:

1, if the left son of the MaxR and the right son of the Maxl are negative, from which to take larger for the node max (to prevent one not to take), and vice versa (both are taken).

2. Max of the node is updated with the left and right son's Max.

3. The Maxl of the node is the maximum value of the left son's Maxl with the left son Sum and the right son Maxl.

4, the MaxR of the node is the maximum value of the right son's maxr and the right son Sum and the left son MaxR.

5, the sum of the node is the sum of the left and right son.

When querying:

1. If the query interval overrides this node, the node information is returned.

2. If only one son is connected, return the information found in that son.

3. If there is a intersection with the two sons, first calculate the information of two sons, then merge the two information according to the modified method, then return.

4. The last Max value returned is the answer.

--http://www.cnblogs.com/whitecloth/archive/2012/03/22/2410925.html

#include <cstdio> #include <algorithm>using namespace std; #define N 500001struct Node{int MAXV,MAXL,MAXR, SUMV;} t[n<<2];inline void Pushup (node &rt,const node &ls,const node &rs) {if (ls.maxr<0 && RS.MAXL  <0) Rt.maxv=max (LS.MAXR,RS.MAXL); else {rt.maxv=0;  if (ls.maxr>0) Rt.maxv+=ls.maxr;  if (rs.maxl>0) Rt.maxv+=rs.maxl; }rt.maxv=max (RT.MAXV,LS.MAXV); Rt.maxv=max (RT.MAXV,RS.MAXV); Rt.maxl=max (LS.MAXL,LS.SUMV+RS.MAXL); Rt.maxr=max ( RS.MAXR,RS.SUMV+LS.MAXR); rt.sumv=ls.sumv+rs.sumv;}  void Buildtree (int rt,int l,int R) {if (l==r) {scanf ("%d", &AMP;T[RT].MAXV);  T[RT].SUMV=T[RT].MAXL=T[RT].MAXR=T[RT].MAXV;  Return }int m= (l+r>>1); Buildtree (rt<<1,l,m) buildtree (rt<<1|1,m+1,r);p ushup (T[rt],T[rt<<1],T[ Rt<<1|1]);}  void update (int p,int v,int rt,int l,int R) {if (l==r) {t[rt].sumv=t[rt].maxl=t[rt].maxr=t[rt].maxv=v;  Return }int m= (l+r>>1); if (p<=m) update (P,V,RT&LT;&LT;1,L,M); Else Update (p,v,rt<<1|1,m+1,R);p ushup (t[rt],t[rt<<1],t[rt<<1|1]);} Node query (int ql,int qr,int rt,int l,int R) {if (QL&LT;=L&AMP;&AMP;R&LT;=QR) return t[rt];int m= (l+r>>1); if (ql<  =m && m<qr) {Node res;  Pushup (Res,query (ql,qr,rt<<1,l,m), query (Ql,qr,rt<<1|1,m+1,r));  return res; }else if (ql<=m) return query (ql,qr,rt<<1,l,m); else return query (QL,QR,RT&LT;&LT;1|1,M+1,R);} int N,m;int Main () {int op,x,y;scanf ("%d%d", &n,&m), Buildtree (1,1,n), for (; m;--m) {scanf ("%d%d%d", &op,  &x,&y);  if (op==1) {if (x>y) swap (x, y);  printf ("%d\n", Query (x,y,1,1,n). MAXV);  } else update (X,Y,1,1,N); }return 0;}

Line segment Tree bzoj1756 Vijos1083 Small white stroll around the park

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.