hdu 5792 World is Exploding 树状数组
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 树状数组的更多相关文章
- HDU 5792 World is Exploding 树状数组+枚举
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Time Limit: 2000/1000 MS (Ja ...
- hdu 5792 World is Exploding 树状数组+离散化+容斥
World is Exploding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- 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 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- HDU 5862 Counting Intersections (树状数组)
Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...
- 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 ...
- HDU 1394 Minimum Inversion Number (树状数组求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...
随机推荐
- ngx_lua_API 指令详解(五)coroutine.create,coroutine.resume,coroutine.yield 等集合指令介绍
ngx_lua 模块(原理实现) 1.每个worker(工作进程)创建一个Lua VM,worker内所有协程共享VM: 2.将Nginx I/O原语封装后注入 Lua VM,允许Lua代码直接访问: ...
- Does Deep Learning Come from the Devil?
Does Deep Learning Come from the Devil? Deep learning has revolutionized computer vision and natural ...
- 网络_OSI模型_数据包传输
2017年1月12日, 星期四 网络_OSI模型_数据包传输 1. 网络_源主机_局域网_交换机_路由器_目标主机 2. OSI7七层_TCP/IP精简 OSI 7层: 应用层 ...
- Algorithm(1) - Karatsuba multiplication
这个系列主要是记一下目前效率较高或者比较出名的一些算法. Karatsuba multiplication: x=5678 then: a=56 b=67 y=1234 c= ...
- MongoDB 之 手把手教你增删改查 MongoDB - 2
我们在 MongoDB 之 你得知道MongoDB是个什么鬼 MongoDB - 1 中学习了如果安装部署一个 MongoDB 如果没看到我的金玉良言的话,就重新打开一次客户端和服务端吧 本章我们 ...
- mysql数据库的快捷键
mysql数据库快捷键 ctrl+q 打开查询窗口 ctrl+/ 注释sql语句 ctrl+shift +/ 解除注释 ctrl+r 运行查询窗口的sql语句 ctrl+shift+r 只运行选中的s ...
- docker 构建带健康检查的redis镜像
=============================================== 2018/11/5_第1次修改 ccb_warlock == ...
- 输入操作遇到unknown error: cannot focus element
事件背景:写脚本遇到sendkey时报错unknown error: cannot focus element,仔细查了,元素定位什么的都没问题,通过js注入修改数据后,保存成功,但是再进入编辑状态查 ...
- 在阿里云申请Symantec免费SSL证书操作流程
2016年阿里云与国内证书颁发机构天威诚信推出了基于Symantec(赛门铁克)的免费SSL证书,有需要免费SSL证书产品的可以前往阿里云进行申请. 申请地址:阿里云云盾证书服务—Symantec免费 ...
- NOIp 2018 普及组
T1标题统计 传送门 题目描述 凯凯刚写了一篇美妙的作文,请问这篇作文的标题中有多少个字符? 注意:标题中可能包含大.小写英文字母.数字字符.空格和换行符.统计标题字 符数时,空格和换行符不计算在内. ...