题意:给一个数列,求四个各不相同的数,一个逆序对,一个正序对,求多少组这样的四个数。

题解:辣鸡如我,还是上官方题解了。

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的更多相关文章

  1. hdu-5792 World is Exploding(容斥+树状数组)

    题目链接: World is Exploding Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Jav ...

  2. HDU5792 World is Exploding(树状数组)

    一共6种情况,a < b且Aa < Ab, c < d 且Ac > Ad,这两种情况数量相乘,再减去a = c, a = d, b = c, b = d这四种情况,使用树状数组 ...

  3. HDU-5792 World is Exploding(树状数组)

    题目大意:给一个整数序列,统计四元组(a,b,c,d)的个数,满足条件1:a<>b<>c<>d:条件2:<a,b>组成一个顺序对,<c,d> ...

  4. hdu5792 World is Exploding(多校第五场)树状数组求逆序对 离散化

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5792 题目描述:给你n个值,每个值用A[i]表示,然后问你能否找到多少组(a,b,c,d)四个编号,四 ...

  5. World is Exploding(hdu5792)

    World is Exploding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  6. HDU 5792---2016暑假多校联合---World is Exploding

    2016暑假多校联合---World is Exploding Problem Description Given a sequence A with length n,count how many ...

  7. 2016 Multi-University Training Contest 5 World is Exploding

    转载自:http://blog.csdn.net/queuelovestack/article/details/52096337 [题意]给你一个序列A,选出四个下标不同的元素,下标记为a,b,c,d ...

  8. HDU 5792 World is Exploding 树状数组+枚举

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Time Limit: 2000/1000 MS (Ja ...

  9. 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]< ...

  10. HDU 5792 World is Exploding (树状数组)

    World is Exploding 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...

随机推荐

  1. Maven的Dependency怎么找?

    用了Maven,所需的JAR包就不能再像往常一样,自己找到并下载下来,用IDE导进去就完事了,Maven用了一个项目依赖(Dependency)的概念,用俗话说,就是我的项目需要用你这个jar包,就称 ...

  2. 【leetcode】Trapping Rain Water(hard)

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  3. CodeForces 300A Array

    http://codeforces.com/problemset/problem/300/A 题意 :给你n个数字,让你分成3组,第一组各个数之积要小于0,第二组要大于0,第三组要等于0,符合要求的答 ...

  4. hdu 1525 Euclid's Game 博弈论

    思路:两个数a和b,总会出现的一个局面是b,a%b,这是必然的,如果a>=b&&a<2*b,那么只有一种情况,直接到b,a%b.否则有多种情况. 对于a/b==1这种局面, ...

  5. linux ps查看进程命令

    linux ps查看进程命令ps命令作用:将某个时间点的程序运作情况撷取下来 实例: [root@linux ~]# ps aux [root@linux ~]# ps -lA [root@linux ...

  6. mac 用 brew

    mac 下用   brew 安装插件, 有重复的不会再次安装,比xmap模式好

  7. Ado.Net小练习01(数据库文件导出,导入)

    数据库文件导出主要程序: <span style="font-family: Arial, Helvetica, sans-serif;"><span style ...

  8. svn merge部分的详细说明

    http://blog.sina.com.cn/s/blog_620eb3b20101hvz7.html 解决版本冲突-使用SVN主干与分支功能 1  前言 大多数产品开发存在这样一个生命周期:编码. ...

  9. cache设计,以及多核造成的不一致性以及解决方案

    http://www.360doc.com/content/11/1013/00/1317564_155625188.shtml http://blog.csdn.net/muxiqingyang/a ...

  10. BIRT使用1:简介、概念、元素、报表设计器组成

    前一篇博客对birt进行了一个初探,相信通过上篇博客大家对birt有个初步认识,接下来我们随着下面这张思维导图的展示,进入birt的使用学习. 这一篇博客是第一部分,主要介绍一下birt的简介.概念. ...