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
题意:见白书p197 ,注意没有两个人的rank是一样的
思路:利用树状数组,树状数组教程(盗链)http://www.cnblogs.com/zhangshu/archive/2011/08/16/2141396.html
考虑第i个人当裁判的情形。假设i左边有ci个比ai小,那么就有(i-1)-ci个比ai大;
同理:假设右边有di个比ai小,那么就有(n-i)-di个比ai大。所以当i当裁判时,有ci(n-i-di)+(i-ci-1)di种比赛。
建立一个大小<=max(ai)的树状数组,全赋0。从左到右扫描数轴,扫描到第i个人时,add(a[i],1)。我们可以发现,此时在ci的值即为sum(a[i]-1)。因为以后这个值会发生变化,所以我们用一个数组把它存起来。全部扫完一遍之后,我们又可以发现,di的值为sum(a[i]-1)-ci。求解结束。
 /*
* Author: Joshua
* Created Time: 2014年07月13日 星期日 14时09分45秒
* File Name: poj3928.cpp
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; #define maxv 100005
#define maxn 20005
typedef long long LL; int c[maxv],n,vv;
int lowBit(int x)
{
return x&(-x);
} void add(int x,int v)
{
while (x<=vv)
{
c[x]+=v;
x+=lowBit(x);
}
} int sum(int x)
{
int ret=;
while (x>)
{
ret+=c[x];
x-=lowBit(x);
}
return ret;
} void solve()
{
int a[maxn],temp[maxv];
LL ans=;
memset(c,,sizeof(c));
scanf("%d",&n);
vv=;
for (int i=;i<=n;++i)
{
scanf("%d",&a[i]);
vv=max(vv,a[i]);
}
for (int i=;i<=n;++i)
{
add(a[i],);
temp[i]=sum(a[i]-);
}
for (int i=;i<=n;++i)
{
int ts=sum(a[i]-);
ans+=temp[i]*(n-i-(ts-temp[i]));
ans+=(i--temp[i])*(ts-temp[i]);
}
printf("%lld\n",ans);
} int main()
{
int T;
scanf("%d",&T);
while (T)
{
solve();
T--;
}
return ;
}
     

poj3928 la4329 pingpong的更多相关文章

  1. POJ3928 Pingpong(统计比 K 小的个数 + 树状数组)

    Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2691   Accepted: 996 Descript ...

  2. POJ3928、LA4329【树状数组】

    借此题试验一下各种做法的效果~ 这题为ACM2008北京站某题,介于简单与中等之间,做出来,罚时不多基本可以铜了,所以这样的题还必须得会,进阶之路. add(a[i]+1,1)这样处理之后,再用sum ...

  3. Ping-Pong (Easy Version)(DFS)

    B. Ping-Pong (Easy Version) time limit per test 2 seconds memory limit per test 256 megabytes input ...

  4. 每日英语:Mrs. Obama Takes Stab at Ping-Pong Diplomacy

    U.S. first lady Michelle Obama took ping-pong diplomacy to a new level on Friday on her weeklong tou ...

  5. English trip M1 - PC7 Can I Borrow Your Ping-Pong? Teacher:Patrick

    In this lesson you will learn to desribe abilities.  这节课你将学习到描述你的能力 课上内容(Lesson) 三种常见情态动词 can aux. 能 ...

  6. 4.3之后的PingPong效果实现

    旧版本的Unity提供Animation编辑器来编辑物理动画. 在其下方可以设置动画是Loop或者是Pingpong等运动效果. 但是,在4.3之后,Unity的动画系统发生了较大的变化. 相信很多童 ...

  7. 二叉索引树,LA2191,LA5902,LA4329

    利用了二进制,二分的思想的一个很巧妙的数据结构,一个lowbit(x):二进制表示下的最右边的一个1开始对应的数值. 那么如果一个节点的为x左孩子,父亲节点就是 x + lowbit(x),如果是右孩 ...

  8. ping-pong buffer

    1 什么是pingpong? pingpong是一种数据缓存的手段,通过pingpong操作可以提高数据传输的效率. 2 什么时候需要pingpong? 在两个模块间交换数据时,上一级处理的结果不能马 ...

  9. 面试题。线程pingpong的输出问题

    第一种情况:public class Main { public static void main(String args[]) { Thread t = new Thread() { public ...

随机推荐

  1. USB基础知识

    Q: USB是什么? A: USB是通用串行总线(Universal Serial Bus)的缩写. Q: USB的优点有哪些? A: ① 支持热插拔:(hot-plugging或Hot Swap)即 ...

  2. 机器学习 —— 基础整理(三)生成式模型的非参数方法: Parzen窗估计、k近邻估计;k近邻分类器

    本文简述了以下内容: (一)生成式模型的非参数方法 (二)Parzen窗估计 (三)k近邻估计 (四)k近邻分类器(k-nearest neighbor,kNN) (一)非参数方法(Non-param ...

  3. Spring源码情操陶冶-AbstractApplicationContext#initMessageSource

    承接前文Spring源码情操陶冶-AbstractApplicationContext#registerBeanPostProcessors 约定web.xml配置的contextClass为默认值X ...

  4. css简单实现五角星评分、点赞收藏、展示评分(半颗星、1/3颗星)

    1.前言 之前做的好几个项目中,都会遇到打分,评分,点赞这样的需求,写了很多次,每次需要再写的时候,就会翻出之前写过的代码,然后copy过来.总觉得这样的话没有进步,没有把知识放进脑袋里,所以,自己花 ...

  5. Nlpir Parser敏感词搜索灵玖语义技术应用

    近年来随着网络技术的飞速发展和用户的剧烈增长,网络传输数据量越来越大,网络用语越来越趋于多样化.如何快速的屏蔽用户的不当言论.过滤用户发表内容中的非法词汇已成为关键词匹配领域的一项重大难题. 目前主要 ...

  6. JavaScript基本数据类型

    JavaScript基本数据类型 在JavaScript种一共有6种数据类型:Null.Undefined.Boolean.String.Number.Object.其中Object是一种复杂数据类型 ...

  7. rpm体系下的linux安装httpd+mysql+…

    一.安装apache 在rpm体系下,apache称为httpd. yum install httpd 即可! 二.安装mysql yum install mysql 三.安装mysql-server ...

  8. python 密码学编程 -- 2

    接上一篇随笔 ******************************************************************** * quote : "http://i ...

  9. JavaScript:int string 相互转化

    A.把int型转换成string型 (1) var   x=100    a   =   x.toString()      (2) var   x=100;    a   =   x   +&quo ...

  10. MacOS下安装gdb、mgo

    安装gdb:http://blog.panks.me/posts/2013/11/install-gdb-on-os-x-mavericks-from-source/ 注意最后两步: killall ...