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 ...
随机推荐
- PDO连接数据库-Xmodel
<?php/* * Copyright (c) 2018, 北京博习园教育科技有限公司 * All rights reserved. * * 文件名称: xmodel.php * 摘 要: 模型 ...
- java学习之路--简单基础的面试题
1.面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: 1)抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象和行为抽象两方面.抽象只关注对象有哪些属性和行为,并不关注 ...
- requirejs配置问题
<script src="lib/requirejs/require.js " data-main="js/main.js"> </scrip ...
- Oracle课程档案,第四天
“子查询”就是查询中嵌套着另一个查询,也即通过SELECT语句的嵌套使用形成子查询.当我们不知道特定的查询条件时,可以用子查询来为父查询提供查询条件以获得查询结果. 子查询先清除子查询 在清除主查询 ...
- javascript的数组之from()
Array.from()方法从一个类似数组或可迭代对象中创建一个新的数组实例. const arr = [1, 2, 3]; Array.from(arr); //[1, 2, 3] Array.fr ...
- vue设置初始对象时为空报错
解决办法:在初始化时提供完整的数据结构
- asp.net mvc ef 性能监控调试工具 MiniProfiler
MiniProfiler是一款针对.NET, Ruby, Go and Node.js的性能分析的轻量级程序.可以对一个页面本身,及该页面通过直接引用.Ajax.Iframe形式访问的其它页面进行监控 ...
- Q: Is Consul eventually or strongly consistent?
强一致 最终一致 Frequently Asked Questions - Consul by HashiCorp https://www.consul.io/docs/faq.html Q: Is ...
- FakeGame 集成总结
1.64位支持(目前编译不过); 2.Dx9? 2.以何种方式提供(源码?工程版本(VS2005还是其他)): 3.是否可以连接TC的服务器进行调试? TDR编解码失败: 不同目录下存在a.lib的不 ...
- C++11 使用异步编程std::async和std::future
先说明一点:std::asyanc是std::future的高级封装, 一般我们不会直接使用std::futrue,而是使用对std::future的高级封装std::async. 下面分别说一下. ...