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 ...
随机推荐
- 算法提高 新建Microsoft Word文档
算法提高 新建Microsoft Word文档 时间限制:1.0s 内存限制:256.0MB 问题描述 L正在出题,新建了一个word文档,想不好取什么名字,身旁一人惊问:“你出 ...
- 为 git设置代理
普通设置 git config --global http.proxy 'socks5://127.0.0.1:1080'git config --global https.proxy 'socks5 ...
- Git Bash上传文件
今天通过Git Bash上传了一个项目(之前是通过Github Desk上传的),操作命令如下: 在目录下shift+右键打开Git Bash 1.git init 2.git add *.py 3. ...
- php const常量 不能使用字符串连接符.链接
class jdpay extends pay { const BASE_URL = "https://mapi.jdpay.com/npp10/"; private $agree ...
- [ssh] 通过ssh私钥生成公钥的方法
ssh-keygen -y -f .ssh/id_rsa.key id_rsa.key是私钥.
- 阻塞队列(BlockingQueue)
阻塞队列是 java.util.concurrent 包提供的一个类,该类提供了多线程中通过队列实现安全高效的数据处理的功能. 所谓阻塞队列,是在普通队列基础上实现了阻塞线程的功能: 队列为空时,获取 ...
- (转载)中文Appium API 文档
该文档是Testerhome官方翻译的源地址:https://github.com/appium/appium/tree/master/docs/cn官方网站上的:http://appium.io/s ...
- [elk]elasticsearch dsl语句
例子1 统计1,有唱歌兴趣的 2,按年龄分组 3,求每组平均年龄 4,按平均年龄降序排序 sql转为dsl例子 # 每种型号车的颜色数 > 1的 SELECT model,COUNT(DISTI ...
- 2.1 Html
一.Head中常用标签 <head>元素出现在文档的开头部分,会书写一些和浏览器中的配置信息. <head>与</head>之间的内容不会在浏览器的文档窗口显示,但 ...
- Clone of COCO API
Clone of COCO API - Dataset @ http://cocodataset.org/ - with changes to support Windows build and py ...