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. codevs 4927 线段树练习5

    赶在期末考试之前把这道傻逼题调了出来. #include<iostream> #include<cstdio> #include<cstring> #include ...

  2. typedef 深入剖析

    typedef是一个我们常常会用到的关键字,而这个关键字有许多陷阱或者说许多不为我们深入理解的地方.很多书上都是很简单地一笔代过,并没有真正地让我们理解这个关键字.本文对其进行详细地说明.综合网络上找 ...

  3. ssh 或者 scp 无需输入密码的解决办法

    这里假设主机A(192.168.100.3)用来获到主机B(192.168.100.4)的文件.   在主机A上执行如下命令来生成配对密钥: ssh-keygen -t rsa   遇到提示回车默认即 ...

  4. ios import和@class的区别

    二者的区别在于: 1.import会包含这个类的所有信息,包括实体变量和方法,而@class只是告诉编译器,其后面声明的名称是类的名称,至于这些类是如何定义的,暂时不用考虑,后面会再告诉你. 2.在头 ...

  5. Excel2007条件格式怎么用

    Excel2007的条件格式功能十分的强大实用,较2003版改进十分的大,下面我们以经验记录为例做一简单的操作示范.注意前部分有二点技巧可借鉴,即不规则选取和不规则统一填充. 工具/原料 EXCEL2 ...

  6. freemaker转word xml注意事项

    java类字符串变量如果含有以下2种字符: &和  <,必须转义否则转化将失败. 其中: &替换成 & <替换成 < 因为一些怪字符数据库存储时已转义了,从数 ...

  7. [Everyday Mathematics]20150220

    试求 $$\bex \sum_{k=0}^\infty\frac{1}{(4k+1)(4k+2)(4k+3)(4k+4)}. \eex$$

  8. 读取Assets中的文件数据

    try { // 返回的字节流 InputStream is = getResources().getAssets().open("info.txt"); // 当读取时,属于文本 ...

  9. selenium python (十五)控制滚动条操作

    #!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip' #一般用到操作滚动条的两个场景    #注册时的法律条文的阅读,判断用户 ...

  10. Solaris系统管理(二)资源管理与网络配置

    上一篇主要总结了Solaris安装后需要进行的一些设置,如ssh,pkgutil管理依赖,vim安装. 这一篇将会对Solaris资源管理与网络配置进行总结. 四 Solaris 系统管理 1,查询总 ...