#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#define lowbit(x) x&-x using namespace std; int c[],a[],n,T,l[]; int sum(int x)
{
int ret=;
while(x>)
{
ret+=c[x];
x-=lowbit(x);
}
return ret;
} void add(int x,int d)
{
while(x<=)
{
c[x]+=d;
x+=lowbit(x);
}
} int main()
{
//freopen("/home/user/in","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(c,,sizeof(c));
long long s=;
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
add(a[i],);
// printf("%d\n",c[i]);
l[i]=sum(a[i]-);//l[i]表示a[i]前面比a[i]小的数的个数
}
//for(int i=0;i<n;i++)
// printf("a[%d]=%d %d %d\n",i,a[i],l[i],sum(a[i]));
for(int i=;i<n;i++)
{
int al=sum(a[i]-);//al表示整个a数组中比a[i]小德数的个数
s+=l[i]*(n-i--al+l[i])+(i-l[i])*(al-l[i]);
}
printf("%lld\n",s);
}
return ;
}

用BIT做的

 #include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set> using namespace std; struct Node
{
int i,x;
bool operator<(Node &a)const
{
return x<a.x;
}
}; Node a[],t[];
int c[],n,T; void merge_sort(int l,int r)
{
if(r-l>)
{
int m=l+(r-l)/;
int p=l,q=m,i=l;
merge_sort(l,m);
merge_sort(m,r);
while(p<m||q<r)
{
if(q>=r||(p<m&&a[p]<a[q])) memcpy(&t[i++],&a[p++],sizeof(a[]));
else
{
memcpy(&t[i],&a[q],sizeof(a[]));
c[a[q].i]+=m-p;//c[i]记录在i前面的比i大的个数
i++;q++;
}
}
for(int i=l;i<r;i++) memcpy(&a[i],&t[i],sizeof(a[]));
}
} int main()
{
//freopen("/home/user/in","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=;i<n;i++)
{
a[i].i=i;
scanf("%d",&a[i].x);
}
memset(c,,sizeof(c[])*(n+));
merge_sort(,n);
long long sum=;
//for(int i=0;i<n;i++) printf("a[%d]=%d %d %d\n",i,a[i].x,c[a[i].i],a[i].i-c[a[i].i]);
for(int i=;i<n;i++)
sum+=c[a[i].i]*(i-a[i].i+c[a[i].i])+(a[i].i-c[a[i].i])*(n-i--c[a[i].i]);
printf("%lld\n",sum); }
return ;
}

用分治法求逆序对的方法做的

LA 4329 BIT 分治的更多相关文章

  1. ACM-ICPC LA 4329 Ping pong(树状数组)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  2. LA 4329 Ping pong 树状数组

    对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...

  3. BIT LA 4329 Ping pong

    题目传送门 题意:训练指南P197 分析:枚举裁判的位置,用树状数组来得知前面比它小的和大的以及后面比它小的和大的,然后O (n)累加小 * 大 + 大 * 小 就可以了 #include <b ...

  4. LA 4329 ping-pong树状数组

    题目链接: 刘汝佳,大白书,P197. 枚举裁判的位置,当裁判为i时,可以有多少种选法,如果已经知道在位置i之前有ci个数比ai小,那么在位置i之前就有i-1-ci个数比ai大. 在位置i之后有di个 ...

  5. LA 4329

    第一次敲树状数组  因为一个小错误 wa了 n 多遍  终于ac  太不容易了 /*********************************************************** ...

  6. LA 4329 (树状数组) Ping pong

    第一次写树状数组,感觉那个lowbit位运算用的相当厉害. 因为-x相当于把x的二进制位取反然后整体再加上1,所以最右边的一个1以及末尾的0,取反加一以后不变. 比如1000取反是0111加一得到10 ...

  7. LA 4329(树状数组)

    题目描述: N <tex2html_verbatim_mark>(3N20000) <tex2html_verbatim_mark>ping pong players live ...

  8. LA 4329 乒乓比赛

    https://vjudge.net/problem/UVALive-4329 题意: 一条大街上住着n个兵乓球爱好者,经常组织比赛切磋技术.每个人都有一个不同的技能值ai.每场比赛需要3个人:两名选 ...

  9. LA 4329 Ping pong (树状数组)

    题意:从左到右给你n个不同的数值,让你找出三个数值满足中间的数值在两边的数值之间的个数. 析:题意还是比较好理解的,关键是怎么求数量,首先我们分解一下只有两种情况,一个是左边<中间<右边, ...

随机推荐

  1. get与post 获取参数值的方式

    get方式  参数带在url后面,form表单中的域  可以 没有value 属性, 后台可以直接在方法的参数中加入和url一样的参数就能直接获得该参数的值(效率高,不安全) post方式 url链接 ...

  2. Computation expressions and wrapper types

    原文地址:http://fsharpforfunandprofit.com/posts/computation-expressions-wrapper-types/ 在上一篇中,我们介绍了“maybe ...

  3. Excel教程(5) - 日期与时间函数

    DATE 用途:返回代表特定日期的序列号. 语法:DATE(year,month,day) 参数:year 为一到四位,根据使用的日期系统解释该参 数.默认情况下,Excel for Windows ...

  4. 此 ObjectContext 实例已释放,不可再用于需要连接的操作

    EF 查询包含导航属性的Model,经常会遇到 此 ObjectContext 实例已释放,不可再用于需要连接的操作 这种错误,解决方法如下: db.T_DailyRecord.Include(&qu ...

  5. android资源文件的选取

    Android app项目中,res是用来存放资源文件的,来看看这些文件的创建和选取规则: 系统启动一个apk后,生成UI的过程中,会根据不同的系统配置来匹配.选择相应的资源文件. You shoul ...

  6. 比较全的 C# 操作 Word的代码

    using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word; ...

  7. li浮动时ul高度为0,解决ul自适应高度的几种方法

    网址:http://www.gxchina.com/edu/11208.html 1.给ul元素设置高度height最直接的办法是给ul元素设置一个高度,即ul标签添加height属性,代码如下: u ...

  8. Ubuntu上用mod_wsgi部署Django出现的一些问题

    1 编码问题 直接运行Django没问题,但通过Apache+mod_wsgi上传中文字符的文件时出错: UnicodeEncodeError: 'ascii' codec can't encode ...

  9. Spring的Bean,AOP以及工具类初探

    1.Bean(Ioc) BeanWrapper 根据JavaDoc中的说明,BeanWrapper提供了设置和获取属性值(单个的或者是批量的),获取属性描述信息.查询只读或者可写属性等功能.不仅如此, ...

  10. RLE行程长度编码压缩算法

    在看emWIN的时候看到一个图片压缩的算法可以有效的对二值图(简单的2中颜色或者更多)进行压缩,压缩的效果可以节省空间而且不丢失信息! 特点 一种压缩过的位图文件格式,RLE压缩方案是一种极其成熟的压 ...