The topic only n individuals, each person has an ID and a skill value, a game requires two players and a referee, only when the referee's ID and skill value between the two players can play a game, now ask how many games can be organized.
Because after the order, the first insert must be small, so the left and right sides of greater than less can be determined, with a tree-like array to maintain the player's ID
Sample Input
1
3 1 2 3
Sample Output
1
1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cmath>6#include <queue>7 using namespacestd;8 intn,m,t;9 Const intmaxn=100000;Ten __int64 C[MAXN]; One structnode A { - intX,id; -}p[maxn+2]; the BOOLCMP (node A,node b) - { - returna.x<=b.x; - } + intLowbit (inti) - { + returni& (-i); A } at voidAddintI,__int64 D) - { - while(i<=MAXN) - { -c[i]+=D; -i+=lowbit (i); in } - } to__int64 sum (inti) + { -__int64 ret=0; the while(i) * { $ret+=C[i];Panax Notoginsengi-=lowbit (i); - } the returnret; + } A intMain () the { + inti,j,k; -__int64 lmin,lmax,rmin,rmax,ans=0; $ //freopen ("1.in", "R", stdin); $scanf"%d",&t); - while(t--) - { the for(i=0; i<=maxn;i++) c[i]=0; -scanf"%d",&n);Wuyi for(i=1; i<=n;i++) scanf ("%d", &p[i].x), p[i].id=i; theSort (p+1, p+1+n,cmp); -Add (p[1].id,1); Wu for(i=2; i<n;i++) - { AboutLmin=sum (p[i].id-1);//The count is from 1 to i-1, so subtract one more 1 $lmax=p[i].id-1-lmin; - //printf ("%i64d%i64d", Lmin,lmax); -Rmin=sum (N)-sum (p[i].id); -rmax=n-p[i].id-rmin; A //printf ("%i64d%i64d \ n", Rmin,rmax); +ans+= (Lmin*rmax+rmin*lmax); Add (P[i].id,1); the //printf ("%i64d\n", ans); - } $printf"%i64d\n", ans); theans=0; the } the return 0; the}
POJ 3928 tree-like array