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;}
}

转载于:https://www.cnblogs.com/qscqesze/p/5730435.html

hdu 5792 World is Exploding 树状数组相关推荐

  1. hdu 5157(manacher+前缀和+树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5157 解题思路: 我们可以先用mancher算法对字符串进行处理,把以每个点为中心的回文串半径求出来, ...

  2. HDU 5517---Triple(二维树状数组)

    题目链接 Problem Description Given the finite multi-set A of n pairs of integers, an another finite mult ...

  3. hdu 6447YJJ's Salesman 离散化+树状数组+DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 因为图中点的坐标值过大,达到1e9.然而只有1e5个点.所以先将其离散化.并按照<x.y& ...

  4. [HDU - 2852] KiKi's K-Number (树状数组+二分)

    链接 http://acm.hdu.edu.cn/showproblem.php?pid=2852 题意 现在需要你对一个空序列做nnn次操作,操作分三种 0x0\ \ x0  x :向序列中加入一个 ...

  5. HDU - 5775 - Bubble Sort( 树状数组 + 思维 )

    题目链接:点击进入 题目 题意 问在给出的冒泡排序过程中,一个数到达的最右边位置与最左边位置距离差. 思路 对于一个数,位置 i ,假设右边比它小的数有 r 个,左边比它大的数有 l 个,最右边到达的 ...

  6. POJ 3928 hdu 2492 Uva1428 PingPong 【树状数组】

    Ping pong                                                   Time Limit: 2000/1000 MS (Java/Others)   ...

  7. HDU 3584 Cube (三维树状数组)

    Problem Description Given an N*N*N cube A, whose elements are either 0 or 1. A[i, j, k] means the nu ...

  8. HDU 3015 Disharmony Trees(树状数组)

    题意:给你n棵树,每棵树上有两个权值X H 对于X离散化 :3 7 1 5 3 6 -> 2 6 1 4 2 5,对于H一样 然后F = abs(X1-X2)   S=min(H1,H2) 求出 ...

  9. HDOJ/HDU 1556 Color the ball(树状数组)

    Problem Description N个气球排成一排,从左到右依次编号为1,2,3-.N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从 ...

最新文章

  1. 使用Powershell管理Linux 下的 SQL Server
  2. Android Studio添加文件注释头模板?
  3. ubuntu交叉编译x264报错:‘X264_VERSION’ undeclared(已解决)运行version.sh
  4. Emmet 语法 速查表
  5. 记一次尴尬的git reset丢失分支故障
  6. Tensorflow模型通过ckpt获取参数
  7. 如何调整一个 IFrame 到其内容的大小不显示滚动条[微软帮助]
  8. CSS中filter滤镜的学习笔记
  9. 【51单片机】利用烧录软件生成延时函数 入门学习
  10. JAVA获取服务器文件名乱码,java获取的文件名乱码问题
  11. 数据库实验报告--安全性实验
  12. 腾讯微云下载慢解决办法
  13. 从数码宝贝看spring bean的生命周期
  14. 防止刷新页面造成表单重复提交
  15. 普通话测试软件测分准吗,普通话学习测试类 | 普通话测试APP,能做到测试、学习提高两不误吗?...
  16. HDU 4417 Super Mario(划分树问题求不大于k的数有多少)
  17. win10下loadrunner11安装与破解
  18. AutoFIS阅读总结
  19. java程序设计项目_JAVA程序设计项目报告书.doc
  20. 基于SRS搭建RTMP直播流媒体服务器

热门文章

  1. 用Django开发Web应用程序异常
  2. Script:列出失效索引或索引分区
  3. TESLA CENTER
  4. counterfactual
  5. the next journal submission for mechanism should be at this journal
  6. 我建议您还是不要尝试和外国人讨论文学了,您干不过的。。。还是跟他们拼数学,逻辑,编程比较合适
  7. Mac m1 ocr 工具
  8. U3D 代码自动化生成定制预置体的旋转问题
  9. sceneManager.loadscene加载场景时不会主动去加载场景的依赖包,要手动加载或添加场景到build setting列表中...
  10. LUA table.sort的问题,数组与表的区别