UVALive - 4329 Ping pong (树状数组)

UVALive - 4329
Ping pong
Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

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 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) , 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 a1a2...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, the total number of different games.

Sample Input

1
3 1 2 3

Sample Output

1
题意:一条大街上住着n个乒乓球爱好者,经常组织比赛。每个人都有一个技能值ai, 每场比赛需要3个人:两名选手和一名裁判。规定裁判位置必须在两个选手的中间, 而且技能值也必须在两个选手的中间,问一共能组织多少种比赛. 解体 设lv[i]表示第i个人左边的人中,技能值小于他的人数 设rv[i]表示第i个人右边的人中,技能值小于他的人数 则ans = lv[i] * (n - i - rv[i]) + (i - 1 - lv[i]) * rv[i] 其中lv[i] * (n - i - rv[i])表示左边少于A[i]的人数 * 右边比A[i]大的人数 右边则是左边比A[i]大的人数 * 右边比A[i]小的人数 所以问题就是求出lv[i]和rv[i]了。 求解lv[i]的答案就是算i之前的比C[A[i] - 1]小的的数的和 有没有发现这既是求解lv[i] = C[1] + ... + C[A[i] - 1].
 
   
/*
Memory: 0 KB		Time: 109 MS
Language: C++ 4.8.2		Result: Accepted
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long LL;
const int MAXN = 2e4 + 5;
const int MAXM = 1e5 + 5;
int lv[MAXN], rv[MAXN], T, N, C[MAXM], A[MAXN];

int lowbit(int x) {
    return x & (-x);
}

void add(int x) {
    while(x <= MAXM) {
        C[x] += 1;
        x += lowbit(x);
    }
}

int sum(int x) {
    int ret = 0;
    while(x > 0) {
        ret += C[x];
        x -= lowbit(x);
    }
    return ret;
}

int main() {
    scanf("%d", &T);
    while(T --) {
        scanf("%d", &N);
        memset(C, 0, sizeof(C));
        for(int i = 0 ; i < N; i ++) {
            scanf("%d", &A[i]);
            add(A[i]);
            lv[i + 1] = sum(A[i] - 1);
        }
        memset(C, 0, sizeof(C));
        for(int i = N - 1 ; i >= 0 ; i --) {
            add(A[i]);
            rv[i + 1] = sum(A[i] - 1);
        }
        LL ans = 0;
        for(int i = 1; i <= N ; i ++) {
            ans += (LL)lv[i] * (N - rv[i] - i) + (LL)rv[i] * (i - lv[i] - 1);
        }
        printf("%lld\n", ans);
    }
    return 0;
}





 
  

你可能感兴趣的:(UVALive - 4329 Ping pong (树状数组))