HDU 5792:world is exploding (tree-like array)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=5792

World is exploding

problem DescriptionGiven a sequence A with length n,count how many quadruple (a,b,c,d) satisfies:A≠B≠C≠D,1≤A<B≤N,1≤C<D≤N,Aa<ab,aC>ad .  InputThe input consists of multiple test cases.
Each test case is begin with a integer n

The next line contains n integersa1,a2? An .
1≤n≤50000
0≤Ai≤1e9   Output For each test Case,output a line contains an integer.   Sample Input42 4 1 341 2 3 4  Sample Output 10

Insipired by http://blog.csdn.net/libin66/article/details/52098019

Test instructions: More simple and direct look.

Idea: To find out in the time before I how much smaller than it (that is, like Aa,ab, how many pairs in ascending order), with up record, behind how much smaller than it (like ac,ad such a descending number of pairs), with the down record, and then ans = up *, because there are a lot of repetition. Using a tree-like array to maintain four arrays, ls--in [1,i-1] how much smaller than a[i], rs--in [i+1,n] how much smaller than a[i], lb--in [1,i-1] how much larger than A[i, rb--in [i+1,n] there is much larger than a[i]. By the repulsion principle to subtract (LS * lb + rs * RB + ls * rs + lb * rb).

1#include <cstdio>2#include <algorithm>3#include <cstring>4 using namespacestd;5 #defineN 500056 #defineMOD 10000000077typedefLong LongLL;8 LL Ls[n], lb[n], rs[n], rb[n], bit[n];9 intA[n], num[n], N, m;Ten  One /* A ls and LB are the number of smaller numbers [1,i-1] than a[i], the number of larger numbers than A[i] - RS and RB are [I+1, n] in the number of smaller numbers than a[i], the number of larger numbers than A[i] - These can all be implemented in a tree-like array the because the answer will be counted. a=c | | a=d | | b=c | | b=d, then enumerate the four things minus it (a=c so necessarily b!=d, the same as the other - A=c:ans-=rs[i]*rb[i] - A=d:ans-=lb[i]*rb[i] - B=c:ans-=ls[i]*rs[i] + B=d:ans-=lb[i]*ls[i] - */ +  A voidInit () at { -memset (LS,0,sizeof(LS)); -memset (LB,0,sizeof(LB)); -memset (RS,0,sizeof(RS)); -Memset (RB,0,sizeof(RB)); -memset (SUM,0,sizeof(sum)); in } -  to intLowbit (intx) + { -     returnX & (-x); the } *  $ voidUpdateintPOS)Panax Notoginseng { -      while(Pos <=m) { thebit[pos]++; +pos + =Lowbit (POS); A     } the } +  -LL Query (intPOS) $ { $LL ans =0; -      while(Pos >0) { -Ans + =Bit[pos]; thepos-=Lowbit (POS); -     }Wuyi     returnans; the } -  Wu intMain () - { About      while(~SCANF ("%d", &N)) { $ init (); -          for(inti =1; I <= N; i++) { -scanf"%d", &a[i]); -Num[i] =A[i]; A         } +Sort (A +1, A +1+n); them = Unique (A +1, A +1+n)-(A +1); -          for(inti =1; I <= N; i++) $Num[i] = Lower_bound (A +1, A +1+m, Num[i])-A; the  theLL up =0, down =0; thememset (bit,0,sizeof(bit)); the          for(inti =1; I <= N; i++) { -Ls[i] = query (Num[i]-1); inLb[i] = query (M)-query (Num[i]); theup + =Ls[i]; the Update (num[i]); About         } the  thememset (bit,0,sizeof(bit)); the          for(inti = n; I >=1; i--) { +Rs[i] = query (Num[i]-1); -Rb[i] = query (M)-query (Num[i]); theDown + =Rs[i];Bayi Update (num[i]); the         } the  -LL ans =0; -  the          for(inti =1; I <= N; i++) { theAns-= ls[i] *Lb[i]; theAns-= rs[i] *Ls[i]; theAns-= rb[i] *Lb[i]; -Ans-= rb[i] *Rs[i]; the         } the  theprintf"%i64d\n", Up * down +ans);94     } the     return 0; the}

HDU 5792:world is exploding (tree-like array)

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.