【UVA1428】Ping pong

本文介绍了一个关于线段树的应用问题,具体为在给定一系列具有不同技能等级的乒乓球选手中寻找符合条件的三人组合,使得他们的技能等级构成递增或递减序列,并计算所有这样的组合数量。通过构建两棵权值线段树来实现高效计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题面

  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?

题意

  有n个人,每个人有一个权值WiWi互不相同
  一个三元组(abc)能够产生1的贡献当且仅当abc,并且WaWbWc或者WaWbWc
  问这n个人能产生多大的贡献(abccba算同一个三元组)

解法

线段树:
  易知,对于i来说,设i左边的人的WWi的人数为Ci,右边的为Di,那么答案就是ni=1DiiCi1+CiniDi
  现在问题就变成了怎么求CiDi。可以看到,CD的值取决于权值小于W的个数,所以可以种两棵权值线段树,一棵从前往后递推求C,另一棵从后往前递推求D,然后再利用上面的公式求答案即可

复杂度

O(nlogn

代码

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#define ls 2*k
#define rs 2*k+1
#define Lint long long int
using namespace std;
const int N=20010;
const int R=100000;
struct node
{
    int l,r;
    int sum;
    void clear()   { sum=0; }
}t[R*5],p[R*5];
int c[N],d[N],w[N];
int T,n;
Lint ans;
void build(int k,int l,int r)
{
    p[k].l=t[k].l=l,p[k].r=t[k].r=r;
    if( l==r )   return ;
    int mid=(l+r)/2;
    build( ls,l,mid ),build( rs,mid+1,r );
}
void insert_t(int k,int x)
{
    if( t[k].l==t[k].r )   { t[k].sum=1;return ; }
    if( x<=t[ls].r )   insert_t( ls,x );
    else   insert_t( rs,x );
    t[k].sum=t[ls].sum+t[rs].sum;
}
void insert_p(int k,int x)
{
    if( p[k].l==p[k].r )   { p[k].sum=1;return ; }
    if( x<=p[ls].r )   insert_p( ls,x );
    else   insert_p( rs,x );
    p[k].sum=p[ls].sum+p[rs].sum;
}
int query_t(int k,int l,int r)
{
    if( t[k].l==l && t[k].r==r )   return t[k].sum;
    if( r<=t[ls].r )   return query_t( ls,l,r );
    else
        if( l>=t[rs].l )   return query_t( rs,l,r );
        else   return query_t( ls,l,t[ls].r )+query_t( rs,t[rs].l,r );
}
int query_p(int k,int l,int r)
{
    if( p[k].l==l && p[k].r==r )   return p[k].sum;
    if( r<=p[ls].r )   return query_p( ls,l,r );
    else
        if( l>=p[rs].l )   return query_p( rs,l,r );
        else   return query_p( ls,l,p[ls].r )+query_p( rs,p[rs].l,r );
}
int main()
{
    build( 1,0,R );
    scanf("%d",&T);
    while( T-- )
    {
        scanf("%d",&n);
        ans=0;
        for(int i=1;i<=n;i++)
        {
            c[i]=d[i]=0;
            scanf("%d",&w[i]);
        }
        for(int i=1;i<=R*4;i++)   p[i].clear(),t[i].clear();
        for(int i=1;i<=n;i++)
        {
            c[i]=query_t( 1,0,w[i]-1 );
            insert_t( 1,w[i] );
        }
        for(int i=n;i>=1;i--)
        {
            d[i]=query_p( 1,0,w[i]-1 );
            insert_p( 1,w[i] );
        }
        for(int i=1;i<=n;i++)   ans=ans+(Lint)d[i]*(i-c[i]-1)+(Lint)c[i]*(n-i-d[i]);
        printf("%lld\n",ans);
    }
    return 0;
}
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值