LA 4329
第一次敲树状数组 因为一个小错误 wa了 n 多遍 终于ac 太不容易了
/*************************************************************************
> Author: xlc2845 > Mail: xlc2845@gmail.com
> Created Time: 2013年11月07日 星期四 11时05分22秒
************************************************************************/ #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
//#define lowbit(x) ((x)&(-(x)))
#define LL long long
#define maxn 20010
using namespace std;
int a[100010], n, num[maxn];
LL c[maxn], d[maxn];
int lowbit(int x)
{
return x&(-x);
} int sum(int x)
{
int ret = 0;
while(x > 0)
ret += a[x], x -= lowbit(x);
return ret;
} void add(int x, int d)
{
while(x <= 100010)
a[x] += d, x += lowbit(x);
} void init()
{
memset(c, 0, sizeof(c));
memset(d, 0, sizeof(d));
memset(a, 0, sizeof(a));
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d",&n);
for(int i = 1; i <= n; i++)
{
scanf("%d",&num[i]);
add(num[i], 1);
c[i] = sum(num[i]-1);
}
memset(a, 0, sizeof(a));
for(int i = n; i >= 1; i--)
{
add(num[i], 1);
d[i] = sum(num[i]-1);
}
LL sum = 0;
for(int i = 1; i <= n; i++)
sum += c[i]*(n-i-d[i])+(i-c[i]-1)*d[i];
printf("%lld\n",sum);
}
return 0;
}
LA 4329的更多相关文章
- 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 (树状数组) 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 BIT 分治
#include <cstdio> #include <queue> #include <cstring> #include <iostream> #i ...
- LA 4329 乒乓比赛
https://vjudge.net/problem/UVALive-4329 题意: 一条大街上住着n个兵乓球爱好者,经常组织比赛切磋技术.每个人都有一个不同的技能值ai.每场比赛需要3个人:两名选 ...
- LA 4329 Ping pong (树状数组)
题意:从左到右给你n个不同的数值,让你找出三个数值满足中间的数值在两边的数值之间的个数. 析:题意还是比较好理解的,关键是怎么求数量,首先我们分解一下只有两种情况,一个是左边<中间<右边, ...
随机推荐
- Windows Phone 中查找可视化树中的某个类型的元素
private void StackPanel_Tap(object sender, TappedRoutedEventArgs e) { //获取到的对象是ListBoxItem ListBoxIt ...
- ADO.NET笔记——使用连接池
相关知识: 连接池的意义: 应用程序往往涉及大量的,并发的数据访问操作 数据库服务器能够同时维系的连接数量非常有限.如果某个数据库访问操作不及时关闭连接,就会减少其他调用对数据库访问的机会.因此,一般 ...
- linux下配置tomcat7 + solr4.9
一.安装准备 操作系统:CentOS 6.5 tomcat版本:apache-tomcat-7.0.54.tar.gz solr版本:solr-4.9.0.tgz 二.部署实施 安装tomcat:将t ...
- ASP.NET Core 1.0
.NET Core dotnet 命令大全:http://www.cnblogs.com/linezero/p/dotnet.html http://www.cnblogs.com/Wddpct/p/ ...
- Nginx+uWSGI+Django原理
Python的Web开发中,如果使用Django框架,那么较为成熟稳定的服务器架构一般是Nginx+uWSGI+Django.而为什么一定要三个结合在一起呢?直接使用Django的runserver来 ...
- 通过数据绑定模板得到对应的Item控件
这类控件都继承于Selector,其中主要有ComboBox.listview.listbox.datagrid. 由于个人对WPF的了解所有可能有遗漏,希望各位能够指出一起进步. 在遍历上面控件时主 ...
- XenServer安装虚拟机---先扩容存放ISO镜像文件
我们都知道xenserver安装后,不管你的盘有多大,只有4G的空间. 故操作是:新建LV卷,可自定义大小 1.vgdisplay #先查看剩余空间 [root@XenServer /]# vgdis ...
- Ubuntu下codeblocks汉化
code::blocks是一个十分好用编辑环境,一个在手,无所不能,为了更好的支持中文,我列出了汉化的方法: 1下载中文汉化包:http://pan.baidu.com/s/1hqvNZbI 2.解压 ...
- 2016 系统设计第一期 (档案一)jQuery ajax serialize()方法form提交数据
jQuery ajax serialize()方法form提交数据,有个很奇怪的问题,好像不能取到隐藏控件的值. //点击提交按钮保存数据 $('#btn_submitUser').click(fun ...
- matlab实现高斯牛顿法、Levenberg–Marquardt方法
高斯牛顿法: function [ x_ans ] = GaussNewton( xi, yi, ri) % input : x = the x vector of 3 points % y = th ...