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. 在Visual Studio 2017中发现的问题

    最近抽时间学习了一下 C# 6.0 的新特性,其中的一个新特性是 ?. 运算符,请看下面的一个简单示例: 当我故意修改成错误代码的时候,请接着看下面的示例: 我想把代码修改成list?[0].Coun ...

  2. 【MYSQL】解决Mysql直接登录问题(删除匿名用户)(转)

    刚安装的Mysql会存在匿名用户. 在命令行下输入mysql,(如果这时提示不是外部或内部指令,那就把mysql server文件下的bin目录添加到系统路径Path中) 如果没有任何提示,直接进入& ...

  3. 17.leetcode 237. Delete Node in a Linked List

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  4. Ubuntu超好用软件:剪贴板

    这个软件的作用就是保存你复制或者剪贴的内容 对于经常复制文字的Ubuntu使用者作用还是比较大的 下面附上安装代码 安装: sudo add-apt-repository ppa:shantzu/cl ...

  5. Django 最好的缓存memcached的使用 小记

    嗯,咳咳咳咳咳,这是在win平台上.linux平台上没折腾 首先你要在win上安装 Memcache 下载 :点我下载 里面有x64 和 x86 的.根据自己的系统版本来选择使用   使用命令行: 1 ...

  6. 微信小程序开发之微信支付

    微信支付是小程序开发中很重要的一个环节,下面会结合实战进行分析总结 环境准备 https服务器 微信小程序只支持https请求,因此需要配置https的单向认证服务(请参考 另一篇文章https受信证 ...

  7. Java Struts图片上传至指定文件夹并显示图片

    继上一次利用Servlet实现图片上传,这次利用基于MVC的Struts框架,封装了Servlet并简化了JSP页面跳转. JSP上传页面 上传一定要为form加上enctype="mult ...

  8. Eclipse中将hadoop项目放在集群中运行

    1.加入配置文件到项目源码目录下(src) <configuration> <property> <name>mapreduce.framework.name< ...

  9. Luogu P3371 【模板】单源最短路径

    题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含三个整数N.M.S,分别表示点的个数.有向边的个数.出发点的编号. 接下来M行每行包含三 ...

  10. crm踩坑记(二)

    Linux tmux 如何查看 tmux如何进行滚动呢? prefix + [, prefix为tmux的前置动作,默认是ctrl + b. 使用方向键或者pageUp来进行翻页. q可以退出滚动模式 ...