hdu 2492 Ping pong 数状数组 求左面比其小、大,右面比其大、小的数的个数

Ping pong

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

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
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int c[100002];
int n;
int lowbit(int x)
{
    return x&(-x);
}
void update(int x,int val)
{
    for(int i=x;i<=100001;i+=lowbit(i))
    {
        c[i]+=val;
    }
}
int getsum(int x)
{
    int cnt=0;
    for(int i=x;i>=1;i-=lowbit(i))
    {
        cnt+=c[i];
    }
    return cnt;
}
int a[100001];
int leftmin[100001],rightmax[100001];
int main()
{
    int ci;scanf("%d",&ci);
    while(ci--)
    {
        memset(c,0,sizeof(c));
        memset(leftmin,0,sizeof(leftmin));
        memset(rightmax,0,sizeof(rightmax));
        scanf("%d",&n);
        for(int i=1;i<=n;i++) cin>>a[i];
        for(int i=1;i<=n;i++)
        {
            leftmin[i]=getsum(a[i]);
            update(a[i],1);
        }
        memset(c,0,sizeof(c));
        for(int i=n;i>=1;i--)
        {
            rightmax[i]=getsum(100001)-getsum(a[i]);
            update(a[i],1);
        }
        //for(int i=1;i<=n;i++) cout<<leftmin[i]<<"..."<<rightmax[i]<<endl;
        long long cnt=0;
        for(int i=2;i<n;i++)
        {
            cnt+=(leftmin[i]*rightmax[i]+(i-1-leftmin[i])*(n-i-rightmax[i]));
        }
        cout<<cnt<<endl;
    }
    return 0;
}
/*
6 6
1 5 4 3 2 6
*/

你可能感兴趣的:(hdu 2492 Ping pong 数状数组 求左面比其小、大,右面比其大、小的数的个数)