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

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

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. Linux中的栈:用户态栈/内核栈/中断栈

    http://blog.chinaunix.net/uid-14528823-id-4136760.html Linux中有多种栈,很容易弄晕,简单说明一下: 1.用户态栈:在进程用户态地址空间底部, ...

  2. SSH应该使用密钥还是密码?

    关于SSH,几乎每个人都同意密钥要优于密码,更安全,并且更先进,但我并不同意这个观点. 虽然密钥的确可以更好,但它有着还没被意识到的严重风险,并且我认为比得到妥善管理的密码更不安全. 通常密钥更好的理 ...

  3. ExecutorService.execute(Runnable x) 判断是否完成,得到返回值

    public class RunnableTestMain { public static void main(String[] args) { ExecutorService pool = Exec ...

  4. bzoj2431:[HAOI2009]逆序对数列

    单组数据比51nod的那道题还弱...而且连优化都不用了.. #include<cstdio> #include<cstring> #include<cctype> ...

  5. windows获取窗口句柄

    1.使用FindWindow函数获取窗口句柄 示例:使用FindWindow函数获取窗口句柄,然后获得窗口大小和标题,并且移动窗口到指定位置. #include <Windows.h> # ...

  6. UEFI GPT

    其实关于UEFI的几篇文章很早就写下了,只是自己读了一遍感觉很不满意,就决定重写.目的是想用最简单直白的语言把内容写出来,让每个人都能轻松读懂.当然,如果你已经对这些内容有了很深的理解的话,这篇文章除 ...

  7. 解决java switch……case不能匹配字符串的问题

    java1.7已经支持了匹配字符串 方案1. enum Animal { dog,cat,bear; public static Animal getAnimal(String animal){ re ...

  8. mysql_insert_id 为什么会返回空值

    如果同时打开了一个以上的数据库资源,如果其中一个资源,没有使用insert语句或没有auto_increment类型的数据,或返回结果恰好为空值时,会导致mysql_insert_id()返回空值. ...

  9. (一)学习C#之浮点类型float小结

    类型:float 大小:32位 范围a:±3.4E38  MSDNhttp://msdn.microsoft.com/zh-cn/library/b1e65aza.aspx 范围b: ±1.5E45~ ...

  10. 使用busybox制作rootfs

    Build Busybox as a static binary(no shared libs),如果选择上,则busybox将以静态形式进行编译,否则将以动态方式编译.此外,还需要对交叉编译环境进行 ...