【UVA1428】Ping pong

来源:互联网 发布:伺服电机编程 编辑:程序博客网 时间:2024/06/07 06:43

题面

  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 intusing 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;}
原创粉丝点击