The title of the Great White
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #include <queue> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <climits> #include <cstdlib> #include <cstring># Include <iostream> #include <algorithm> #define LL long long#define PI 3.1415926535897932626using namespace Std;int gcd (int a, int b) {return a% b = = 0? b:gcd (b, a% b);} #define MAXN 100010#define maxd 20010int num[maxn];int val[maxd],lmin[maxd],rmin[maxd];int lowbit (int x) {return x & ( -X);} int sum (int x) {int ret = 0; while (x > 0) {ret + = num[x]; X-= Lowbit (x); } return ret;} void Add (int x,int d) {while (x <= maxn) {num[x] + = D; x + = Lowbit (x); }}int Main () {int t,n; scanf ("%d", &t); While (t--) {scanf ("%d", &n); memset (num,0,sizeof (num)); for (int i = 1; I <= n; i++) {scanf ("%d", &val[i]); Add (val[i],1); Lmin[i] = SUM (val[i]-1); } memset (Num,0,sizeof (num)); for (int i = n; I >= 1; i--) {Add (val[i],1); Rmin[i] = SUM (val[i]-1); } LL ans = 0; for (int i = 1; I <= n; i++) ans + lmin[i] * (N-i-rmin[i]) + (I-lmin[i]-1) * rmin[i]; printf ("%lld\n", ans); } return 0;}
Uvalive 4329 Ping Pong