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. ssh无密码登陆权威指南

    [0]写在前面 由于ssh 实现的是免密码登陆,大致步骤是: 0.1) client通过ssh登陆到server: 0.2) server检查家目录下的.ssh文件, 并发送公钥文件 authoriz ...

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

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

  3. mysql系列之5.mysql备份恢复

    备份数据: mysqldump #mysqldump -uroot -p123456 test > /test_bak.sql #egrep -v "#|\*|--|^$" ...

  4. 查看物料凭证MB03 /MIGO A04-显示,R02-物料凭证

    当货物移动操作后,可以使用事物码MB03.MIGO查询最近一次生成的物料凭证, 如果未知凭证号,Table:MKPF / AUFM/EKBE MKPF 抬头:物料凭证 KEY: MBLNR 物料凭证编 ...

  5. (转)nginx-rtmp-module和ffmpeg搭建实时HLS切片

    1.rtmp服务器 nginx+pcre+zlib+openssl+nginx-rtmp-module ./configure \ --prefix=/usr/local/nginx \ --sbin ...

  6. 基于ajax和Form实现的注册

    注册 urls.py from django.contrib import admin from django.urls import path, re_path from app01 import ...

  7. ceph分布式存储系统初探

    前言 由于公司的业务调整,现在我又要接触ceph这个东西,由于我接手的是一个网盘类项目,所以分布式存储系统ceph就是我必须要学的了.现在压力还是比较大的,从业务直接到后台核心. 大概在这几天,我将c ...

  8. c#应用程序带参数运行

    有时候我们需要让软件带参数运行,使用参数控制软件的部分行为, C#默认窗口应用是不带参数的,不过在Main函数的参数手动加上就可以得到参数了. 举例如下: /// <summary> // ...

  9. [STM8L]基于STM8L152的TAB段式LCD液晶驱动的分析 - 单片机干货 - 中国电子技术论坛 - 最好最受欢迎电子论坛!

    [STM8L]基于STM8L152的TAB段式LCD液晶驱动的分析 - 单片机干货 - 中国电子技术论坛 - 最好最受欢迎电子论坛!.md 主控芯片为STM8L152C4T6自带LCD控制器,低功耗系 ...

  10. GPIO设备虚拟文件结点的创建【转】

    本文转载自:http://blog.csdn.net/dwyane_zhang/article/details/6742066 所谓GPIO设备虚拟文件结点,就是方便用户在应用程序直接操纵GPIO的值 ...