hdu2492(树状数组+离线处理)

Ping pong

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


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
2008 Asia Regional Beijing
 

Recommend
gaojie
       本题给定一个数的序列,要求iaj>ak的组合数。
       本题要个很直观的想法就是按ai,aj,ak的顺序暴力枚举,总的时间复杂度为O(N^3),肯定是超时的;于是有个较之有效点的方法,枚举中间aj,然后分别从两边枚举,总的时间复杂度为O(N^2),在3<=N<=20000且Time Limit: 2000/1000 MS 的情况下肯定也会超时。我们考虑考虑到将时间复杂度降为O(N)或O(N*log(N)),很明显O(N)的时间复杂度在涉及比较大小是很难达到的。在时间复杂度为O(N*log(N))的算法中选择,枚举中间位置aj,于是剩下的就是要求ij且ak>aj或iaj和k>j且ak
       先从左到右枚举aj,在aj位置加1,并求在aj左边的元数ai的个数num_left[j](可以保证i
       然后从右到左枚举aj,在aj位置加1,并求在aj左边的元数ak的个数num_right[j]((可以保证k>j且ak
       最终ans=∑((num_left[j]*(n-j-num_right[j])+((num_right[j]*(j-1-num_left[j]))))
#include
#include
#include
using namespace std;

int da[20000+10];
int num_left[20000+10],num_right[20000+10];

//************************************************************
const int MAXN=100000+100;
int C[MAXN];
int Lowbit[MAXN];

int QuerySum(int p)
//查询原数组中下标1-p的元素的和 
{ 
   int nSum=0; 
   while(p>0) 
   {  
      nSum+=C[p]; 
      p-=Lowbit[p]; 
   } 
    return nSum; 
} 

//2.修改+初始化
void Modify(int p,int val) 
//原数组中下表为p的元素+val,导致C[]数组中部分元素值的改变
{ 
    while(p<=MAXN-10) 
   { 
      C[p]+=val; 
      p+=Lowbit[p]; 
    } 
} 
//************************************************************


int main()
{
	int cas,i,n;
	__int64 ans;
	for(i=1;i>cas;
	while(cas--)
	{
		scanf("%d",&n);
		for(i=1;i<=n;i++)
			scanf("%d",&da[i]);

		memset(C,0,sizeof(C));
		for(i=1;i<=n;i++)
		{
			Modify(da[i],1);
			num_left[i]=QuerySum(da[i]-1);//前面比她小的
		}

		memset(C,0,sizeof(C));
		for(i=n;i>=1;i--)
		{
			Modify(da[i],1);
			num_right[i]=QuerySum(da[i]-1);//后面比她小的
		}

		ans=0;
		for(i=1;i<=n;i++)
		{
			ans+=(__int64)num_left[i]*(__int64)(n-i-num_right[i]);
			ans+=(__int64)(i-1-num_left[i])*(__int64)num_right[i];
		}
		printf("%I64d\n",ans);
	}
	return 0;
}

你可能感兴趣的:(数据结构-线段树(树状数组))