UVA1428 Ping pong
思路
分别统计这个位置左边和右边各有多少大于和小于它的数,乘起来即可
使用权值树状数组
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
#define int long long
using namespace std;
const int MAXN = 100000;
struct BIT{
int a[MAXN+10];
int lowbit(int x){
return x&(-x);
}
int query(int x){
int ans=0;
while(x){
ans+=a[x];
x-=lowbit(x);
}
return ans;
}
void add(int pos,int x){
while(pos<=MAXN){
a[pos]+=x;
pos+=lowbit(pos);
}
}
void init(void){
memset(a,0,sizeof(a));
}
}L,R;
int T,n,a[MAXN],ans=0;
signed main(){
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
scanf("%lld",&T);
while(T--){
ans=0;
L.init();
R.init();
scanf("%lld",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
R.add(a[i],1);
}
for(int i=1;i<=n;i++){
R.add(a[i],-1);
ans+=L.query(a[i]-1)*(R.query(MAXN)-R.query(a[i]-1));
ans+=R.query(a[i]-1)*(L.query(MAXN)-L.query(a[i]-1));
L.add(a[i],1);
}
printf("%lld\n",ans);
}
return 0;
}
UVA1428 Ping pong的更多相关文章
- HDU 2492 Ping pong (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=2000 ...
- UVALive 4329 Ping pong
Ping pong Time Limit: 3000MS Memory Limit: Unknown 64bit IO Fo ...
- POJ 3928 Ping pong(树状数组)
Ping pong Time Limit: 1000MS ...
- LA4329 Ping pong(树状数组与组合原理)
N (3N20000)ping pong players live along a west-east street(consider the street as a line segment). E ...
- Ping pong
Ping pong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- POJ 3928 Ping pong
题目链接:http://poj.org/problem?id=3928 乒乓比赛,有N个人参加,输入每个玩家的技能等级,对每个人设置一个特定ID和一个技能值,一场比赛需要两个选手和一个裁判,只有当裁判 ...
- Frequent values && Ping pong
Frequent values 题意是不同颜色区间首尾相接,询问一个区间内同色区间的最长长度. 网上流行的做法,包括翻出来之前POJ的代码也是RMQ做法,对于序列上的每个数,记录该数向左和向右延续的最 ...
- 【暑假】[实用数据结构]UVAlive 4329 Ping pong
UVAlive 4329 Ping pong 题目: Ping pong Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: % ...
- Ping pong(树状数组经典)
Ping pong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- JAVA课程课后作业03之动手动脑
一.构造函数 问题一: 错误代码如图: 错误原因:从图片中的编译报错的地方来看,程序是在给新的对象分配空间是出现了问题,因而我们往下观察Foo类,Foo类的构造方法是有一个参数的有参方法,而前面构造新 ...
- mybatis10--自连接多对一查询
查询老师对应的所有导师的信息 在09的基础上修改dao和mapper文件 public interface TeacherDao { /** * 根据老师的编号查询所有的导师信息 */ Teacher ...
- js 判断当前操作系统是ios还是android还是电脑端
js判断客户端是否是IOS或者是Android //如果返回true 则说明是Android function is_weixin() { var ua = window.navigator.user ...
- vue中的图标字体引入
网址:https://icomoon.io/app/#/select: 特点:样式多,免费 操作: 1.相中的,随便点,不要钱,generat fonts然后download,得到一个压缩文件,解压, ...
- Tensorflow一些常用基本概念与函数(1)
为了快速的熟悉TensorFlow编程,下面从一段简单的代码开始: import tensorflow as tf #定义‘符号’变量,也称为占位符 a = tf.placeholder(" ...
- easyui combobox 去空格事件 去掉,结果输入空格体验不畅的感觉,让combobox能够输入空格
$("[comboname=name]").next("span").find("input.textbox-text").unbind(& ...
- Linux网络 - 数据包的接收过程(转)
https://segmentfault.com/a/1190000008836467
- Gym 101981G - Pyramid - [打表找规律][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem G]
题目链接:http://codeforces.com/gym/101981/attachments The use of the triangle in the New Age practices s ...
- Restful API设计规范
理解RESTful架构 Restful API设计指南 理解RESTful架构 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式 ...
- Ubuntu 18.04 安装Docker
第一种方法从Ubuntu的仓库直接下载安装: 安装比较简单,这种安装的Docker不是最新版本,不过对于学习够用了,依次执行下面命令进行安装. $ sudo apt install docker.io ...