Ping pong(线段树入门)

http://poj.org/problem?id=3928

Ping pong

Time Limit:1000MS

Memory Limit:65536K

Total Submissions:1253

Accepted:470

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

3 1 2 3

Sample Output

1

Source

Beijing 2008

解析:这题其实就是《算法竞赛入门经典》的P197,说的是一条大街上有n个乒乓球爱好者,每个人都技能值ai,每场比赛许3人,其中两名选手,一名裁判,裁判的能力要在选手之间,求一共能组织多少组;

思路:典型的线段树法,书中的思路已经非常清楚,套用模板,注意细节即可;

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
const int maxn=100000+10;
__int64 a[20010];
__int64 c[maxn],d[maxn];
__int64 x1[20010],x2[20010],y1[20010],y2[20010];
__int64 lowbit(__int64 x)
 {
 return x&(x^(x-1));
 }
 __int64 sum1(__int64 x)//不重复覆盖区域求和 
  {
  __int64 ret=0;
  while(x>0)
   {
    ret+=c[x];
    x-=lowbit(x);
   }
   return ret;
  }
__int64 sum2(__int64 x)
  {
  __int64 ret=0;
  while(x>0)
   {
    ret+=d[x];
    x-=lowbit(x);
   }
   return ret;
  }
 void add1(__int64 x )//将x所覆盖范围全部修改 
   {
    while(x<=100000)
     {
     c[x]++;
     x+=lowbit(x);
     }
}
void add2(__int64 x )
   {
    while(x<=100000)
     {
     d[x]++;
     x+=lowbit(x);
     }
}
int main()
{
__int64 t,i,n,ans;
scanf("%I64d",&t);
while(t--)
 {     memset(c,0,sizeof(c));
   memset(d,0,sizeof(d));
   ans=0;
  scanf("%I64d",&n);//printf("%I64d \n",n);
  for(i=1;i<=n;i++)
   {
   scanf("%I64d",&a[i]);
   add1(a[i]);//初始化 
   x1[i]=sum1(a[i]-1);
   y1[i]=sum1(100000)-sum1(a[i]);
   }
   for(i=n;i>=1;i--)
   {
   add2(a[i]);
   x2[i]=sum2(a[i]-1);
   y2[i]=sum2(100000)-sum2(a[i]);
  // printf("%I64d ",a[i]);
   }
   for(i=2;i<=n;i++)
   ans+=x1[i]*y2[i]+x2[i]*y1[i];
   printf("%I64d\n",ans);
 }
 //system("pause");


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值