Ping pong
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3109   Accepted: 1148

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

Source


白书
求f[i]为i之前技能值小于i的人数,g[i]为i之后小于i的人数
求法类似逆序对,x[i]技能值为i的人是否有,用树状数组维护x,求和即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=2e4+,M=1e5+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int T,n,m,a[N],bit[M],f[N],g[N];
inline int lowbit(int x){return x&-x;}
inline void add(int p,int v,int bit[]){
for(int i=p;i<=m;i+=lowbit(i)) bit[i]+=v;
}
inline int sum(int p,int bit[]){
int ans=;
for(int i=p;i>;i-=lowbit(i)) ans+=bit[i];
return ans;
}
int main(){
T=read();
while(T--){
n=read();
for(int i=;i<=n;i++) a[i]=read(),m=max(m,a[i]);
memset(bit,,sizeof(bit));
for(int i=;i<=n;i++){
add(a[i],,bit);
f[i]=sum(a[i]-,bit);
}
memset(bit,,sizeof(bit));
for(int i=n;i>=;i--){
add(a[i],,bit);
g[i]=sum(a[i]-,bit);
}
ll ans=;
for(int i=;i<=n-;i++) ans+=f[i]*(n-i-g[i])+g[i]*(i--f[i]);//,printf("t%d %d %d\n",i,f[i],g[i]);
printf("%lld\n",ans);
}
}

POJ3928Ping pong[树状数组 仿逆序对]的更多相关文章

  1. POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)

    树状数组求逆序对   转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...

  2. poj3067 Japan 树状数组求逆序对

    题目链接:http://poj.org/problem?id=3067 题目就是让我们求连线后交点的个数 很容易想到将左端点从小到大排序,如果左端点相同则右端点从小到大排序 那么答案即为逆序对的个数 ...

  3. SGU180(树状数组,逆序对,离散)

    Inversions time limit per test: 0.25 sec. memory limit per test: 4096 KB input: standard output: sta ...

  4. [NOIP2013提高&洛谷P1966]火柴排队 题解(树状数组求逆序对)

    [NOIP2013提高&洛谷P1966]火柴排队 Description 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相 ...

  5. [NOI导刊2010提高&洛谷P1774]最接近神的人 题解(树状数组求逆序对)

    [NOI导刊2010提高&洛谷P1774]最接近神的人 Description 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某 ...

  6. 【bzoj2789】[Poi2012]Letters 树状数组求逆序对

    题目描述 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B. 输入 第一行一个正整数n ...

  7. poj3067Japan——树状数组查找逆序对

    题目:http://poj.org/problem?id=3067 利用树状数组查找逆序对. 代码如下: #include<iostream> #include<cstdio> ...

  8. “浪潮杯”第九届山东省ACM大学生程序设计竞赛(重现赛)E.sequence(树状数组求逆序对(划掉))

    传送门 E.sequence •题意 定义序列 p 中的 "good",只要 i 之前存在 pj < pi,那么,pi就是 "good": 求删除一个数, ...

  9. 2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对)

    2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对) https://www.luogu.com.cn/problem/P5041 题意: 给一个字符串 \(S\) ,每 ...

随机推荐

  1. sql语句查询

    1. sql语句查询某位数字或者某几位数字开头的数据,字段类型为数字类: %’: 2. sql搜索以4开头和含有李字的数据: select * from wlzbpre_user where real ...

  2. 用eclipse做项目中常遇到的问题-如何创建并读取properties文件

    在用eclipse做项目开发的时候我们常常会将一些重要的内容写在配置文件里面, 特别是连接数据库的url,username,password等信息,我们常常会新建一个properties文件将所有信息 ...

  3. Lind.DDD.Utils.HttpHelper里静态对象引出的Http超时问题

    回到目录 Lind.DDD.Utils.HttpHelper组件主要实现了对HTTP的各种操作,如Get,Post,Put和Delete,它属于最纯粹的操作,大叔把它封装的目的主要为了实现与API安全 ...

  4. Ajax实现简单下拉选项

    基本都是固定步骤!主要在JAVASCRIPT和PHP中的操作 1.HTML代码里就只有两个SELECT标签如下: <select id="province"> < ...

  5. Struts2(2) —— Action

    Struts2框架中的Action类是一个单独的javabean对象.不像Struts1中还要去继承HttpServlet,耦合度减小了. 1,流程 拦截器拦截请求,创建代理Action对象,执行方法 ...

  6. Mou常用快捷键

    title: Mou常用快捷键date: 2015-11-08 17:16:38categories: 编辑工具 tags: mou 小小程序猿我的博客:http://daycoding.com Vi ...

  7. weblogic安装注意事项_linux

    ➠更多技术干货请戳:听云博客 一.安装过程:参考“weblogic安装截屏(linux)” 注意事项:安装weblogic时,需要注意以下两点: 1.首先在安装目录下创建weblogic12文件夹 如 ...

  8. js 多选 反选

    //$(".435__1").attr("checked", true); //$(".435__0").removeAttr(" ...

  9. Unix Linux 通用vi命令,使用帮助手册【珍藏版】

    Vi 简介 Vi 是 Unix 世界里极为普遍的全萤幕文书编辑器,几乎可以说任何一台 Unix 机器都会提供这套软体.Linux 当然也有,它的 vi 其实是 elvis(版权问题),不过它们都差不多 ...

  10. docker'部署

    环境:ubuntu-14.04.4-server-amd64 1.更换阿里云源 备份源配置文件: $ sudo cp /etc/apt/sources.list /etc/apt/sources.li ...