HDU-2492 pingpong(树状数组)

Ping pong

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5456    Accepted Submission(s): 2031


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
 
Source
 
Recommend
gaojie
 
题目大意:给一列数,第一个数是人数n,后面n个数依次是每个人的能力排名,每两个人比赛,要找第三个人当裁判,第三个能力必须在两人之间并且位置也必须在两人之间。三个人其中任意一个人不同都算不同的比赛,求一共有多少比赛。
 
解题思路:以第三个人为主体,求出每个人前面有多少人排名比自己高和低,求出每个人后面有多少人排名比自己高和低,然后 (前高 * 后低)+ (前低 * 后高) 就是此人当裁判的场数。相加即可。代码比较清晰,直接上代码。(可还是被long long 坑了几次。。)
 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn=5*1e5+5;
long long n;
long long a[maxn],a1[maxn],a2[maxn],b1[maxn],b2[maxn],c[maxn];

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

void update(long long x,long long value)
{
    for(;x<=maxn;x+=lowbit(x))
    {
        c[x] += value;
    }
}

long long getsum(long long x)
{
    long long sum=0;
    for(;x>0;x-=x&(-x))
        sum += c[x];
    return sum;
}

int main()
{
    int i,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&n);
        memset(c,0,sizeof(c));
        for(i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            update(a[i],1);
            a1[i] = getsum(a[i]-1);//
            a2[i] = i-a1[i]-1;////printf("a1[%d]--%d  a2[%d]--%d
",i,a1[i],i,a2[i]);
        }
        memset(c,0,sizeof(c));
        for(i=n;i>0;i--)
        {
            update(a[i],1);
            b1[i] = getsum(a[i]-1);//
            b2[i] = n-i-b1[i];////printf("b1[%d]--%d  b2[%d]--%d
",i,b1[i],i,b2[i]);
        }
        long long ans = 0;
        for(i=1;i<=n;i++)
        {
            ans += a1[i]*b2[i]+a2[i]*b1[i];
        }
        printf("%lld
",ans);
    }
}
原文地址:https://www.cnblogs.com/WWkkk/p/7375026.html