Algorithm template--segment Tree 7 (Dice flipping problem)

Source: Internet
Author: User

Implementation function: First enter a sequence of length n, consisting of 1-4 (1 means rolling forward, 2 means rolling back, 3 means rolling to the left, 4 means rolling to the right, the original state of the dice: 1 before 2 Left 4 right 5 after 3 next 6), then enter any number of actions, enter "1 x y" Indicates that the sequence number x is changed to Y, and the input "2 x Y" indicates the output for the original state of the dice, according to the sequence from X to Y can make the dice look like

Principle: Or the line tree, and only need to modify the interval access, but the integration between the interval is no longer simple accumulation, but--displacement relationship, through the merger of the replacement relationship to achieve timely maintenance can be

1 type2Cube=Array[1..6] ofLongint;3 ConstFo:cube= (3,1,6,4,5,2);4Ba:cube= (2,6,1,4,5,3);5Le:cube= (5,2,3,1,6,4);6Ri:cube= (4,2,3,6,1,5);7Mo:cube= (1,2,3,4,5,6);8 var9 I,j,k,l,m,n:longint;TenA:Array[0..1000000] ofCube; One A1:cube; A functionmin (x,y:longint): Longint;inline; -          begin -               ifX<y ThenMin:=xElsemin:=y; the          End; - functionMax (x,y:longint): Longint;inline; -          begin -               ifX>y ThenMax:=xElsemax:=y; +          End; -  + functionmerge (A1,a2:cube): Cube;inline; A           varA3:cube;i:longint; at           begin -                 fori:=1  to 6  Do -a3[i]:=A1[a2[i]]; - exit (A3); -           End; - procedurebuilt (z,x,y:longint), inline; in           begin -                ifX=y Then to                   begin + read (i); -                         CaseI of the                             1: a[z]:=fo; *                             2: a[z]:=BA; $                             3: a[z]:=le;Panax Notoginseng                             4: a[z]:=ri; -                        End; the                   End +                Else A                    begin theBuilt (z*2, X, (X+y)Div 2); +Built (z*2+1, (X+y)Div 2+1, y); -A[z]:=merge (a[z*2],a[z*2+1]); $                    End; $           End; - procedureop (z,x,y,t,v:longint); inline; -           begin the                if(t=y) and(x=t) Then -                   beginWuyi                         CaseV of the                             1: a[z]:=fo; -                             2: a[z]:=BA; Wu                             3: a[z]:=le; -                             4: a[z]:=ri; About                        End; $ exit; -                   End; -                ift<= (X+y)Div 2  ThenOP (z*2, X, (X+y)Div 2, t,v)ElseOP (z*2+1, (X+y)Div 2+1, y,t,v); -A[z]:=merge (a[z*2],a[z*2+1]); A           End; + functionRes (z,x,y,l,r:longint): Cube;inline; the          begin -               if(L>r) Thenexit (MO); $               if(x=l) and(Y=R) Thenexit (A[z]); theExit (Merge (Result (z*2, X, (X+y)Div 2, L,min ((x+y)Div 2, r)), result (z*2+1, (X+y)Div 2+1, Y,max ((x+y)Div 2+1, L), R )); the          End; the begin the READLN (n); -Built1,1, n); in Readln; the       whileTrue Do the            begin About read (i); the                  CaseI of the                      1:begin the readln (j,k); +Op1,1, n,j,k); -                      End; the                      2:beginBayi readln (j,k); theA1:=res (1,1, n,j,k); the                              fori:=1  to 6  DoWrite (A1[i],' '); - Writeln; -                      End; the                 End; the            End; the  the End. -                  

Algorithm template--segment Tree 7 (Dice flipping problem)

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.