LA 4329 BIT 分治
#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 分治的更多相关文章
- ACM-ICPC LA 4329 Ping pong(树状数组)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- LA 4329 Ping pong 树状数组
对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...
- BIT LA 4329 Ping pong
题目传送门 题意:训练指南P197 分析:枚举裁判的位置,用树状数组来得知前面比它小的和大的以及后面比它小的和大的,然后O (n)累加小 * 大 + 大 * 小 就可以了 #include <b ...
- LA 4329 ping-pong树状数组
题目链接: 刘汝佳,大白书,P197. 枚举裁判的位置,当裁判为i时,可以有多少种选法,如果已经知道在位置i之前有ci个数比ai小,那么在位置i之前就有i-1-ci个数比ai大. 在位置i之后有di个 ...
- LA 4329
第一次敲树状数组 因为一个小错误 wa了 n 多遍 终于ac 太不容易了 /*********************************************************** ...
- LA 4329 (树状数组) Ping pong
第一次写树状数组,感觉那个lowbit位运算用的相当厉害. 因为-x相当于把x的二进制位取反然后整体再加上1,所以最右边的一个1以及末尾的0,取反加一以后不变. 比如1000取反是0111加一得到10 ...
- LA 4329(树状数组)
题目描述: N <tex2html_verbatim_mark>(3N20000) <tex2html_verbatim_mark>ping pong players live ...
- LA 4329 乒乓比赛
https://vjudge.net/problem/UVALive-4329 题意: 一条大街上住着n个兵乓球爱好者,经常组织比赛切磋技术.每个人都有一个不同的技能值ai.每场比赛需要3个人:两名选 ...
- LA 4329 Ping pong (树状数组)
题意:从左到右给你n个不同的数值,让你找出三个数值满足中间的数值在两边的数值之间的个数. 析:题意还是比较好理解的,关键是怎么求数量,首先我们分解一下只有两种情况,一个是左边<中间<右边, ...
随机推荐
- Mysql表名区分大小写
mysql数据库在windows服务器上表名和字段名均不区分大小写, 但在linux服务器上表名默认是区分大小写的,可在/etc/my.cnf文件中的[mysqld]下面加上一条配置 lower_ca ...
- 从零开始学Axure原型设计(高级篇)
如果你熟悉了Axure的部件库,那么你可以得心应手地画出心目中产品的线框图:如果你会用Axure的母版.动态面板功能,那么你应该能够画出一些简单网站的原型图:但只有你精通了Axure的条件逻辑.变量. ...
- [转]5个JavaScript面试题
问题1:闭包 考虑下面的代码: 1 2 3 4 5 6 var nodes = document.getElementsByTagName('button'); for (var i = 0; i & ...
- webapi中的路由约束
Route Constraints Route constraints let you restrict how the parameters in the route template are ma ...
- SpringMVC集成shrio框架
使用SHIRO的步骤:1,导入jar2,配置web.xml3,建立dbRelm4,在Spring中配置 添加所需jar包: <!--Apache Shiro所需的jar包--> <d ...
- 使用日志记录功能查看PHP扩展的执行过程
了解过PHP内核的同学都知道,PHP的一次请求的生命周期 1.启动Apache后,PHP解释程序也随之启动.PHP调用各个扩展的MINIT方法,从而使这些扩展切换到可用状态 2.当一个页面请求发生时, ...
- elicpse之tomcat配置
环境:eclipse4.5.0,tomcat7.0.57 部署描述:按照一般的部署,把tomcat部署到webapps的下面,server options 下面选的是 Modules auto rel ...
- php精度计算问题
如果用php的+-*/计算浮点数的时候,可能会遇到一些计算结果错误的问题,比如echo intval( 0.58*100 );会打印57,而不是58,这个其实是计算机底层二进制无法精确表示浮点数的一个 ...
- GO对象和指针初始化
转自http://www.cnblogs.com/tianyajuanke/p/5234205.html
- 从Map、JSONObject取不存在键值对时的异常情况
1.在Map中取不存在的键值对时不会报异常,只会返回null. @Test public void testMap() { Map<String, Object> map = new Ha ...