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 树状数组
这题不是一眼题,值得做. 思路: 假设第个选手作为裁判,定义表示在裁判左边的中的能力值小于他的人数,表示裁判右边的中的能力值小于他的人数,那么可以组织场比赛. 那么现在考虑如何求得和数组.根据的定义知 ...
随机推荐
- 电子现金、电子钱包、qPBOC、闪付、UPCash
一.关于金融IC卡领域的规范 由Europay.Mastercard.Visa三大国际信用卡组织联合制定的金融集成电路(IC)卡金融支付标准,称为EMV规范,其目的是为金融IC卡.金融终端.支付系统以 ...
- XML 数据请求与JSON 数据请求
(1)XML 数据请求 使用 AFNetworking 中的 AFHTTPRequestOperation 和 AFXMLParserResponseSerializer,另外结合第三方框架 XMLD ...
- Android笔记:管理所有活动
以关闭所有活动为例 public class ActivityCollector { public static List<Activity> activities = new Array ...
- Mysql 调用存储过程的两种方式
一,使用call语句: 如:创建 call 调用: 使用占位符,通过prepare,execute调用:
- 解决osg路径与文件名中的中文字符问题
转至:http://blog.csdn.net/zhuqinglu/article/details/2064013 在打开或者保存一个osg模型的时候,经常遇到中文路径或者中文文件名的问题,此时会提示 ...
- 图文转换——NABCD
我们小组要做的是一款可以对文字进行扫描转为txt格式将文字保存下来的移动通信终端的APP. N-need 生活中经常遇到这种问题,看到报纸上的一篇文章,特别想收藏这篇文章,或者是在网上看到一篇好 ...
- AJAX JSON类型返回
文本样式和下拉样式 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- nginx 配一个简单的静态文件服务器 和一个虚似机
下面是个图片服务器: server { listen ; server_name img.xxx.xxx.com; root /data/site/img.xxx.xxx.com; access_lo ...
- Jquery.Datatables 结合时间段查询,daterangepicker实现Datatables表格带参数查询
参考:http://datatables.club/example/user_share/send_extra_param.html 下载地址:http://pan.baidu.com/s/1 ...
- 阿里云 SWAP
https://yq.aliyun.com/articles/52098 https://www.kejianet.cn/aliyun-swap/