pku3468 Segment Tree Lazy

Source: Internet
Author: User

A very important thing in the line tree is called "lazy", also called "Yeptree", (Yep is the 2008 Physics competition Silver SJTU), because yep and lazy are similar in a sense. Well, anyway, lazy is a very common thing in a line tree. Why call him lazy? Is that each time when the sum value of the node is modified, if every time it is decentralized to the leaf node, then the complexity will become nlogn, not as direct violence (tree array also sqrt (n), although I do not), and for some changes in a large number of problems, with the segment tree dynamic maintenance, If you find a large interval to be modified, you can mark the large interval directly, and then change the sum value, and so on the next modification or query and then modify or query its tag value, so that each query or modification can be logn, greatly saving time.

I have not written a line tree for a long time, can now balance the tree with the balance tree, do not know how. Today this problem originally was very simple results I wrote N class has been WA, finally found that the lazy after the sum of the time there is a detail is not handled well, the amount, I always deal with the details is not good. But the point is I used to write a lazy line tree is very familiar, incredibly forget. Also, writing programs are basically 100+,200+, much longer than Noip. At that time, Noip the third problem marked 50 lines.

The gossip is not much to say, the question.

Main topic:

Give you n number, q operation, operation there are two, ' q a B ' is asked a~b this number of and, ' C a B C ' is to put a~b this paragraph number are added C.

Brief:

Is the line tree, sum represents the interval and, col represents dyeing, is the most basic operation of the line segment tree.

Attached procedure (118 lines):

Program pku3468; var ctree:array[0..300000] of record l,r,mid,left,right:longint; Sum,col:int64; End A:ARRAY[0..100001] of Int64; N,tot:longint; Sum:int64; {-------------------------------------------} procedure build (U:longint); Begin if CTREE[U].L+1=CTREE[U].R then BEGIN ctree[u].sum:=a[ctree[u].l];exit; End ctree[u].mid:= (CTREE[U].L+CTREE[U].R) Div 2; Inc (TOT); Ctree[u].left:=tot; CTREE[TOT].L:=CTREE[U].L; Ctree[tot].r:=ctree[u].mid; Build (TOT); Inc (TOT); Ctree[u].right:=tot; Ctree[tot].l:=ctree[u].mid; CTREE[TOT].R:=CTREE[U].R; Build (TOT); Ctree[u].sum:=ctree[ctree[u].left].sum+ctree[ctree[u].right].sum; End {-------------------------------------------} procedure change (l,r,u:longint; p:int64); Begin if CTREE[U].L+1=CTREE[U].R then BEGIN Inc (CTREE[U].SUM,P); exit; End If Ctree[u].col<>0 then begin ctree[u].sum:=ctree[u].sum+ctree[u].col* (CTREE[U].R-CTREE[U].L); Inc (CTREE[CTREE[U].LEFT].COL,CTREE[U].COL); Inc (CTREE[CTREE[U].RIGHT].COL,CTREE[U].COL); ctree[u].col:=0; End If (L=CTREE[U].L) and (R=CTREE[U].R) THEN BEGIN Inc (CTREE[U].COL,P); ctree[u].sum:=ctree[u].sum+ctree[u].col* (CTREE[U].R-CTREE[U].L); Inc (CTREE[CTREE[U].LEFT].COL,CTREE[U].COL); Inc (CTREE[CTREE[U].RIGHT].COL,CTREE[U].COL); ctree[u].col:=0; Exit End else if L>=ctree[u].mid then change (l,r,ctree[u].right,p) Else if R<=ctree[u].mid then change (L,r,ctree[u]. LEFT,P) Else begin change (L,CTREE[U].MID,CTREE[U].LEFT,P); Change (CTREE[U].MID,R,CTREE[U].RIGHT,P); End Ctree[u].sum:=ctree[ctree[u].left].sum+ctree[ctree[u].right].sum; Inc (Ctree[u].sum,ctree[ctree[u].left].col* (CTREE[U].MID-CTREE[U].L)); Inc (Ctree[u].sum,ctree[ctree[u].right].col* (Ctree[u].r-ctree[u].mid)); End {-------------------------------------------} procedure find (L,r,u:longint); Begin if CTREE[U].L+1=CTREE[U].R then BEGIN Inc (SUM,CTREE[U].SUM+CTREE[U].COL); exit; End If Ctree[u].col<>0 then BEGIN Inc (Sum,ctree[u].col* (R-L)); End if (ctree[u].l=l) and (CTREE[U].R=R) THEN BEGIN Inc (Sum,ctree[u].sum); exit; End If L>=ctree[u].mid then find (l,r,ctree[u].right) Else if R<=ctree[u].mid then find (l,r,ctree[u].left) ELSE begin find ( L,ctree[u].mid,ctree[u].left); Find (Ctree[u].mid,r,ctree[u].right); End End {-------------------------------------------} procedure main; var i,q,l,r:longint; P:int64; T:char; Begin READLN (N,Q); For I:=1 to n do read (A[i]); READLN; Ctree[1].l:=1;ctree[1].r:=n+1;tot:=1; Build (1); For i:=1 to Q do begin read (t); If t= ' Q ' then begin sum:=0; READLN (L,R); Find (l,r+1,1); Writeln (sum); End else if t= ' C ' then begin READLN (L,R,P); Change (L,R+1,1,P); End End End {-------------------------------------------} begin main; End.

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.