BIT LA 4329 Ping pong
题意:训练指南P197
分析:枚举裁判的位置,用树状数组来得知前面比它小的和大的以及后面比它小的和大的,然后O (n)累加小 * 大 + 大 * 小 就可以了
#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int N = 1e5 + 5;
const int M = 2e4 + 5;
struct BIT {
int c[N], sz;
void init(int n) {
memset (c, 0, sizeof (c));
sz = n;
}
void updata(int i, int x) {
while (i <= sz) {
c[i] += x; i += i & -i;
}
}
int query(int i) {
int ret = 0;
while (i) {
ret += c[i]; i -= i & -i;
}
return ret;
}
}bit;
int a[M], small[M][2], large[M][2]; int main(void) {
int T; scanf ("%d", &T);
while (T--) {
bit.init (100000);
int n; scanf ("%d", &n);
for (int i=1; i<=n; ++i) {
scanf ("%d", &a[i]);
small[i][0] = bit.query (a[i] - 1);
large[i][0] = i - 1 - small[i][0];
bit.updata (a[i], 1);
}
for (int i=1; i<=n; ++i) {
small[i][1] = bit.query (a[i] - 1) - small[i][0];
large[i][1] = n - i - small[i][1];
}
ll ans = 0;
for (int i=1; i<=n; ++i) {
ans += 1ll * small[i][0] * large[i][1];
ans += 1ll * large[i][0] * small[i][1];
}
printf ("%lld\n", ans);
} return 0;
}
BIT LA 4329 Ping pong的更多相关文章
- 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天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...
- LA 4329 Ping pong (树状数组)
题意:从左到右给你n个不同的数值,让你找出三个数值满足中间的数值在两边的数值之间的个数. 析:题意还是比较好理解的,关键是怎么求数量,首先我们分解一下只有两种情况,一个是左边<中间<右边, ...
- LA 4329 Ping pong
#include <iostream> #include <cstring> #include <cstdio> using namespace std; ; ; ...
- LA 4329 - Ping pong 树状数组(Fenwick树)
先放看题传送门 哭瞎了,交上去一直 Runtime error .以为那里错了. 狂改!!!!! 然后还是一直... 继续狂改!!!!... 一直.... 最后发现数组开小了.......... 果断 ...
- 【暑假】[实用数据结构]UVAlive 4329 Ping pong
UVAlive 4329 Ping pong 题目: Ping pong Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: % ...
- UVALive 4329 Ping pong
Ping pong Time Limit: 3000MS Memory Limit: Unknown 64bit IO Fo ...
- UVALive 4329 Ping pong(树状数组)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13895 题意:一条街上住有n个乒乓选手,每个人都有一个技能值,现在 ...
- UVALive - 4329 Ping pong 树状数组
这题不是一眼题,值得做. 思路: 假设第个选手作为裁判,定义表示在裁判左边的中的能力值小于他的人数,表示裁判右边的中的能力值小于他的人数,那么可以组织场比赛. 那么现在考虑如何求得和数组.根据的定义知 ...
随机推荐
- August 14th, Week 34th Sunday, 2016
To live is to function, that is all there is in living. 活着就要发挥作用,这就是生活的全部内容. I often joke that my dr ...
- 对于大一学习计算机的新手(c/c++ )提出一些学习经验
对于刚刚上大一的新手,且是那种十分有上进的学生,在学习计算机的过程中必然会有一大堆的困惑,比如: 1 .如何学好编程(这与以往的应试教育完全不同,按照以往的那种学习方式,看书刷题不过是成为一个考试学霸 ...
- MyString(重写String)
http://wenku.baidu.com/view/d7ac113243323968011c925b.html 已知类String的原型为: class String { public: ...
- js对象
js中除数字.字符串.布尔值.null值.undefined之外都是对象. 对象是属性的容器,属性包含属性名和值,属性名可以是包括空字符串在内的任意字符串(个人想法还是使用js标识符好,省的麻烦),值 ...
- 自己动手编译hadoop-2.5.2源码
搭建环境:Centos x 6.5 64bit (后来:我才知道原来官网上发布的就是64位的,不过这个对我来说是个学习过程,对以后进行其他平台编译的时候有帮助!) 1.安装JDK 我这里用的是64位 ...
- python以指定宽度及格式输出字符串
- ASP.NET MVC中ViewData、ViewBag和TempData
1.ViewData 1.1 ViewData继承了IDictionary<string, object>,因此在设置ViewData属性时,传入key必须要字符串型别,value可以是任 ...
- Linux内核驱动之GPIO子系统(一)GPIO的使用
转自:http://blog.csdn.net/mirkerson/article/details/8464290 一 概述 Linux内核中gpio是最简单,最常用的资源(和 interrupt , ...
- python中的Iterable, Iterator,生成器概念
https://nychent.github.io/articles/2016-05/about-generator.cn 这个深刻 谈起Generator, 与之相关的的概念有 - {list, s ...
- Win10 安装Vs2015 社区版和企业版各个问题汇总
1.前提下已经下载ISO文件 2.你的电脑没有连接网络或者你使用了宽带通类似的运营商网络. 3.你确保你正确安装了win10 并且已经激活 出现的问题如下 一.当你安装离线下载的ISO for Vs2 ...