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. 百度富文本编辑器Ueditor上传图片时标签中添加宽高

    ueditor.all.js中:直接搜索callback() function callback(){ try{ var link, json, loader, body = (iframe.cont ...

  2. boost::noncopyable

    /** * boost::noncopyable 实现单例不用麻烦了,直接从这个继承就行了 */ #include <boost/noncopyable.hpp> class myclas ...

  3. hadoop 小文件 挂载 小文件对NameNode的内存消耗 HDFS小文件解决方案 客户端 自身机制 HDFS把块默认复制3次至3个不同节点。

    hadoop不支持传统文件系统的挂载,使得流式数据装进hadoop变得复杂. hadoo中,文件只是目录项存在:在文件关闭前,其长度一直显示为0:如果在一段时间内将数据写到文件却没有将其关闭,则若网络 ...

  4. JAVA中equals()与==的区别详解

    在进行判断操作时,常常会用到==或者equals()进行等价判断,那么两者究竟有什么区别呢,下面整理一下个人理解. 简单介绍: ==是一种引用相等性比较符,判断引用到堆上同一个对象的两个引用是相等的. ...

  5. 【剑指Offer学习】【面试题33:把数组排成最小的数】

    题目:输入一个正整数数组,把数组里全部数字拼接起来排成一个数.打印能拼接出的全部数字中最小的一个. 样例说明: 比如输入数组{3. 32, 321},则扫描输出这3 个数字能排成的最小数字321323 ...

  6. FI模块与SD、MM的接口配置方法

    [转自 http://blog.itpub.net/195776/viewspace-1023910/] 1 FI/SD 借口配置FI/SD通过tcode VKOA为billing设置过帐科目,用户可 ...

  7. SNMP服务安装 Centos6.8环境网络安装

    概念: snmp是英文“Simple Network Management Protocol”的缩写,意为:简单网络管理协议. snmp是目前最常用的环境管理协议. snmp被设计成与协议无关的. s ...

  8. 20145239杜文超 《Java程序设计》第10周学习总结

    20145239 <Java程序设计>第10周学习总结 教材学习内容总结 Java的网络编程 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 网络概述 1.计算机 ...

  9. media server died的解决方法【转】

    本文转载自:https://blog.csdn.net/class_brick/article/details/78086261 在对MediaRecord进行初始化时添加上 mMediaRecord ...

  10. Hadoop- MapReduce在实际应用中常见的调优

    1.Reduce Task Number 通常来说一个block就对应一个map任务进行处理,reduce任务如果人工不去设置干预的话就一个reduce.reduce任务的个数可以通过在程序中设置   ...