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. UIKeyboardType

    typedef NS_ENUM(NSInteger, UIKeyboardType) { UIKeyboardTypeDefault, // Default type for the current ...

  2. cena 测评机下载地址

    以下是cane的下载地址,现在分享给你们,希望有所帮助 下载地址百度云:https://pan.baidu.com/s/1JBXiVSZy-jhIc0V-F2ESPA 密码:hgtk 点击下载即可. ...

  3. Java 的Throwable、error、exception的区别

    1. 什么是异常? 异常本质上是程序上的错误,包括程序逻辑错误和系统错误.比如使用空的引用(NullPointerException).数组下标越界(IndexOutOfBoundsException ...

  4. Linux学习-Tarball 的管理与建议

    使用原始码管理软件所需要的基础软件 从原始码的说明我们晓得要制作一个 binary program 需要很多咚咚的呢!这包括底下这些基础的软件: gcc 或 cc 等 C 语言编译程序 (compil ...

  5. windows基本DOS命令

    基本命令 dir : 列出当前目录下的文件以及文件夹,后面可以接其他路径 md : 创建目录(mkdir),一次创建多级目录,mkdir a\b\c rd : 删除目录,删除非空目录rd /s(删除最 ...

  6. [每日App一]QQ主题要逆天!轻轻松松月入30万!

    听从吾师秦刚(微信或QQ:1111884)酋长的建议,谋哥(微信viyi88)我开始新的征程,每日更新一篇干货文章(要坚持啊!否则被酋长鄙视了). 好了,废话不多说,今天我给大家揭秘一个你从来想也木有 ...

  7. python - log日志

    # -*- coding:utf-8 -*- ''' @project: jiaxy @author: Jimmy @file: study_logging.py @ide: PyCharm Comm ...

  8. jquery拼接字符串

    1. $("#div").append("<table><tr align='center'>" +"<td >& ...

  9. Leetcode 454.四数相加II

    四数相加II 给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0. 为了使问题简单 ...

  10. Leetcode 448.找到所有数组中消失的数字

    找到所有数组中消失的数字 给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现 ...