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个不同的数值,让你找出三个数值满足中间的数值在两边的数值之间的个数. 析:题意还是比较好理解的,关键是怎么求数量,首先我们分解一下只有两种情况,一个是左边<中间<右边, ...
随机推荐
- hdu_4742_Pinball Game 3D(cdq分治+树状数组)
题目链接:hdu_4742_Pinball Game 3D 题意: 给你n个点,让你求三维的LIS,并且求出有多少种组合能达到LIS. 题解: 求三维的LIS,典型的三维偏序问题,x排序,解决一维,c ...
- ESFramework 4.0 进阶(04)-- 驱动力:通信引擎(下)
在ESFramework 4.0 进阶(03)-- 驱动力:通信引擎(上)一文中,我们对ESFramework提供的每一个通信引擎的接口都做了详细了说明,这篇文章我们将继续探讨这些接口的实现类 -- ...
- Openjudge-计算概论(A)-简单算术表达式求值
描述: 两位正整数的简单算术运算(只考虑整数运算),算术运算为: +,加法运算:-,减法运算:*,乘法运算:/,整除运算:%,取余运算. 算术表达式的格式为(运算符前后可能有空格):运算数 运算符 运 ...
- Table获取checkbox选中行数据
//检测勾选值 function checkEnter() { var Ivalue = ""; $("#dataTable tr").each(functio ...
- 用mybatis生成插件自动生成配置文件
1.在当前的maven项目的pom.xml中添加插件 <build> <plugins> <plugin> <groupId>org.mybatis.g ...
- javascript焦点图之垂直滚动
html代码布局,需要用到定位,不细说了 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- Codeforces Round #375 (Div. 2)A. The New Year: Mee
A. The New Year: Meeting Friends time limit per test 1 second memory limit per test 256 megabytes in ...
- Oracle字符串操作[转:http://www.cnblogs.com/xd502djj/archive/2010/08/11/1797577.html]
ORACLE 字符串操作 1 字符串连接 SQL> select 'abc' || 'def' from dual; 'ABC'|------abcdef 2 小写SQL>select ...
- 转:java.io.IOException: Exceeeded maximum number of redirects: 5 解决版本
Jmeter运行的时候出现的重定向超过n次的问题: When trying to test a Silverlight application, I get the below error. Has ...
- 感知哈希算法的java实现
一.原理讲解 实现这种功能的关键技术叫做"感知哈希算法"(Perceptual Hash Algorithm), 意思是为图片生成一个指纹(字符串格式), 两张图片的指纹 ...