POJ 2750 potted Flower (segment tree interval merging)

Source: Internet
Author: User
Tags mul

Start to find out the solution, look at the online Daniel's solutions to find is the interval merger ...
Give you the number of N to form a sequence ring, and then each time a point of modification, and output the maximum interval of the series and (note is the ring, and the maximum interval of only n-1 number)

In fact, only need maintenance: The maximum interval value Mmax, the minimum interval value mmin. Of course, maintaining these two values requires maintenance: The value of the maximum and minimum interval at which the left endpoint begins, and the maximum and minimum interval at which the right endpoint begins. Then the sum of the series, the maximum value of the sequence, and the minimum value. Then is the interval maximum value of the query is equal to the entire interval of Mmax and interval and sum minus mmin, but when all are non-negative, are negative when the special sentence. I brush so many line segments tree still did not see out, sure enough or too weak

#include <Set>#include<map>#include<queue>#include<stack>#include<cmath>#include<vector>#include<string>#include<cstdio>#include<cstring>#include<stdlib.h>#include<iostream>#include<algorithm>using namespacestd;#defineEPS 1E-8/*Note that there may be output -0.000*/#defineSGN (x) (X<-eps -1:x<eps? 0:1)//X is a comparison of two floating-point numbers, note the return integer#defineCvs (x) (x > 0.0 x+eps:x-eps)//floating point Conversion#defineZero (x) (((x) >0? ( x):-(x)) <eps)//determine if it equals 0#defineMul (A, B) (a<<b)#defineDir (A, B) (a>>b)typedefLong Longll;typedef unsignedLong Longull;Const intinf=1<< -;Const DoublePi=acos (-1.0);Const intmax=100010<<2;structnode{intmmax,lmax,rmax,mmin,lmin,rmin; intSum,manx,minx; voidInitintnum) {Mmax=lmax=rmax=mmin=lmin=rmin=manx=minx=sum=num; }} Segtr[max];intNon//non-negative number of numbersintNminintAintb) {    returnA>b?b:a;}intNmaxintAintb) {    returnA>b?a:b;}voidUpnow (intNowintNext//Key{Segtr[now].minx=nmin (segtr[next].minx,segtr[next|1].minx);//segment tree is required to use the maximum intervalSegtr[now].manx=nmax (segtr[next].manx,segtr[next|1].manx); Segtr[now].sum=segtr[next].sum+segtr[next|1].sum; Segtr[now].lmax=nmax (segtr[next].lmax,segtr[next].sum+segtr[next|1].lmax);//the maximum interval of the left endpoint may spanSegtr[now].rmax=nmax (segtr[next|1].rmax,segtr[next|1].sum+Segtr[next].rmax); Segtr[now].mmax=nmax (Nmax (segtr[next].mmax,segtr[next|1].mmax), segtr[next].rmax+segtr[next|1].lmax);//the interval maximum may also be the maximum value across the left and right intervalsSegtr[now].lmin=nmin (segtr[next].lmin,segtr[next].sum+segtr[next|1].lmin); Segtr[now].rmin=nmin (segtr[next|1].rmin,segtr[next|1].sum+segtr[next].rmin); Segtr[now].mmin=nmin (Nmin (segtr[next].mmin,segtr[next|1].mmin), segtr[next].rmin+segtr[next|1].lmin); return;}voidCreate (intStaintEnnintNow ) {    if(sta==enn) {scanf ("%d",&segtr[now].sum); if(segtr[now].sum>=0) Non++;        Segtr[now].init (segtr[now].sum); return; }    intMid=dir (Sta+enn,1); intNext=mul (now,1);    Create (Sta,mid,next); Create (Mid+1, enn,next|1);    Upnow (Now,next); return;}voidUpdate (intStaintEnnintNowintXinty) {    if(sta==enn&&sta==x) {if(segtr[now].sum>=0&&y<0) Non--; if(segtr[now].sum<0&&y>=0) Non++;        Segtr[now].init (y); return; }    intMid=dir (Sta+enn,1); intNext=mul (now,1); if(mid>=x) Update (sta,mid,next,x,y); ElseUpdate (Mid+1, enn,next|1, x, y);    Upnow (Now,next); return;}intMain () {intn,m; intPos,val;  while(~SCANF ("%d",&N)) {Non=0; Create (1N1); scanf ("%d",&m);  for(intI=0; i<m; i++) {scanf ("%d%d",&pos,&val); Update (1N1, Pos,val); if(non==n)//are all non-negativeprintf"%d\n", segtr[1].sum-segtr[1].minx); Else if(non==0) printf ("%d\n", segtr[1].manx); Elseprintf ("%d\n", Nmax (segtr[1].mmax,segtr[1].sum-segtr[1].mmin));//ring processing can be used and subtracted from the interval minimum value        }    }    return 0;}

POJ 2750 potted Flower (segment tree interval merging)

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.