N (3N20000)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(1T20) <tex2html_verbatim_mark>, indicating the number of test cases, followed by T<tex2html_verbatim_mark>lines each of which describes a test case.

Every test case consists of N + 1 integers. The first integer is N <tex2html_verbatim_mark>, the number of players. Then N distinct integersa1a2...aN <tex2html_verbatim_mark>follow, indicating the skill rank of each player, in the order of west to east ( 1ai100000 <tex2html_verbatim_mark>, 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

题目大意:
一条大街上住着n个乒乓球爱好者,经常组织比赛切磋技术。每个人都有一个能力值a[i]。每场比赛需要三个人:两名选手,一名裁判。他们有个奇怪的约定,裁判必须住在两名选手之间,而裁判的能力值也必须在两名选手之间。问一共能组织多少种比赛。 分析:
考虑第i个人,假设a1到ai-1中有ci个比ai小,那么就有(i-1)-ci个比ai大;同理,如果ai+1到an中有di个比ai小,那么就有(n-i)-di个比ai大。更具乘法原理和加法原理,i当裁判就有
ci * (n-i-di) + (i-1-ci)*di
种比赛。(感觉这种思路简直碉堡了) 然后问题就转化为了计算数组c和数组d。这样的话就很容易想到使用树状数组去计算前缀和。
见代码:
 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout) template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } //typedef __int64 LL;
typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-; int T, n, a[MAXN], c[MAXN], d[MAXN], x[MAXM]; int lowbit(int x)
{
return x & (-x);
} int getSum(int k)
{
int ans = ;
while(k>)
{
ans += x[k];
k -= lowbit(k);
}
return ans;
} void edit(int k)
{
while(k <= )
{
x[k] += ;
k += lowbit(k);
}
} int main()
{
// FOPENIN("in.txt");
// FOPENOUT("out.txt");
while(~scanf("%d", &T)) while(T--)
{
mem0(x); mem0(c); mem0(d);
scanf("%d", &n);
for(int i=; i<=n; i++) scanf("%d", &a[i]);
for(int i=; i<=n; i++) { edit(a[i]); c[i] = getSum(a[i]-); }
mem0(x);
for(int i=n; i>=; i--) { edit(a[i]); d[i] = getSum(a[i]-); }
LL ans = ;
for(int i=;i<n;i++)
{
ans += (LL)c[i]*(n-i-d[i]) + (LL)d[i]*(i-c[i]-);
}
printf("%lld\n", ans);
}
return ;
}

LA4329 Ping pong(树状数组与组合原理)的更多相关文章

  1. LA4329 Ping pong 树状数组

    题意:一条大街上住着n个乒乓球爱好者,经常组织比赛切磋技术.每个人都有一个能力值a[i].每场比赛需要三个人:两名选手,一名裁判.他们有个奇怪的约定,裁判必须住在两名选手之间,而裁判的能力值也必须在两 ...

  2. Ping pong(树状数组经典)

    Ping pong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. poj3928 Ping pong 树状数组

    http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  4. UVA 1428 - Ping pong(树状数组)

    UVA 1428 - Ping pong 题目链接 题意:给定一些人,从左到右,每一个人有一个技能值,如今要举办比赛,必须满足位置从左往右3个人.而且技能值从小到大或从大到小,问有几种举办形式 思路: ...

  5. LA 4329 Ping pong 树状数组

    对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...

  6. UVALive - 4329 Ping pong 树状数组

    这题不是一眼题,值得做. 思路: 假设第个选手作为裁判,定义表示在裁判左边的中的能力值小于他的人数,表示裁判右边的中的能力值小于他的人数,那么可以组织场比赛. 那么现在考虑如何求得和数组.根据的定义知 ...

  7. POJ 3928 Ping pong 树状数组模板题

    開始用瓜神说的方法撸了一发线段树.早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> ...

  8. HDU 2492 Ping pong (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=2000 ...

  9. LA 4329 - Ping pong 树状数组(Fenwick树)

    先放看题传送门 哭瞎了,交上去一直 Runtime error .以为那里错了. 狂改!!!!! 然后还是一直... 继续狂改!!!!... 一直.... 最后发现数组开小了.......... 果断 ...

  10. hdu 6203 ping ping ping(LCA+树状数组)

    hdu 6203 ping ping ping(LCA+树状数组) 题意:给一棵树,有m条路径,问至少删除多少个点使得这些路径都不连通 \(1 <= n <= 1e4\) \(1 < ...

随机推荐

  1. 使用MFC中的AfxBeginThread创建多线程

    创建一个基于对话框的工程,工程名为CreateThreadRect   在CreateThreadRect.cpp中增加一个ThreadProc函数,代码如下   工作者线程的函数必须是全局函数或静态 ...

  2. Openerp上传中文名附件,下载时报错的处理方法

    文档管理中,如果上传的文件名含有中文字符,下载时会提示出错,如没有权限等.这个问题困惑我比较久的时间,通过跟踪openerp_server.log,可以看到类似提示: 2012-09-28 21:51 ...

  3. XenServer6.2详细安装步骤

    系统要求 系统要求 XenServer 至少需要两台单独的 x86 物理计算机:一台用作 XenServer 主机,另一台用于运行XenCenter 应用程序. XenServer 主计算机完全专用于 ...

  4. BZOJ 2120/BZOJ 2453

    分块傻逼题. memset很慢的...而且其实也没有用.... #include<iostream> #include<cstdio> #include<cstring& ...

  5. 利用matlab编写实现显示fmri切片slice图像 混合显示 不同侧面显示 可叠加t检验图显示 by DR. Rajeev Raizada

    1.参考 reference 1. tutorial主页:http://www.bcs.rochester.edu/people/raizada/fmri-matlab.htm. 2.speech_b ...

  6. (六)6.7 Neurons Networks whitening

    PCA的过程结束后,还有一个与之相关的预处理步骤,白化(whitening) 对于输入数据之间有很强的相关性,所以用于训练数据是有很大冗余的,白化的作用就是降低输入数据的冗余,通过白化可以达到(1)降 ...

  7. Spring中@Resource与@Autoware

    问题 这其实就是@Autoware与@Resource没有正确的使用,这个错误是因为wmPoiOplogService这个变量装配方式是@Resource,按照@Resource的按名字查找的方式,并 ...

  8. 在SQL语言中,join什么时候用,什么时候不用啊?请高手举例解释一下。谢谢

    JOIN 在内连接时,可以不使用,其它类型连接必须使用.如SELECT * FROM TABLEA INNER JOIN TABLEB ON A.ID=B.ID可以这样写:SELECT * FROM ...

  9. 深入理解JavaScript闭包(closure)

    最近在网上查阅了不少javascript闭包(closure)相关的资料,写的大多是非常的学术和专业.对于初学者来说别说理解闭包了,就连文字叙述都很难看懂.撰写此文的目的就是用最通俗的文字揭开Java ...

  10. PHP include()和require()方法的区别

    本文总结了PHP的include()和require()两种包含外部文件的方法的不同之处.基本上就是,加载失败的处理方法,性能,以及使用弹性方面的不同. PHP的include()和require() ...