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. java基础——快速排序

    今天又把以前学的快速排序拿出来回忆一下 高快省的排序算法 有没有既不浪费空间又可以快一点的排序算法呢?那就是“快速排序”啦!光听这个名字是不是就觉得很高端呢. 假设我们现在对“6 1 2 7 9 3 ...

  2. Java基础 匿名内部类 异常 多线程 集合面试题

    匿名内部类:没有名字的内部类.就是内部类的简化形式.一般只用一次就可以用这种形式.匿名内部类其实就是一个匿名子类对象.想要定义匿名内部类:需要前提,内部类必须继承一个类或者实现接口. 匿名内部类的格式 ...

  3. c++调用系统关机命令 c++调用暂停命令

    #include<stdlib.h> int main() { //调用系统dos命令 system("shutdown -s -t 120"); ; } system ...

  4. 好久没写了,总结一下lnux常用的命令(基础)

    Linux 1.init 0 关机 2.init 6  重启 3.ls 列出当前目录下的文件 4.cd  切换目录  cd -  切换最近使用的两次目录 5.pwd 查看当前所在的路径 (“-”为用户 ...

  5. com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 3 字节的 UTF-8 序列的字节 x 无效

    在启动Tomcat项目时,控制台报错:nested exception is com.sun.org.apache.xerces.internal.impl.io.MalformedByteSeque ...

  6. PHP四种序列化方案

    原文地址:https://t.ti-node.com/thread/... 数据的序列化是一个非常有用的功能,然而目测很多人跟我一样,在刚接触这玩意的时候压根就不理解这货色到底是干啥用的,反正老师说了 ...

  7. Library setup

  8. 《linux设备驱动开发详解》笔记——14 linux网络设备驱动

    14.1 网络设备驱动结构 网络协议接口层:硬件无关,标准收发函数dev_queue_xmit()和netif_rx();  注意,netif_rx是将接收到的数据给上层,有时也在驱动收到数据以后调用 ...

  9. Applied Nonparametric Statistics-lec7

    Ref: https://onlinecourses.science.psu.edu/stat464/print/book/export/html/9 经过前面的步骤,我们已经可以判断几个样本之间是否 ...

  10. The 2018 ACM-ICPC China JiangSu Provincial Programming Contest I. T-shirt

    JSZKC is going to spend his vacation! His vacation has N days. Each day, he can choose a T-shirt to ...