Hdu4000-tree array-2

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4000

For an arrangement of 1 -- N, calculate the number of all I <j <K and A [I] <A [k] <A [J...

 

Analysis: I don't know how to do it during the competition. I always want to simply enumerate I, j, or K to get the result. I don't know how to change it... In fact, you can change the direction of thinking, for each position to consider the number of The number of his greater than the number, then the small number followed by two large numbers x (x-1)/2, then, you must subtract all the numbers of I <j <K and A [I] <A [J] <A [k] that constitute the end. Both of these operations can be solved, then the problem is converted to scanning the array again...

 

Code:

# Include <stdio. h ># include <iostream> using namespace STD; const int n = 100001; int A, N, F [N] ;__ int64 ans, TMP, tmp1; void insert (int I) {for (; I <= N; I + = I & (-I) f [I] + = 1 ;} int query (int I) {int TMP = 0; For (; I> 0; I-= I & (-I) TMP + = f [I]; return TMP;} int main () {int I, Cas, cas1 = 1; scanf ("% d", & CAS); While (CAS --) {scanf ("% d", & N ); ans = 0; for (I = 1; I <= N; I ++) f [I] = 0; for (I = 1; I <= N; I ++) {scanf ("% d", & A); insert (a); TMP = query (A-1); tmp1 = (n-A)-(i-1-tmp ); ans-= TMP * tmp1; If (tmp1> = 2) ans + = tmp1 * (tmp1-1)/2;} printf ("case # % d: % i64d \ n ", cas1 ++, ANS % 100000007);} 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.