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个不同的数值,让你找出三个数值满足中间的数值在两边的数值之间的个数. 析:题意还是比较好理解的,关键是怎么求数量,首先我们分解一下只有两种情况,一个是左边<中间<右边, ...
随机推荐
- 【Sort】QuickSort
快速排序,平均运行时间O(N log N),最坏运行时间O(N^2). 我觉得先看Python版的快排算法(http://www.cnblogs.com/fcyworld/p/6160558.html ...
- Ubuntur软件安装
Ubuntu软件安装 Falsh sudo apt-get install flashplugin-installer
- XML 树结构
XML 树结构 XML 文档形成了一种树结构,它从"根部"开始,然后扩展到"枝叶". 一个 XML 文档实例 XML 文档使用简单的具有自我描述性的语法: &l ...
- chapter8_3 c代码和错误
1.C代码 Lua提供的所有关于动态链接的功能都集中在一个函数中,即package.loadlib. 该函数有两个字符串参数:动态库的完整路径和一个函数名称: local path = "/ ...
- Openjudge-计算概论(A)-找和为K的两个元素
描述: 在一个长度为n(n < 1000)的整数序列中,判断是否存在某两个元素之和为k. 输入第一行输入序列的长度n和k,用空格分开.第二行输入序列中的n个整数,用空格分开.输出如果存在某两个元 ...
- css样式表及属性
CSS(Cascading Style Sheet,叠层样式表),作用是美化HTML网页. /*注释区域*/ 此为注释语法 一.样式表 (一)样式表的分类 1.内联样式表 和HTML联合显示,控 ...
- 行列转换之静态、动态、PIVOT方法
/* 标题:普通行列转换(version 2.0) 作者:爱新觉罗.毓华 时间:2008-03-09 地点:广东深圳 说明:普通行列转换(version 1.0)仅针对sql server 2000 ...
- 慎用#define
#define INT_MAX 2147483647 INT_MAX+1 就会变成负数 long long r; r > INT_MAX+1 就会出错,应该写成 r > 21474 ...
- Webdriver控制翻页控件,并实现向前向后翻页功能,附上代码,仅供参考,其他类似日期控件的功能可以自己封装
新增输入与选择页面的html源码: <div style="margin-top:-60px;" class="modal-content" id=&qu ...
- Position & anchorPoint 深入
引言 相信初接触到CALayer的人都会遇到以下几个问题: 为什么修改anchorPoint会移动layer的位置?CALayer的position点是哪一点呢?anchorPoint与positio ...