思路

分别统计这个位置左边和右边各有多少大于和小于它的数,乘起来即可

使用权值树状数组

代码

#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的更多相关文章

  1. HDU 2492 Ping pong (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=2000 ...

  2. UVALive 4329 Ping pong

                                      Ping pong Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Fo ...

  3. POJ 3928 Ping pong(树状数组)

                                                                          Ping pong Time Limit: 1000MS   ...

  4. LA4329 Ping pong(树状数组与组合原理)

    N (3N20000)ping pong players live along a west-east street(consider the street as a line segment). E ...

  5. Ping pong

    Ping pong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  6. POJ 3928 Ping pong

    题目链接:http://poj.org/problem?id=3928 乒乓比赛,有N个人参加,输入每个玩家的技能等级,对每个人设置一个特定ID和一个技能值,一场比赛需要两个选手和一个裁判,只有当裁判 ...

  7. Frequent values && Ping pong

    Frequent values 题意是不同颜色区间首尾相接,询问一个区间内同色区间的最长长度. 网上流行的做法,包括翻出来之前POJ的代码也是RMQ做法,对于序列上的每个数,记录该数向左和向右延续的最 ...

  8. 【暑假】[实用数据结构]UVAlive 4329 Ping pong

    UVAlive 4329 Ping pong 题目: Ping pong Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: % ...

  9. Ping pong(树状数组经典)

    Ping pong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

随机推荐

  1. maven如何单独启动插件目标

    说来恼火,在网上找了半天没找到 原来插件名称其实不需要定义而是直接想象到的,如下,我尝试过了好几种 mvn plugin:goal 都没成功:从代码看goal已经没啥疑问了,就是compile,但哪个 ...

  2. __super:: 使用

    它会调用 基类的函数方法实现. __super::Close(); __super::Find();

  3. /编写一个函数,要求从给定的向量A中删除元素值在x到y之间的所有元素(向量要求各个元素之间不能有间断), 函数原型为int del(int A ,int n , int x , int y),其中n为输入向量的维数,返回值为删除元素后的维数

    /** * @author:(LiberHome) * @date:Created in 2019/2/28 19:39 * @description: * @version:$ */ /* 编写一个 ...

  4. css学习_css用户界面样式

    1.css用户界面样式 a.鼠标样式(记住几个兼容性好的) cursor:default/pointer/move/text; b.轮廓 outline outline:2px solid red: ...

  5. torch.nn.Embedding

    自然语言中的常用的构建词向量方法,将id化后的语料库,映射到低维稠密的向量空间中,pytorch 中的使用如下: import torch import torch.utils.data as Dat ...

  6. ES6中Set 和 Map用法

    JS中Set与Map用法 一.Set 1.基本用法 ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set 本身是一个构造函数,用来生成 Set 数据结构. ...

  7. app优化篇

    UIImageView高效加个圆角 一般通过clipsToBounds和layer.cornerRadius会强制Core Animation提前渲染屏幕的离屏绘制,影响性能. 通过贝塞尔曲线切割图片 ...

  8. git常用操作命令使用说明

    设置用户名和邮箱 git config --global user.email 'xxx' git config --global user.name 'xxx' 创建分支 git branch xx ...

  9. Yii2 mongoDb的配置及使用

    yii2 的配置都是在启动时加载的,所以mongo的配置也同样在component里面配置. 具体实现(无用户和密码): [ 'mongo1' => [ 'class' => '\yii\ ...

  10. 扩容Linux文件系统

    扩容Linux文件系统 腾讯云 云硬盘扩容 https://cloud.tencent.com/product/cbs https://cloud.tencent.com/document/produ ...