From 4ae88a860b69c30dc5d9cc2893e05c29e89cf713 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Sat, 7 May 2016 19:40:20 +0800 Subject: [PATCH] Create 2492_cnblogs.cpp From http://www.cnblogs.com/qiufeihai/archive/2012/03/12/2391988.html --- HDOJ/2492_cnblogs.cpp | 117 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 HDOJ/2492_cnblogs.cpp diff --git a/HDOJ/2492_cnblogs.cpp b/HDOJ/2492_cnblogs.cpp new file mode 100644 index 0000000..8cac02e --- /dev/null +++ b/HDOJ/2492_cnblogs.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +//Constant Declaration +/*--------------------------*/ +//#define LL long long +#define LL __int64 +const int M=100001; +const int INF=1<<30; +const double EPS = 1e-11; +const double PI = acos(-1.0); +/*--------------------------*/ +// some essential funtion +/*----------------------------------*/ +void Swap(int &a,int &b){ int t=a;a=b;b=t; } +int Max(int a,int b){ return a>b?a:b; } +int Min(int a,int b){ return a 0) + { + sum += c[k]; + k -= LowBit(k); + } + return sum; +} + +void Update(int k, int sc) +{ + while (k <= 100000) + { + c[k] += sc; + k += LowBit(k); + } +} + + + +int main() +{ + //freopen("in.txt","r",stdin); + //freopen("out.txt","w",stdout); + int t, case1 = 0; + scanf("%d", &t); + int m; + int i, j; + int num; + //scanf("%d%d", &n, &m); + while (t--) + { + scanf("%d", &num); + memset(c, 0, sizeof(c)); + memset(left_lower, 0, sizeof(left_lower)); + memset(right_lower, 0, sizeof(right_lower)); + for (i = 1; i <= num; i++) + { + scanf("%d", &a[i]); + left_lower[i] += Sum(a[i] - 1);///Sum(a[i] - 1):插入该数前,区间1到a[i]-1的总个数 + Update(a[i], 1); + } + + memset(c, 0, sizeof(c)); + for (i = num; i > 0; i--)//顺序插入 + { + right_lower[i] += Sum(a[i] - 1); + Update(a[i], 1); + } + + LL ans = 0; + + for (i = 1; i <= num; i++)//逆序插入 + { + ans += left_lower[i]*(num - i - right_lower[i]);//由于只能求比其小的个数,可以用i右边的总个数见减比他小的来求比他大的个数。 + ans += (i - 1 - left_lower[i])*right_lower[i];// + } + + printf("%I64d\n", ans); + } + + + return 0; +}