http://acm.hdu.edu.cn/showproblem.php?pid=2492
这一题搞懂了基本树状数组你也就明白什么回事了
Ping pong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4869 Accepted Submission(s): 1775
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?
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).
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
#include <cstdio>
#include <cstring>
int n;
int a[20010];
int c[100010];
int lmin[20010],lmax[20010];
int rmin[20010],rmax[20010];
int bit(int t){
return t&(-t);
}
void update(int pos,int num){
while(pos<=100010){
c[pos]+=num;
pos+=bit(pos);
}
}
int getsum(int pos){
int ans=0;
while(pos>0){
ans+=c[pos];
pos-=bit(pos);
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
__int64 ans=0;
memset(c,0,sizeof(c));
for(int i=0;i<n;i++){
lmin[i]=getsum(a[i]);//在第i+1个元素a[i]出现之前的元素中比a[i]小的有几个
lmax[i]=i-lmin[i];
update(a[i],1);
}
memset(c,0,sizeof(c));
for(int i=n-1;i>=0;i--){
rmin[i]=getsum(a[i]);
rmax[i]=n-1-i-rmin[i];
update(a[i],1);
// printf("%d %d\n",rmin[i],rmax[i]);
}
for(int i=0;i<n;i++)
ans+=rmin[i]*lmax[i]+lmin[i]*rmax[i];
printf("%I64d\n",ans);
}
return 0;
}
AC之路,我选择坚持~~
本文探讨如何利用树状数组解决竞赛匹配问题,详细解释了树状数组的基本概念及应用,通过实例演示了如何计算不同竞赛游戏的数量。
6万+

被折叠的 条评论
为什么被折叠?



