World is Exploding

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5792

Description

Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a<b≤n,1≤c<d≤n,Aa<Ab,Ac>Ad.

Input

The input consists of multiple test cases.

Each test case begin with an integer n in a single line.

The next line contains n integers A1,A2⋯An.

1≤n≤50000

0≤Ai≤1e9

Output

For each test case,output a line contains an integer.

Sample Input

4

2 4 1 3

4

1 2 3 4

Sample Output

1

0

Hint

题意

给你n个数,问你有多少个四元组,满足a!=b,b!=c,c!=d,a!=d,a!=c,b!=d,且A[a]<A[b],a<b,A[c]>A[d],c<d的关系的。

题解:

考虑容斥,我们知道(a,b)的对数,(c,d)的对数,两个乘起来,再减去不合法的就好了。

不合法的,显然就是长度为3的哪些重复计算的,只要减去就好了。

维护l[i]表示前面有多少个比他小,l1[i]前面有多少个比他大

r[i]后面有多少个比他小,r1[i]有多少个比他大

然后莽一波容斥。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e4+7;
int d[maxn],n,a[maxn],l[maxn],r[maxn],l1[maxn],r1[maxn];;
int lowbit(int x){
return x&(-x);
}
void update(int x,int v){
for(int i=x;i<maxn;i+=lowbit(i))
d[i]+=v;
}
int get(int x){
int ans = 0;
for(int i=x;i;i-=lowbit(i))
ans+=d[i];
return ans;
}
map<int,int>H;
vector<int> V;
int main(){
while(scanf("%d",&n)!=EOF){
memset(l1,0,sizeof(l1));
memset(r1,0,sizeof(r1));
memset(l,0,sizeof(l));
memset(r,0,sizeof(r));
memset(d,0,sizeof(d));
V.clear();H.clear();
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
V.push_back(a[i]);
}
sort(V.begin(),V.end());
V.erase(unique(V.begin(),V.end()),V.end());
for(int i=0;i<V.size();i++)
H[V[i]]=i+1;
for(int i=1;i<=n;i++)a[i]=H[a[i]]; long long ans1=0,ans2=0,ans3=0;
for(int i=1;i<=n;i++){
l[i]=get(a[i]-1);
l1[i]=get(maxn-1)-get(a[i]);
update(a[i],1);
}
memset(d,0,sizeof(d));
for(int i=n;i>=1;i--){
r[i]=get(a[i]-1);
r1[i]=get(maxn-1)-get(a[i]);
update(a[i],1);
}
for(int i=1;i<=n;i++){
ans3+=1ll*l[i]*r[i];
ans3+=1ll*l1[i]*r1[i];
ans3+=1ll*l[i]*l1[i];
ans3+=1ll*r[i]*r1[i];
ans1+=l[i],ans2+=r[i];
}
cout<<ans1*ans2-ans3<<endl;
}
}

hdu 5792 World is Exploding 树状数组的更多相关文章

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

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

  2. hdu 5792 World is Exploding 树状数组+离散化+容斥

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

  3. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  4. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

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

  6. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  7. HDU 5862 Counting Intersections (树状数组)

    Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...

  8. hdu 5592 ZYB's Game 树状数组

    ZYB's Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=55 ...

  9. HDU 1394 Minimum Inversion Number (树状数组求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...

随机推荐

  1. 何凯文每日一句打卡||DAY14

  2. Django中合并同一个model的多个QuerySet

    [1]相同modelarticles1 = Article.objects.order_by("autoid").filter(autoid__lt = 16).values('a ...

  3. HDU 4472 Count DP题

    解题报告:题目大意,给你n个球,要将这n个球从下到上按层次排列,要求同一个层次的的每一个分支的数量都必须相等,问有多少种排列的方法. 此题的一个DP题,假设现在有n个球,要将这n个球排列好,我们就必须 ...

  4. HDU 1728 逃离迷宫 BFS题

    题目描述:输入一个m*n的地图,地图上有两种点,一种是 . 表示这个点是空地,是可以走的,另一种是 * ,表示是墙,是不能走的,然后输入一个起点和一个终点,另外有一个k输入,现在要你确定能否在转k次弯 ...

  5. Android学习笔记——从源码看Handler的处理机制

    可能是出于性能的考虑,Android的UI操作是非线程安全的. 也就是说,如果你在一个新开的线程中直接操作UI是会引发异常的. 但是,Android又规定,不要去阻塞UI线程!否则,轻者引起程序卡顿, ...

  6. json 删除、添加对象

    1. 定义json对象    var entryJson = []; 2. 删除.添加对象 entryJson.pop();     //删除最后一个对象   entryJson.push({ //往 ...

  7. 扩展欧几里得(E - The Balance POJ - 2142 )

    题目链接:https://cn.vjudge.net/contest/276376#problem/E 题目大意:给你n,m,k,n,m代表当前由于无限个质量为n,m的砝码.然后当前有一个秤,你可以通 ...

  8. requests(二): json请求中固定键名顺序&消除键和值之间的空格

    继上一篇requests发送json请求的文章后,实际工作中遇到了以下2种情况. 1:服务端要求json字符串,键名的顺序固定  2.服务端对于接收到的json数据中,若key和value之间有空格, ...

  9. 搜索引擎ElasticSearchV5.4.2系列一之ES介绍

    相关博文: 搜索引擎ElasticSearchV5.4.2系列一之ES介绍 搜索引擎ElasticSearchV5.4.2系列二之ElasticSearchV5.4.2+kibanaV5.4.2+x- ...

  10. make distclean

    清空bin目录make dirclean 清空所有相关的东西,包括下载的软件包,配置文件,feeds内容等make distclean 这个命令会删除feeds目录及其下面的所有的文件,直接结果就是运 ...