http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22105
#include <iostream>#include<cstdio>#include<cstring>#include<string>#defineINF 0x3f3f3f3f#defineLson rt<<1, L, M#defineRson rt<<1|1, m+1, Rusing namespaceStd;typedefLong LongLL;Const intMAXN =524288+5;intN;structP {LL D, sum, Head, tail; intL, R, Head_bound, Tail_bound;} P[MAXN<<1];voidPushup (intRT) { intL = rt<<1, R = rt<<1|1; if(P[L].D >=p[r].d) {P[RT].D=P[L].D; P[RT].L=P[L].L; P[RT].R=P[L].R; } Else{P[RT].D=P[R].D; P[RT].L=P[R].L; P[RT].R=P[R].R; } if(P[rt].d < P[l].tail +p[r].head) {P[RT].D= P[l].tail +P[r].head; P[RT].L=P[l].tail_bound; P[RT].R=P[r].head_bound; } Else if(P[RT].D = = P[l].tail +p[r].head) { if(P[rt].l >p[l].tail_bound) {P[RT].L=P[l].tail_bound; P[RT].R=P[r].head_bound; } Else if(P[RT].L = =p[l].tail_bound) { if(P[rt].r >p[r].head_bound) {P[RT].R=P[r].head_bound; } } } if(P[l].head >= P[l].sum +p[r].head) {P[rt].head=P[l].head; P[rt].head_bound=P[l].head_bound; } Else{P[rt].head= P[l].sum +P[r].head; P[rt].head_bound=P[r].head_bound; } if(P[r].tail>P[r].sum +p[l].tail) { //carefully consider, in order to take the smallest subscript, here to take the greater than number P[rt].tail=P[r].tail; P[rt].tail_bound=P[r].tail_bound; } Else{P[rt].tail= P[r].sum +P[l].tail; P[rt].tail_bound=P[l].tail_bound; } p[rt].sum= P[l].sum +p[r].sum;}voidBuildintRtintLintR) { if(L = =r) {scanf ("%lld", &P[RT].D); P[rt].sum= P[rt].head = P[rt].tail =P[RT].D; P[RT].L= P[RT].R = P[rt].head_bound = P[rt].tail_bound =l; return; } intm = (L + r) >>1; Build (Lson); Build (Rson); Pushup (RT);} P Query (intQlintQrintRtintLintR) { if(QL <= l && r <= qr)returnP[rt]; intm = (L + r) >>1; P left, right, ret; BOOLOk1 =0, Ok2 =0; if(QL <=m) { left=query (QL, QR, Lson); Ok1=1; } if(QR >m) { right=query (QL, QR, Rson); Ok2=1; } if(OK1) {ret=Left ; if(OK2) {if(Ret.d <right.d) {RET.D=RIGHT.D; RET.L=RIGHT.L; RET.R=RIGHT.R; } if(Ret.d < Left.tail +right.head) {RET.D= Left.tail +Right.head; RET.L=Left.tail_bound; RET.R=Right.head_bound; } Else if(RET.D = = Left.tail +right.head) { if(Ret.l >left.tail_bound) {RET.L=Left.tail_bound; RET.R=Right.head_bound; } Else if(RET.L = =left.tail_bound) { if(RET.R >right.head_bound) {RET.R=Right.head_bound; } } } if(Left.head >= Left.sum +right.head) {ret.head=Left.head; Ret.head_bound=Left.head_bound; } Else{Ret.head= Left.sum +Right.head; Ret.head_bound=Right.head_bound; } if(Right.tail>Right.sum +left.tail) {ret.tail=Right.tail; Ret.tail_bound=Right.tail_bound; } Else{Ret.tail= Right.sum +Left.tail; Ret.tail_bound=Left.tail_bound; } ret.sum= Left.sum +right.sum; } } Else if(OK2) {ret=Right ; } returnret;}intMain () {intM, A, B, kase=1; while(SCANF ("%d%d", &n, &m)! =EOF) {Build (1,1, N); printf ("Case %d:\n", kase++); //Note the position of case here, I wrote it down in the while inside, WA to cry ... while(m--) {scanf ("%d%d", &a, &b); P ans= Query (A, B,1,1, N); printf ("%d%d\n", ANS.L, ANS.R); } } return 0;}
uvalive-3938--"Ray, Pass me the dishes!" "segment Tree"