Description
N (3N20000) Ping pong players live along a West-east street (consider the street as a line segment). Each player has a unique skill rank. To improve their skill rank, they often compete with each other. If-Players want to compete, they must choose a referee among other ping pong players and hold the game in the referee ' S house. For some reason, the contestants can ' t choose a referee whose skill rank is higher or lower than both of theirs. The contestants has to walk to the referee's house, and because they is lazy, they want to make their total walking dist Ance no more than the distance between their houses. Of course all players live in different houses and the position of their houses is all different. If the referee or any of the contestants are different, we call both games different. Now are the problem:how many different games can being held in this ping Pong street?
Input
The first line of the input contains an integer t(1T), indicating the number of test cases, F ollowed by T lines Each of the which describes a test case.
Every test case consists of N + 1 integers. The first integer is N , the number of players. Then N distinct integers a1, a2 ... AN follow, indicating the skill rank of each player, in the order of West to east ( 1ai100000, I= 1 ... N ).
Output
For each test case, output a single line contains an integer with the total number of different games.
Sample Input
13 1 2 3
Sample Output
1
/*Test Instructions: There are N table tennis enthusiasts living on a street, often organizing competitions. Everyone has a skill value AI, each game requires 3 people: two players and a referee. The referee position must be in the middle of the two players, and the skill value must also be in the middle of two players, ask how many kinds of game ideas can be organized: Consider the case of the first person when the referee, assuming A1 to A[i-1] ci is smaller than AI, then there is (i-1)-ci than AI, the same, assuming a[i+1 ] to an of Di is smaller than AI, then there are (n-i)-di than AI, and then according to the multiplication principle and the addition principle, I when the referee has CI (n-i-di) + (i-ci-1) *di, so the problem is converted to seek c,d*/#include<bits/stdc++.h>#defineN 100010using namespacestd;intA[n];intC[n],d[n],x[n];intLowbit (intx) { returnx& (-x);}voidAddintx) { while(x<N) {a[x]++; X+=lowbit (x); }}intSumintx) { ints=0; while(x>0) {s+=A[x]; X-=lowbit (x); } returns;}intMain () {Freopen ("C:\\users\\acer\\desktop\\in.txt","R", stdin); intT,n; while(SCANF ("%d", &t)! =EOF) { while(t--) {memset (c,0,sizeofc); memset (d,0,sizeofd); memset (x,0,sizeofx); scanf ("%d",&N); for(intI=1; i<=n;i++) scanf ("%d",&X[i]); for(intI=1; i<=n;i++) {Add (X[i]); C[i]=sum (x[i]-1); } memset (A,0,sizeof 0); for(inti=n;i>=1; i--) {Add (X[i]); D[i]=sum (x[i]-1); } intCur=0; for(intI=2; i<n;i++) cur+=c[i]* (N-i-d[i]) + (i-c[i]-1)*D[i]; printf ("%d\n", cur); } }}
La4329:ping Pong (tree-like array)