Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu

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

 
枚举每一个位置x作为裁判,ans+=(x左边能力值比x小的人数)*(x右边能力值比x大的人数)+(x右边能力值比x小的人数)*(x左边能力值比x大的人数)
将读入的每个能力值记录id,按能力值从小到大排序,用树状数组维护上述人数并计算答案即可
 
极限情况下答案为C(n,3),超过int范围,需要long long
 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n;
LL t[mxn+];
struct node{
int x;
int id;
}a[mxn];
int cmp(const node a,const node b){
return a.x<b.x;
}
int lowbit(int x){return x&-x;}
void add(int p,int v){//k==0 个数 k==1 距离
while(p<=mxn){t[p]+=v;p+=lowbit(p);}
}
LL smm(int x){
LL res=;
while(x){res+=t[x];x-=lowbit(x);}
return res;
}
int T;
int main(){
scanf("%d",&T);
int i,j;
while(T--){
memset(t,,sizeof t);
scanf("%d",&n);
for(i=;i<=n;i++){
a[i].x=read();
a[i].id=i;
}
LL ans=;
sort(a+,a+n+,cmp);
add(a[].id,);
for(i=;i<n;i++){
int ldn=smm(a[i].id);//所有id小于当前id,且x小于当前x的
int lup=a[i].id--ldn;//所有id小于当前id,但x大于当前x的
int rdn=(smm(mxn)-ldn);//所有id大于当前id,且x小于当前x的
int rup=n-rdn-a[i].id;//所有id大于当前id,且x大于当前x的
ans+=ldn*rup+lup*rdn;
add(a[i].id,);
}
printf("%lld\n",ans);
}
return ;
}

POJ3928 Ping pong的更多相关文章

  1. poj3928 Ping pong 树状数组

    http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  2. HDU 2492 Ping pong (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=2000 ...

  3. UVALive 4329 Ping pong

                                      Ping pong Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Fo ...

  4. POJ 3928 Ping pong(树状数组)

                                                                          Ping pong Time Limit: 1000MS   ...

  5. LA4329 Ping pong(树状数组与组合原理)

    N (3N20000)ping pong players live along a west-east street(consider the street as a line segment). E ...

  6. Ping pong

    Ping pong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  7. POJ 3928 Ping pong

    题目链接:http://poj.org/problem?id=3928 乒乓比赛,有N个人参加,输入每个玩家的技能等级,对每个人设置一个特定ID和一个技能值,一场比赛需要两个选手和一个裁判,只有当裁判 ...

  8. Frequent values && Ping pong

    Frequent values 题意是不同颜色区间首尾相接,询问一个区间内同色区间的最长长度. 网上流行的做法,包括翻出来之前POJ的代码也是RMQ做法,对于序列上的每个数,记录该数向左和向右延续的最 ...

  9. 【暑假】[实用数据结构]UVAlive 4329 Ping pong

    UVAlive 4329 Ping pong 题目: Ping pong Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: % ...

随机推荐

  1. javaweb基础(12)_session详解

    一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...

  2. linux更新git

    在CentOS中使用yum install git安装的git是1.7版本的,所以需要更新1.9以及更高版本的git. 安装方法如下: 1.安装依赖的包: yum -y install curl-de ...

  3. vue-awesome-swiper插件爬坑

    最近自己在做一个基于vue的知乎的移动端单页面,遇到很多坑,先说一下遇到最大的坑,其实并不推荐使用 vue-awesome-swiper,如果项目应用轮播,切换少的话.言归正传,现在来介绍vue-aw ...

  4. Jenkins忘记管理员密码处理

    1.先找到jenkins安装目录打开config.xml文件. 2.然后编辑,删除以下部分: <useSecurity>true</useSecurity> <autho ...

  5. angular5 HttpInterceptor使用

    HttpInterceptor接口是ng的http请求拦截器,当需要拦截http请求,可以实现该接口. 1.创建HttpInterceptor 的实现类,并使用@Injectable()注解 @Inj ...

  6. Linux 常用命令(三)

    一.less --分页查看文件:方面查阅(编辑)大文件 说明:支持方向键盘和鼠标向上向下浏览 -N 显示行号 二.head --output the first  part of files 默认显示 ...

  7. CDH4 journalnode方式手工安装手册之三

    一.                                启动JournalNode 每台机器都要执行: mkdir -p /smp/hadoop-cdh4/bch/ chmod -R 77 ...

  8. python偏函数使用

    偏函数依托于python functools模块.

  9. python基础学习笔记——运算符

    计算机可以进行的运算有很多种,可不只加减乘除这么简单,运算按种类可分为算数运算.比较运算.逻辑运算.赋值运算.成员运算.身份运算.位运算,今天我们暂只学习算数运算.比较运算.逻辑运算.赋值运算 算数运 ...

  10. JSON Undefined 问题

    在IE6和IE7浏览器下或在IE8-IE10浏览器文档模式为IE7及以下时,控制台会报错:JSON is undefined. 这种错误在IE6和IE7浏览器下出现很正常,因为JSON在IE8+浏览器 ...