POJ3928(树状数组:统计数字出现个数)

Ping pong
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2641   Accepted: 978

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
题目大意:给定几个ping pong玩家,每个玩家都有唯一的技能值。每个玩家按东西方向排成一列。问从中选两个玩家,再选一个裁判。裁判在位置上在两个玩家之间(当然不能是其中一个玩家)。裁判的技能 值必须在两个玩家之间(注意:每个玩家的技能值都是唯一的)。三个人组成一个组合,只要其中有一个人不同就算不同的组合,问共有多少种组合?
思路:如果从玩家的角度解题,那么会TLE。从裁判的角度看,就是从找出位置在其左方且技能值比其小的玩家个数 x 位置在其右方且技能值比其大的玩家个数 + 位置在其左方且技能值比其大的玩家个数 x 位置在其右方且技能值比其小的玩家个数。
#include <iostream>
#include <string.h>
using namespace std;
const int MAXN=20005;
const int N=100005;
struct Node{
    int lp,lw,rp,rw;//分别存储左边技能比其高,低的人数和右边技能比其高,低的人数. 
}ref[MAXN];
int n;
int skill[MAXN];
int bit[N];
void add(int i,int x)
{
    while(i<N)
    {
        bit[i]+=x;
        i+=(i&-i);
    }
}
int sum(int i)
{
    int s=0;
    while(i>0)
    {
        s+=bit[i];
        i-=(i&-i);
    }
    return s;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        memset(bit,0,sizeof(bit));
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>skill[i];
        }
        for(int i=0;i<n;i++)
        {
            int ans=sum(skill[i]);
            ref[i].lw=ans;
            ref[i].lp=i-ans;
            add(skill[i],1);
        }
        memset(bit,0,sizeof(bit));
        for(int i=n-1;i>=0;i--)
        {
            int ans=sum(skill[i]);
            ref[i].rw=ans;
            ref[i].rp=n-1-i-ans;
            add(skill[i],1);
        }
        long long res=0;
        for(int i=0;i<n;i++)
        {
            res+=(ref[i].lw*ref[i].rp);
            res+=(ref[i].lp*ref[i].rw);
        }
        cout<<res<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/program-ccc/p/5138306.html