HDU 5147 Sequence II

Source: Internet
Author: User

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

Test instructions: Ask how many such a four-tuple (a,b,c,d), satisfies the condition is 1<=a<b<c<d; aa<ab; ac<ad;

Idea: Use a tree-like array, from right to left to find out how many reverse-order number pairs are recorded in the R array, and then in the left-to-right to find out how many reverse number pairs exist in the L array before entering this number, then enumerate B on the line;

1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineMAXN 500015 #definell Long Long6 using namespacestd;7 8 intT;9 intN;Ten intA[MAXN]; One intC[MAXN]; A ll L[MAXN],R[MAXN],SUM[MAXN]; -  - intLowbit (intx) the { -     returnx&-x; - } -  + voidInsertintXintv) - { +      while(x<MAXN) A     { atc[x]+=v; -x+=lowbit (x); -     } - } -  -ll Getsum (intx) in { -ll ans=0; to      while(x>0) +     { -ans+=C[x]; thex-=lowbit (x); *     } $     returnans;Panax Notoginseng } -  the intMain () + { Ascanf"%d",&t); the      while(t--) +     { -memset (SUM,0,sizeof(sum)); $Memset (A,0,sizeof(a)); $scanf"%d",&n); -          for(intI=1; i<=n; i++) -         { thescanf"%d",&a[i]); -         }WuyiMemset (c,0,sizeof(c)); thesum[n+1]=0; -          for(intI=n; i>=1; i--) Wu         { -R[i]=getsum (N)-getsum (A[i]); AboutInsert (A[i],1); $sum[i]=sum[i+1]+R[i]; -         } -Memset (c,0,sizeof(c)); -          for(intI=1; i<=n; i++) A         { +L[i]=getsum (a[i]-1); theInsert (A[i],1); -         } $ll ans=0; the          for(intI=2; i<=n; i++) the         { theans+= (l[i]*sum[i+1]); the         } -printf"%lld\n", ans); in     } the     return 0; the}
View Code

HDU 5147 Sequence II

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.