Topic Link: Click to enter
Start by trying to enumerate the two contestants, and then ask for the number of judges that meet the requirements, but the complexity of time will not be able to meet the requirements of the topic. It is assumed that each person can be enumerated as a referee, assuming that the first player is the referee and then the left 1–i-1 Lmin[i] The rank of individual is lower than he, then there is i-1-lmin[i] individual rank is not lower than he, set his right i+1–n Rmin[i] individual rank is lower than him, then have n-i-rmin[i] individual rank is not lower than him. The number of games that can be held when the first contestant is a referee is lmin[i] (N-i-rmin[i]) +rmin[i] (I-1-lmin[i]). So the problem now is to find out the rmin and lmin of each player.
Here we can assume that there is a rank array c[], where c[i]=x indicates that there is an X person in the position of the first place. And now we're asking how many people on the left side of the I are ranked lower than it; we can insert the rank of all the players on the left side of it into this array and query the value of c[1]+c[2]+...+c[a[i]-1]. So all we have to do is query and update all the players from left to right, which can be done using a line tree or a tree-like array. For each player on the right side of the situation can also be treated similarly.
The code is as follows:
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>using namespace STD;#define MAXN 100010#define LL Long Longinta[maxn],c[maxn*4],m;intLMIN[MAXN],RMIN[MAXN];intLowbit (intx) {returnx& (-X);}intSumintx) {intret=0; while(x>0) {ret+=c[x]; X-=lowbit (x); }returnRET;}voidAddintXintD) { while(x<=m) {C[x]+=d; X+=lowbit (x); }}intMain () {//freopen ("In.txt", "R", stdin); intTscanf("%d", &t); while(t--) {intN m=0;scanf("%d", &n); for(intI=1; i<=n;i++) {scanf("%d", &a[i]); M=max (M,a[i]); }memsetC0,sizeof(C));memset(Lmin,0,sizeof(Lmin));memset(Rmin,0,sizeof(rmin)); for(intI=1; i<=n;i++) {Lmin[i]=sum (a[i]-1); Add (A[i],1); }memsetC0,sizeof(C)); for(intj=n;j>=1; j--) {Rmin[j]=sum (a[j]-1); Add (A[j],1); } ll ans=0; for(intI=2; i<n;i++) {ans+= (LL) lmin[i]* (N-i-rmin[i]); ans+= (LL) rmin[i]* (i-1-lmin[i]); }printf("%lld\n", ans); }return 0;}
Uvalive 4329--ping pong+ tree-like array