HDU 4000 Fruit Ninja

Source: Internet
Author: User
/*  Question: Given the sequence, one of the subsequences is (x, y, z). Ask how many of these subsequences are there at most, so that x <z <y, in this analysis, the sequence is 1 to n: because it is an arrangement, so there is no same element, so x, y, z are not equal to each other. Therefore, the size relationship in the sequence can be expressed as small, medium, large, and small, and = small ____, so the answer is small, medium, and small _-small, medium, and small _. You can count the number of all elements after this bit as X, then the small _ = x * (x-1)/2 and small statistics in the big, just need to count the first and later than his big number x, y, the number of small-to-large = x * y statistics can be calculated using a tree array. The tree array can calculate the number of previously added elements smaller than the current element.  */  # Include <Iostream> # Include <Cstdio> # Include <Cstring> Using   Namespace  STD;  # Define Mod4 100000007 # Define X 100005 Int  A [x];  Long   Long  C [X], big [X], SMA [x];  Int Lowbit ( Int  X ){  Return X &- X ;}  Void Modify ( Int  X ){  While (X <X) {C [x] = (C [x] + 1 ) % MOD; x + = Lowbit (x );}}  Long   Long Query ( Int  X ){  Long   Long Ret = 0  ;  While (X> 0  ) {RET + = C [X]; RET % = MOD; x -= Lowbit (x );}  Return  RET ;}  Int  Main () {freopen (  "  Sum. In  " , "  R  "  , Stdin); freopen (  " Sum. Out  " , "  W  "  , Stdout );  Int CNT = 0  , T, n; CIN > T;  While (T -- ) {Printf (  "  Case # % d:  " , ++CNT); scanf (  "  % D  " ,& N); memset (C,  0 , Sizeof  (C); memset (big,  0 , Sizeof  (BIG); memset (SMA,  0 , Sizeof  (SMA ));  For (Int I = 1 ; I <= N; I ++ ) {Scanf (  "  % D  " ,& A [I]); SMA [I] = Query (A [I]); ///  Count the number smaller than him  Modify (A [I]);} memset (C,  0 , Sizeof  (C ));  For (Int I = N; I> = 1 ; I --) ///  Count the number later than him  {Big [I] = N-I- Query (A [I]); Modify (A [I]);}  Long   Long S = 0 , B = 0  ;  For ( Int I = 1 ; I <= N; I ++ ) {S = (S + (BIG [I]- 1 ) * Big [I]/ 2 ) % MOD; ///  Count the number of small _ B = (B + big [I] * SMA [I]) % MOD; ///  Count the numbers in a Small and Medium Size  } Cout <(S-B + mod) % mod < Endl ;}  Return   0  ;} 

 

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.