[Java Segment Tree]2015 Shanghai Invitational D Doom

Source: Internet
Author: User

Test instructions: N Number of M queries

Each query [L, R] Sum, and then the [L, R] between all the numbers into square (modulo 9223372034707292160LL)

Very obvious line-segment tree

Seeing this modulus (Llong_max is 9223372036854775807) obviously squared will explode ll

It's easy to see that all the squares have been done a few times and the value is no longer changed, and this "a few times" is quite small, so direct violence is good.

     Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); BigInteger a=biginteger.valueof (2);//This is the square of 2.Long b=9223372034707292160l; BigInteger MoD=NewBigInteger (b.tostring ());  for(inti=1;i<=40;i++) {a=a.multiply (a). MOD (mod); System.out.println (i+ ":" + a);//the value after the square I time        }    }

1 ImportJava.io.*;2 ImportJava.util.*;3 Importjava.math.*;4 Importjava.nio.charset.StandardCharsets;5 6  Public classMain7 {8     StaticBigInteger li=Biginteger.zero;9     StaticLong b=9223372034707292160l;Ten     StaticBigInteger mod=NewBigInteger (b.tostring ()); One     StaticBiginteger[] Sum=Newbiginteger[200005]; A     Static Boolean[] num=New Boolean[200005]; -     StaticInputreader in =NewInputreader (); -      Public Static voidPushup (intRT) the     { -Sum[rt]= (Sum[rt*2].add (sum[rt*2+1]). MOD (mod); -Num[rt]=num[rt*2]&num[rt*2+1]; -     } +      Public Static voidBuildintLintRintRT) -     { +         if(l==R) A         { atsum[rt]=In.nextbiginteger (); -num[rt]=false; -             return ; -         } -         intM= (l+r)/2; -Build (L, M, rt*2); inBuild (M+1, R, rt*2+1); - pushup (RT); to     } +      Public StaticBigInteger Query (intLintRintLintRintRT) -     { the         if(L<=l && r<=R) *             returnSum[rt]; $         intM= (l+r)/2;Panax NotoginsengBigInteger ret=Li; -         if(l<=m) theRet=ret.add (Query (L, R, L, M, rt*2) . mod (mod); +         if(r>m) ARet=ret.add (Query (L, R, M+1, R, rt*2+1) . mod (mod); the         returnret.mod (mod); +     } -      Public Static voidUpdateintPintLintRintRT) $     { $         if(l==R) -         { -BigInteger cur=(Sum[rt].multiply (Sum[rt])). MOD (mod); the             if(sum[rt].equals (cur)) -num[rt]=true;Wuyisum[rt]=cur; the             return ; -         } Wu         intM= (l+r)/2; -         if(p<=m) AboutUpdate (P, L, M, rt*2); $         Else  -Update (P, m+1, R, rt*2+1); - pushup (RT); -     } A     Static intN; +      Public Static voidUpdateintLintRintLintRintRT) the     { -         if(L<=l && r<=R) $         { the             if(Num[rt]) the                 return ; the             if(l==R) theUpdate (l, 1, N, 1); -             Else  in             { the                 intMm= (l+r)/2; theUpdate (L, R, L, MM, rt<<1); AboutUpdate (L, R, Mm+1, R, rt<<1|1); the             } the              the             return ; +         } -         intM= (l+r)/2; the         if(l<=m)BayiUpdate (L, R, L, M, rt*2); the         if(r>m) theUpdate (L, R, M+1, R, rt*2+1); - pushup (RT); -     } the      Public Static voidMain (string[] args) the     { the         intT, Ca=1; thet=in.nextint (); -          while((t--)!=0) the         { then=in.nextint (); the             intm=in.nextint ();94Build (1, N, 1); theSystem.out.println ("Case #" + Ca + ":"); theca++; theBigInteger ans=Li;98              while((m--)!=0) About             { -                 intL, R;101L=in.nextint ();102R=in.nextint ();103Ans=ans.add (Query (L, R, 1, N, 1) . mod (mod);104 System.out.println (ans); theUpdate (L, R, 1, N, 1);106             }107         }108     }109}
View Code

[Java Segment Tree]2015 Shanghai Invitational D Doom

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.