题目描述:

N <tex2html_verbatim_mark>(3N20000) <tex2html_verbatim_mark>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 <tex2html_verbatim_mark>(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 <tex2html_verbatim_mark>integers. The first integer is N <tex2html_verbatim_mark>, the number of players. Then N <tex2html_verbatim_mark>distinct integers a1a2...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 <tex2html_verbatim_mark>).

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

分析:对于每个a[i],算出从a[1]到a[i-1]比a[i]小的数有c[i]个,a[i+1]到a[n]比a[i]小的有d[i]个,则考虑当i为裁判时,可能的方案数为:c[i](n-i-d[i])+d[i](i-c[i]-1)。关键是算c[i]、d[i]。运用树状数组可以算出c[i],d[i]。

 #include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
using namespace std;
const int maxn=;
const int maxv=;
int a[maxn],c[maxv],cc[maxn],d[maxn],n;
int lowbit(int x){
return x&-x;
}
int sum(int x){
int res=;
while(x>){
res+=c[x];
x-=lowbit(x);
}
return res;
}
void add(int x,int v){
while(x<=maxv){
c[x]+=v;
x+=lowbit(x);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
memset(c,,sizeof(c));
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n-;i++){
cc[i]=sum(a[i]-);
add(a[i],);
}
memset(c,,sizeof(c));
for(int i=n;i>=;i--){
d[i]=sum(a[i]-);
add(a[i],);
}
ll ans=;
for(int i=;i<=n-;i++)
ans+=cc[i]*(n-i-d[i])+(i-cc[i]-)*d[i];
printf("%lld\n",ans);
}
return ;
}
												

LA 4329(树状数组)的更多相关文章

  1. LA 4329 (树状数组) Ping pong

    第一次写树状数组,感觉那个lowbit位运算用的相当厉害. 因为-x相当于把x的二进制位取反然后整体再加上1,所以最右边的一个1以及末尾的0,取反加一以后不变. 比如1000取反是0111加一得到10 ...

  2. poj Ping pong LA 4329 (树状数组统计数目)

    Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2302   Accepted: 879 Descript ...

  3. 算法竞赛入门经典 LA 4329(树状数组)

    题意: 一排有着不同能力值的人比赛,规定裁判的序号只能在两人之间,而且技能值也只能在两人之间 问题: <算法竞赛入门经典-训练指南>的分析: 上代码: #include<iostre ...

  4. UVALive 4329 树状数组第二题

    大白书上的题目,比较巧妙的是其分析,为了求某个i点做裁判的时候的情况数,只要知道左边有多少比它小的记为ansc,右边有多少比它小的记为ansd,则总种数,必定为 ansc*(右边总数-ansd)+an ...

  5. TTTTTTTTTTTTT LA 2191 树状数组 稍修改

    题意:给出n个数字,操作有修改(S)和输出区间和(M). #include <iostream> #include <cstdio> #include <cstring& ...

  6. LA 4329 Ping pong 树状数组

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

  7. LA 4329 ping-pong树状数组

    题目链接: 刘汝佳,大白书,P197. 枚举裁判的位置,当裁判为i时,可以有多少种选法,如果已经知道在位置i之前有ci个数比ai小,那么在位置i之前就有i-1-ci个数比ai大. 在位置i之后有di个 ...

  8. 树状数组 LA 4329 亚洲赛北京赛区题

    复习下树状数组 还是蛮有意思的一道题: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&cat ...

  9. LA 4329(树状数组)

    算法竞赛入门经典 p197 题目大意: 一条大街上住着n个乒乓球爱好者.常常比赛切磋技术.每一个人都有一个不同的技能值a[i].每场比赛须要3个人:两名选手,一名裁判.他们有个奇怪的约定,裁判必须住在 ...

随机推荐

  1. C51 的编程规范

    编程首要是要考虑程序的可行性,然后是可读性.可移植性.健壮性以及可测试性.这是总则.但是很多人忽略了可读性.可移植性和健壮性(可调试的方法可能歌不相同),这是不对的. 1.当项目比较大时,最好分模块编 ...

  2. 国内外最全的asp.net开源项目

    转自:http://www.cnblogs.com/fengtangquan/archive/2010/10/19/1855472.html 最近一些项目开始用到CMS系统,最开始是研究JAVA的,无 ...

  3. HDU 5935 Car 【模拟】 (2016年中国大学生程序设计竞赛(杭州))

    Car Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  4. DevExpress控件汉化类 z

    更新了一些字段,VER9.3.3 using System; using DevExpress.XtraEditors.Controls; using DevExpress.XtraGrid.Loca ...

  5. ATL ActiveX 非管理员权限发布(支持vs2005)

    在win7系统中,vs2005开发的atl activex需要管理员权限才能注册. 解决方法: PerUserRegistration.h #pragma once class PerUserRegi ...

  6. Good Numbers

    Problem Description If we sum up every digit of a number and the result can be exactly divided by 10 ...

  7. HDOJ/HDU 1328 IBM Minus One(水题一个,试试手)

    Problem Description You may have heard of the book '2001 - A Space Odyssey' by Arthur C. Clarke, or ...

  8. 解析XML文件示例

    项目中要解析Xml文件,于是在工程里找了下前人写例子. 1,SAX(基于事件,效率高,使用声明加载什么). public class MVCConfig { private static MVCCon ...

  9. ubuntu-15.04 下编译 ijkplayer 安卓版

    我的环境: 1.win7 主系统 2.VirtualBox 5.1.2 虚拟机 3.ubuntu-15.04-desktop-amd64.iso 镜像文件 4.android-ndk-r10e-lin ...

  10. How to effectively work with multiple files in Vim?

    Why not use tabs (introduced in Vim 7)? You can switch between tabs with :tabn and :tabp, With :tabe ...