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. 监控电脑CPU,内存,文件大小,硬盘空间,IP,用户名

    public class MonitorTools { /// <summary> /// 获取具体进程的内存,线程等参数情况 /// </summary> /// <p ...

  2. 01_3_创建一个Servlet

    01_3_创建一个Servlet 1.创建一个Servlet import java.io.IOException; import java.io.PrintWriter; import javax. ...

  3. linux 下使用 curl 访问带多参数,GET掉参数解决方案

    url 为 http://mywebsite.com/index.php?a=1&b=2&c=3 web形式下访问url地址,使用 $_GET是可以获取到所有的参数 curl  -s  ...

  4. C++ 学习笔记(三)string 类

    在C语言中如果想要使用字符串那么有两种方法: 1.定义char型数组:char[10]; 然后将每个字符填充到对应的位置. 优点:这种方式将字符串放在内存所以每个位置都可以修改. 缺点:赋值比较麻烦, ...

  5. Goroutine 中执行匿名函数 坑

    //相对应for 循环 goroutine跑到慢 所以这里很大概率只会打印最后一条数据 func goRun() { values := []int{1, 2, 3} for _, v := rang ...

  6. excel日期格式取年份

    具体思路:先将日期格式更改为常规格式,再取常规格式的前4位数字 例如:A1==1981/12/22 第一步B1=TEXT(A1,"emd") 第二步C1=LEFT(B1,4) 结束

  7. /dev/sda is apparently in use by the system; will not make a filesystem here!解决方法

    /dev/sda  is apparently in use by the system; will not make a filesystem here! 翻译:系统显然在使用,不会在这里做文件系统 ...

  8. UVa 1407 树形背包 Caves

    这道题可以和POJ 2486 树形背包DP Apple Tree比较着来做. 参考题解 #include <iostream> #include <cstdio> #inclu ...

  9. 学习正则有感by魔芋(命名问题)

    魔芋: 事实上,我是反感一些特殊的名词.一些名词看上去就让人感觉到抗拒. 关于一个概念用不同的名词来定义,简直是太糟糕了. 举个例子: 匹配一个后面带有exp2的exp1的正则. 写法: exp1(? ...

  10. Selenium WebDriver-操作复选框

    #encoding=utf-8 import unittest import time import chardet from selenium import webdriver class Visi ...