Ping pong
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2641   Accepted: 978

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
题目大意:给定几个ping pong玩家,每个玩家都有唯一的技能值。每个玩家按东西方向排成一列。问从中选两个玩家,再选一个裁判。裁判在位置上在两个玩家之间(当然不能是其中一个玩家)。裁判的技能 值必须在两个玩家之间(注意:每个玩家的技能值都是唯一的)。三个人组成一个组合,只要其中有一个人不同就算不同的组合,问共有多少种组合?
思路:如果从玩家的角度解题,那么会TLE。从裁判的角度看,就是从找出位置在其左方且技能值比其小的玩家个数 x 位置在其右方且技能值比其大的玩家个数 + 位置在其左方且技能值比其大的玩家个数 x 位置在其右方且技能值比其小的玩家个数。
#include <iostream>
#include <string.h>
using namespace std;
const int MAXN=;
const int N=;
struct Node{
int lp,lw,rp,rw;//分别存储左边技能比其高,低的人数和右边技能比其高,低的人数.
}ref[MAXN];
int n;
int skill[MAXN];
int bit[N];
void add(int i,int x)
{
while(i<N)
{
bit[i]+=x;
i+=(i&-i);
}
}
int sum(int i)
{
int s=;
while(i>)
{
s+=bit[i];
i-=(i&-i);
}
return s;
}
int main()
{
int T;
cin>>T;
while(T--)
{
memset(bit,,sizeof(bit));
cin>>n;
for(int i=;i<n;i++)
{
cin>>skill[i];
}
for(int i=;i<n;i++)
{
int ans=sum(skill[i]);
ref[i].lw=ans;
ref[i].lp=i-ans;
add(skill[i],);
}
memset(bit,,sizeof(bit));
for(int i=n-;i>=;i--)
{
int ans=sum(skill[i]);
ref[i].rw=ans;
ref[i].rp=n--i-ans;
add(skill[i],);
}
long long res=;
for(int i=;i<n;i++)
{
res+=(ref[i].lw*ref[i].rp);
res+=(ref[i].lp*ref[i].rw);
}
cout<<res<<endl;
}
return ;
}

POJ3928(树状数组:统计数字出现个数)的更多相关文章

  1. POJ3067(树状数组:统计数字出现个数)

    Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24151   Accepted: 6535 Descriptio ...

  2. POJ2481(树状数组:统计数字 出现个数)

    Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 15405   Accepted: 5133 Description ...

  3. HDU 5997 rausen loves cakes(启发式合并 + 树状数组统计答案)

    题目链接  rausen loves cakes 题意  给出一个序列和若干次修改和查询.修改为把序列中所有颜色为$x$的修改为$y$, 查询为询问当前$[x, y]$对应的区间中有多少连续颜色段. ...

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

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

  5. [bzoj1901][zoj2112][Dynamic Rankings] (整体二分+树状数组 or 动态开点线段树 or 主席树)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  6. HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)

    题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...

  7. BZOJ 2683: 简单题(CDQ分治 + 树状数组)

    BZOJ2683: 简单题(CDQ分治 + 树状数组) 题意: 你有一个\(N*N\)的棋盘,每个格子内有一个整数,初始时的时候全部为\(0\),现在需要维护两种操作: 命令 参数限制 内容 \(1\ ...

  8. FZOJ 2245 动态树(离散+离线+ 树状数组)

    Problem 2245 动态树 Accept: 17    Submit: 82Time Limit: 3000 mSec    Memory Limit : 65536 KB  Problem D ...

  9. UVA1406 - A Sequence of Numbers(树状数组)

    UVA1406 - A Sequence of Numbers(树状数组) 题目链接 题目大意: 给定N个数字.给两种操作:C x: 把这N个数字都加上x. Q x:查询这N个数里面有多少个数字和2^ ...

随机推荐

  1. Spark源码分析之三:Stage划分

    继上篇<Spark源码分析之Job的调度模型与运行反馈>之后,我们继续来看第二阶段--Stage划分. Stage划分的大体流程如下图所示: 前面提到,对于JobSubmitted事件,我 ...

  2. Perl语言学习笔记 15 智能匹配与give-when结构

    1.智能匹配操作符 替代绑定操作符: 在哈希中查找某一个键: 比較两个数组是否全然同样: 查找列表中是否存在某个元素: 智能匹配操作符与顺序无关.~~ 左右元素能够互换 2.智能操作符优先级 3.gi ...

  3. iOS对象(数组)转化为JSon字符串

    - (void)seabc { NSArray *arry=[NSArray arrayWithObjects:@"0081",@"0082",@"0 ...

  4. Maven 编译

    pom.xml 添加插件 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins< ...

  5. 在嵌入式、海思、ARM中进行统一的音频AAC编码的必要性

    前言 最近来到深圳,跟许多做硬件的小伙伴聊安防.聊互联网.聊技术,受益颇多,其中聊到一点,大家一直都在想,互联网发展如此迅猛,为啥大部分的摄像机还是采用的传统G.726/G.711的音频编码格式呢,如 ...

  6. Mybatis之入门Helloworld程序

    本篇我们来实现一个Mybatis的Helloworld级别的一个示例程序. 一.搭建基本环境 1.基本开发环境搭建,这里选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.8,m ...

  7. git拉取远程分支到本地分支或者创建本地新分支

    git fetch origin branchname:branchname 可以把远程某各分支拉去到本地的branchname下,如果没有branchname,则会在本地新建branchname g ...

  8. vue+vuex构建单页应用

    基本 构建工具: webpack 语言: ES6 分号:行首分号规则(行尾不加分好, [ , ( , / , + , - 开头时在行首加分号) 配套设施: webpack 全家桶, vue 全家桶 项 ...

  9. 题解 P4799 【[CEOI2015 Day2]世界冰球锦标赛】

    题解 P4799 [[CEOI2015 Day2]世界冰球锦标赛] 双向搜索好题 传送门 实际上,双向搜索就是把\(a^n\)的复杂度转变成了大多为\(O(nlogna^{\frac{n}{2}})\ ...

  10. python自动化运维六:paramiko

    paramiko是基于python实现的SSH2远程安全连接,支持认证以及密钥方式,可以实现远程命令执行,文件传输,中间SSH代理等功能.也就是采用SSH的方式进行远程访问.SSH登陆的方式可以参考之 ...