Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2492
Ping pong
Problem description
N (3 <= n <= 20000) 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 two 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 have to walk to the referee's house, and because they are lazy, they want to make their total walking distance no more than the distance between their houses. of course all players live in different houses and the position of their houses are all different. if the referee or any of the two contestants is different, we call two games different. now is the problem: how many different Games can be held in this Ping Pong Street?
Input
The first line of the input contains an integer T (1 <= T <= 20), indicating the number of test cases, followed by T lines each of 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. (1 <= AI <= 100000, I = 1... N ).
Output
For each test case, output a single line contains an integer, the total number of different games.
Sample Input
1
3 1 2 3
Sample output
1
Let's talk about Code directly.
All explanations are marked in the code.
1 # include <stdio. h> 2 # include <string. h> 3 # define size 100100 4 int N, C [size], X1 [size], X2 [size], Y1 [size], Y2 [size], A [size]; 5 6 int lowbit (int K) 7 {8 return (K &-k); 9} 10 void Update (INT POs, int num) 11 {12 while (Pos <size) // important is size instead of <= N13 {14 C [POS] + = num; 15 POS + = lowbit (POS ); 16} 17} 18 int sum (int pos) 19 {20 int S = 0; 21 While (Pos> 0) 22 {23 S + = C [POS]; 24 pos-= lowbit (POS); 25} 26 return s; 27} 28 29 30 31 int main () 32 {33 int I, CAS; 34 scanf ("% d", & CAS); 35 while (CAS --) 36 {37 memset (C, 0, sizeof (c )); 38 scanf ("% d", & N); 39 for (I = 1; I <= N; I ++) 40 {41 scanf ("% d ", & A [I]); 42 int K1; 43 k1 = sum (A [I]); 44x1 [I] = k1; // One of the input I numbers is smaller than a [I] 45x2 [I] = i-1-k1; // One of the input I numbers is greater than a [I] 46 update (A [I], 1); 47} 48 memset (C, 0, sizeof (c); 49 Int J = 1; // represents the number of input numbers 50 for (I = N; I> = 1; I --, J ++) 51 {52 int K1; 53 k1 = sum (A [I]); 54 Y1 [I] = k1; // The number of the numbers entered after entering a [I] is 55 Y2 [I] = j-1-k1 smaller than a [I; // enter the number of 56 updates (A [I], 1) that are greater than a [I] after a [I ); 57} 58 _ int64 ans = 0; 59 for (I = 1; I <= N; I ++) 60 {61 // printf ("X1 [% d] = % d X2 [% d] = % d Y1 [% d] = % d Y2 [% d] = % d \ n ", i, X1 [I], I, X2 [I], I, Y1 [I], I, Y2 [I]); 62 ans + = x1 [I] * Y2 [I] + x2 [I] * Y1 [I]; 63 // ans + = x1 [I] * X2 [I]; 64} 65 printf ("% i64d \ n", ANS); 66} 67 68}
View code