#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. javascript基础(五)函数

    原文http://pij.robinqu.me/ 通过call和apply间接调用函数(改变this) call 和 apply带有多个参数,call和apply把当前函数的this指向第一个参数给定 ...

  2. 一行一行分析JQ源码学习笔记-06

    节点类型获取$("span")首先 判断 if(select.nodeType) markarray() 类数组 转化成真正的数组 var adiv = document.getE ...

  3. jQuery技巧大放送【转】

    1.关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用do ...

  4. Spring入门学习(一)

    Spring的主要功能是控制反转和面向切面编程,下面我们就来编写第一个spring的程序来体验一下控制反转 首先是加载配置文件 <?xml version="1.0" enc ...

  5. Qt5 OpenGL框架

    #ifndef MYRENDERER_H #define MYRENDERER_H #include <QOpenGLContext> #include <QOpenGLFuncti ...

  6. python 函数/列表的应用

    enumerate 函数用于遍历序列中的元素以及它们的下标: >>> for i,j in enumerate(('a','b','c')): print i,j 0 a1 b2 c ...

  7. sql分页比较简单快捷的方法

    SELECT TOP 显示数量* FROM 表 WHERE (主键id>(SELECT MAX(主键id) FROM(SELECT TOP 页码数*显示数量 主键id FROM 表 ORDER ...

  8. A - LCM Challenge

    A - LCM Challenge Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Others ...

  9. (转)用JMX监测JVM的运行参数

    翻译自http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html  用JMX管理你的JVMJMX是Java管理扩展 ...

  10. slam相关知识

    Kinect视觉SLAM技术介绍 http://www.open-open.com/news/view/ce76e2 本文介绍SLAM的历史.理论以及实现的方式,且主要介绍基于视觉(Kinect)的实 ...