[UVALive4329] Ping Pong (tree-like array, combination)

Source: Internet
Author: User

Title Link: https://vjudge.net/problem/UVALive-4329

Test instructions: N number, find three number Ai,aj,ak, make i<j<k and Ai<aj<ak or Ai>aj>ak. Ask how many combination methods.

Enumerate AJ, which is the number of smaller than AJ in CJ [1,j], the number of DJs (j,n) smaller than AJ.

So the number of J-1-CJ is [1,j] larger than AJ, N-J-DJ is the number of (j,n) larger than AJ.

The answer is ∑dj* (J-1-CJ) + (N-J-DJ) *CJ

You can get C and D separately with bit in O (NLGN).

1#include <bits/stdc++.h>2 using namespacestd;3 4typedefLong LongLL;5 Const intMAXN =20020;6 Const intMAXM =100100;7 intN, M;8 intA[MAXN];9 LL BIT[MAXM];Ten LL C[MAXM], D[MAXM]; One  AInlineintLowbit (intx) { -     returnX & (-x); - } the  - voidAddinti) { -      while(I <=m) { -bit[i]++; +i + =lowbit (i); -     } + } A  atLL sum (inti) { -LL ret =0; -      while(i) { -RET + =Bit[i]; -I-=lowbit (i); -     } in     returnret; - } to  +  - intMain () { the     //freopen ("in", "R", stdin); *     intT; $scanf"%d", &T);Panax Notoginseng      while(t--) { -scanf"%d", &n); them =-1; +          for(inti =1; I <= N; i++) { Ascanf"%d", &a[i]); them =Max (M, A[i]); +         } -LL ret =0; $memset (bit,0,sizeof(bit)); $          for(inti =1; I <= N; i++) { - Add (A[i]); -C[i] = SUM (a[i]-1); the         } -memset (bit,0,sizeof(bit));Wuyi          for(inti = n; I >=1; i--) { the Add (A[i]); -D[i] = SUM (a[i]-1); Wu         } -          for(inti =1; I <= N; i++) { AboutRET + = ((I-1)-c[i]) * D[i] + (N-i-d[i]) *C[i]; $         } -cout << ret <<Endl; -     } -     return 0; A}

[UVALive4329] Ping Pong (tree-like array, combination)

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.