hdu5792--World is Exploding
题意:给一个数列,求四个各不相同的数,一个逆序对,一个正序对,求多少组这样的四个数。
题解:辣鸡如我,还是上官方题解了。

rg(i)就是i右边比i大的数的个数,rs(i)就是i右边比i小的数的个数。
lg(i)就是i左边比i大的数的个数,ls(i)就是i左边比i小的数的个数。
allg就是所有逆序对的个数,∑rs(i)或者∑lg(i)都可以。
然后答案就是对于每一个数,当它为正序对中较小的那个数时的答案相加。
这样算会有重复,就是那个正序对中较大的数也被算入了逆序对中,所以再减去每个数字作为正序对较大的数时的它所可能的逆序对数量。
注意用会超int 用long long
// 2016多校5-1012/hdu5792
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#include <cmath>
using namespace std;
#define PF(x) cout << "debug:" << x << " "; const int N = ;
typedef long long ll;
int a[N], b[N];
int lg[N], ls[N], rg[N], rs[N]; int bit[N]; int lowbit(int x) { return x&-x; } void add(int p, int v, int n)
{
while (p <= n) {
bit[p] += v;
p += lowbit(p);
}
} ll sum(int p)
{
ll ans = ;
while (p > ) {
ans += bit[p];
p -= lowbit(p);
}
return ans;
} int main(int argc, char const *argv[])
{
freopen("in", "r", stdin);
int n;
while (~scanf("%d", &n)) {
for (int i = ; i <= n; ++i) {
scanf("%d", a+i);
b[i] = a[i];
}
//离散话
sort(a+, a+n+);
int tot = unique(a+, a+n+) - a;
for (int i = ; i <= n; ++i) {
b[i] = lower_bound(a, a+tot, b[i]) - a;
}
//for (int i = 1; i <= n; ++i) printf("%d\n", b[i]);
//求lg ls
memset(bit, , sizeof bit);
for (int i = ; i <= n; ++i) {
ls[i] = sum(b[i]-);
lg[i] = i - - sum(b[i]);
add(b[i], , n);
}
//求rg rs
memset(bit, , sizeof bit);
for (int i = n; i > ; --i) {
rs[i] = sum(b[i]-);
rg[i] = n - i - sum(b[i]);
add(b[i], , n);
}
// cout << "ls "; for (int i = 1; i <= n; ++i) printf("%d ", ls[i]); printf("\n");
// cout << "lg "; for (int i = 1; i <= n; ++i) printf("%d ", lg[i]); printf("\n");
// cout << "rs "; for (int i = 1; i <= n; ++i) printf("%d ", rs[i]); printf("\n");
// cout << "rg "; for (int i = 1; i <= n; ++i) printf("%d ", rg[i]); printf("\n");
//求allg
ll allg = ;
for (int i = ; i <= n; ++i) allg += lg[i];
//PF(allg);
ll ans = ;
for (int i = ; i <= n; ++i) {
ans += (ll)rg[i] * (allg - lg[i] - rs[i]);
}
for (int i = ; i <= n; ++i) {
ans -= (ll)ls[i] * (lg[i] + rs[i]);
}
cout << ans << endl;
}
return ;
}
hdu5792--World is Exploding的更多相关文章
- hdu-5792 World is Exploding(容斥+树状数组)
题目链接: World is Exploding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- HDU5792 World is Exploding(树状数组)
一共6种情况,a < b且Aa < Ab, c < d 且Ac > Ad,这两种情况数量相乘,再减去a = c, a = d, b = c, b = d这四种情况,使用树状数组 ...
- HDU-5792 World is Exploding(树状数组)
题目大意:给一个整数序列,统计四元组(a,b,c,d)的个数,满足条件1:a<>b<>c<>d:条件2:<a,b>组成一个顺序对,<c,d> ...
- hdu5792 World is Exploding(多校第五场)树状数组求逆序对 离散化
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5792 题目描述:给你n个值,每个值用A[i]表示,然后问你能否找到多少组(a,b,c,d)四个编号,四 ...
- World is Exploding(hdu5792)
World is Exploding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 5792---2016暑假多校联合---World is Exploding
2016暑假多校联合---World is Exploding Problem Description Given a sequence A with length n,count how many ...
- 2016 Multi-University Training Contest 5 World is Exploding
转载自:http://blog.csdn.net/queuelovestack/article/details/52096337 [题意]给你一个序列A,选出四个下标不同的元素,下标记为a,b,c,d ...
- HDU 5792 World is Exploding 树状数组+枚举
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Time Limit: 2000/1000 MS (Ja ...
- 2016 Multi-University Training Contest 5 1012 World is Exploding 树状数组+离线化
http://acm.hdu.edu.cn/showproblem.php?pid=5792 1012 World is Exploding 题意:选四个数,满足a<b and A[a]< ...
- HDU 5792 World is Exploding (树状数组)
World is Exploding 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...
随机推荐
- 《Introduction to Tornado》中文翻译计划——第五章:异步Web服务
http://www.pythoner.com/294.html 本文为<Introduction to Tornado>中文翻译,将在https://github.com/alioth3 ...
- poj 3440 Coin Toss 概率问题
这题主要是推导数学公式!!! 将概率问题转化为圆心所在的面积! 代码如下: #include<iostream> #include<stdio.h> #include<a ...
- 孟岩的c++ 的学习方法,这何尝有不是做人做事的方法呢?
“(孟岩)我主张,在具备基础之后,学习任何新东西,都要抓住主线,突出重点.对 于关键理论的学习,要集中精力,速战速决.而旁枝末节和非本质性的知识内容,完全可 以留给实践去零敲碎打. “原因是这样的,任 ...
- mybatis和spring3.1整合
因spring3发布时mybatis还没有出正式版本,所以spring没有整合最新的mybatis.不过社区倒是开发了一个中间件. 需要的jar包 mybatis-3.0.6.jar mybatis- ...
- orm框架与缓存的关系
1.mybatis规定,一级缓存没必要bean类实现序列化,但二级缓存bean类必须实现序列化. 因为二级缓存是基于namespace的也就是基于接口的,二级缓存可以设置存储源,可以是redis或者m ...
- ArcGIS学习记录—属性表的编辑与修改
原文地址: ArcGIS问题:属性表的编辑与修改 - Silent Dawn的日志 - 网易博客 http://gisman.blog.163.com/blog/static/344933882009 ...
- Web缓存杂谈--Etag & If-None-Match
一.概述 缓存通俗点,就是将已经得到的‘东东’存放在一个相对于自己而言,尽可能近的地方,以便下次需要时,不会再二笔地跑到起始点(很远的地方)去获取,而是就近解决,从而缩短时间和节约金钱(坐车要钱嘛). ...
- ListView使用CursorAdapter增加和删除item
@Override protected void onCreate(Bundle savedInstanceState) { // TODO 自动生成的方法存根 super.onCreate(save ...
- codevs3731 寻找道路
方向dfs判定是否可行,spfa跑最短路. noip水题,wa好几次. #include<cstdio> #include<algorithm> #include<cst ...
- sql 的错误处理功能很弱
--下面演示了SQL错误处理的脆弱性--邹建 --演示1--测试的存储过程1create proc p1asprint 12/0if @@error<>0print '发生错误1' sel ...