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. Spring定时器实现(一)

    Spring定时器简单应用实现,如下: 首先.Spring配置文件: <?xml version="1.0" encoding="UTF-8"?> ...

  2. Maven在导入其他项目时报错:Plugin execution not covered by lifecycle configuration

    这几天想把Spring 攻略第二版完整的学习下,所以就在网上下载了该教材的源码,寻思边看书边练习!之前有过一些Maven开发的相关经验,觉得Maven在引入jar包上的配置还是很方便的,所以这次源码的 ...

  3. 【javascript】数组的操作

    一.常用操作 toString():把数组转换成一个字符串  toLocaleString():把数组转换成一个字符串  join():把数组转换成一个用符号连接的字符串  shift():将数组头部 ...

  4. 设计模式的征途—16.访问者(Visitor)模式

    在患者就医时,医生会根据病情开具处方单,很多医院都会存在以下这个流程:划价人员拿到处方单之后根据药品名称和数量计算总价,而药房工作人员根据药品名称和数量准备药品,如下图所示. 在软件开发中,有时候也需 ...

  5. C#多线程之旅(7)——终止线程

    先交代下背景,写<C#多线程之旅>这个系列文章主要是因为以下几个原因:1.多线程在C/S和B/S架构中用得是非常多的;2.而且多线程的使用是非常复杂的,如果没有用好,容易造成很多问题.   ...

  6. [补档]暑假集训D5总结

    %dalao 今天又有dalao来讲课,讲的是网络流 网络流--从入门到放弃:7-29dalao讲课笔记--https://hzoi-mafia.github.io/2017/07/29/27/   ...

  7. Jersey实现Restful服务

    jersey 是基于Java的一个轻量级RESTful风格的Web Services框架.以下我基于IDEA实现Restful完整Demo. 1.创建maven-web工程,后面就是正常的maven工 ...

  8. Python 文件的处理

    简单的读取文件 f.read()  是读取这个文件的所有内容 f.readline()  是读取文件的一行 .write()  会去检查这个文件是否存在,不存在则创建,存在的话,则以覆盖的方式将内容写 ...

  9. 模拟生产搭建Standby RAC实验环境(11.2.0.4 DG)

    模拟生产搭建Standby RAC实验环境(11.2.0.4 DG) 环境:RHEL 6.5 + Oracle 11.2.0.4 GI.DB 1.需求背景介绍 2.准备工作 3.主库配置 4.备库配置 ...

  10. Tp3.2提交表单与操作表单

    笔记笔记 先去建个表———— 然后把我输入的东西 存到表里: <input type="submit" id="tijiao" value="提 ...