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 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(1T20) <tex2html_verbatim_mark>, indicating the number of test cases, followed by T<tex2html_verbatim_mark>lines each of which describes a test case.
Every test case consists of N + 1 integers. The first integer is N <tex2html_verbatim_mark>, the number of players. Then N distinct integersa1, a2...aN <tex2html_verbatim_mark>follow, indicating the skill rank of each player, in the order of west to east ( 1ai100000 <tex2html_verbatim_mark>, 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 题目大意:
一条大街上住着n个乒乓球爱好者,经常组织比赛切磋技术。每个人都有一个能力值a[i]。每场比赛需要三个人:两名选手,一名裁判。他们有个奇怪的约定,裁判必须住在两名选手之间,而裁判的能力值也必须在两名选手之间。问一共能组织多少种比赛。 分析:
考虑第i个人,假设a1到ai-1中有ci个比ai小,那么就有(i-1)-ci个比ai大;同理,如果ai+1到an中有di个比ai小,那么就有(n-i)-di个比ai大。更具乘法原理和加法原理,i当裁判就有
ci * (n-i-di) + (i-1-ci)*di
种比赛。(感觉这种思路简直碉堡了) 然后问题就转化为了计算数组c和数组d。这样的话就很容易想到使用树状数组去计算前缀和。
见代码:
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout) template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } //typedef __int64 LL;
typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-; int T, n, a[MAXN], c[MAXN], d[MAXN], x[MAXM]; int lowbit(int x)
{
return x & (-x);
} int getSum(int k)
{
int ans = ;
while(k>)
{
ans += x[k];
k -= lowbit(k);
}
return ans;
} void edit(int k)
{
while(k <= )
{
x[k] += ;
k += lowbit(k);
}
} int main()
{
// FOPENIN("in.txt");
// FOPENOUT("out.txt");
while(~scanf("%d", &T)) while(T--)
{
mem0(x); mem0(c); mem0(d);
scanf("%d", &n);
for(int i=; i<=n; i++) scanf("%d", &a[i]);
for(int i=; i<=n; i++) { edit(a[i]); c[i] = getSum(a[i]-); }
mem0(x);
for(int i=n; i>=; i--) { edit(a[i]); d[i] = getSum(a[i]-); }
LL ans = ;
for(int i=;i<n;i++)
{
ans += (LL)c[i]*(n-i-d[i]) + (LL)d[i]*(i-c[i]-);
}
printf("%lld\n", ans);
}
return ;
}