Problem descriptionn (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?
Inputthe 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 ).
Outputfor each test case, output a single line contains an integer, the total number of different games.
Sample Input
13 1 2 3
Sample output
1
Source2008 Asia Regional Beijing has n table tennis fans living on a street. They often organize competitions to learn about the technology. Each person has a different skill value, A (I ). Three people are required for each game: two contestants and one referee. They have a strange rule that the referee must be in the middle of two players, and the skill value is between them. Ask how many competitions can be organized. [Input format] enter the number of first behavior data groups T (1 <= T <= 20). Each group occupies one row. The first is an integer N (3 "= your" = 20000 ), then there are n different integers, that is, A (1), a (2 )...... A (n) (1 <= a (I) <= 100000), the skill values of each table tennis enthusiast are given in the order from left to right. [Output format] outputs the total number of competitions for each group of data. [Analysis] We consider the case where I am a referee. Suppose A (1) to a (I-1) has a C (I) smaller than a (I), then there is a i-1-c (I) larger than a (I); likewise, assume that the number of d (I) in a (I + 1) to a (n) is smaller than that in a (I), that is, n-I-d (I) higher than a (I). According to the multiplication principle and addition principle, I have C (I) * (N-I-d (I) + d (I) * (i-1-c (I) Race. In this way, the problem is converted to C (I) and D (I ). C (I) can scan all a (I) from left to right, so that X [J] indicates all a (I) that has been considered so far) if there is a (I) = J (X [J] = 0 indicates that there is no, = 1 indicates that there is), then C (I) is the prefix and X [1] + X [2] +... X [A (I)-1]. This is a typical application of tree arrays. Of course, we can also use line segments to solve the problem. However, the code is not efficient, and the code I wrote is attached below.
# Include <stdio. h> # include <string. h> # include <stdlib. h> # define a 100010 # define n 20010int C [a], C [N], d [N]; int A [n]; int maxa = 0; int max (int A, int B) {If (A> B) return a; return B;} int lowbit (INT X) {return X &-X ;} int sum (int x) {int ret = 0; while (x> 0) {RET + = C [X]; X-= lowbit (x);} return ret ;} void add (int x, int d) {While (x <= maxa) {// note that N is not used, is the largest value in a [] C [x] + = D; x + = lowbit (x) ;}} int main () {int T; scanf ("% d", & T); While (t --) {memset (C, 0, sizeof (c); memset (A, 0, sizeof (a); memset (C, 0, sizeof (c); memset (D, 0, sizeof (d); int N; scanf ("% d ", & N); For (INT I = 1; I <= N; I ++) {scanf ("% d", & A [I]); maxa = max (maxa, a [I]) ;}for (INT I = 1; I <= N; I ++) {Add (A [I], 1 ); c [I] = sum (A [I]-1); // number of people on the left that is smaller than a [I]} memset (C, 0, sizeof (c); For (INT I = N; I> = 1; I --) {Add (A [I], 1 ); d [I] = sum (A [I]-1); // Number of Persons smaller than a [I] on the right} long ans = 0; for (INT I = 1; I <= N; I ++) {ans + = (long) (C [I] * (N-I-d [I]) + (long) (D [I] * (i-1-c [I]);} printf ("% i64d \ n", ANS);} return 0;}/* 33 1 2 35 1 4 9 8 25 1 3 2 4 5 */
HDU 2492 pingpang