World is Exploding
题意:
给出一个长为n的序列A,问有多少四元组(a, b, c, d)满足$a \ne b \ne c \ne d, 1 \leq a < b \leq n, 1 \leq c < d \leq n, A_a < A_b, A_c > A_d$.
解法:
容斥,考虑用正序对乘以逆序对后,得到的方案中四种不合法情况:
1.$c < d = a < b$
2.$a < b = c < d$
3.$a = c < d$ 且 $a = c < d$
4.$a < b = d$ 且 $c < b = d$
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> #define N 50010
#define LL long long
#define lb(x) (x&(-x)) using namespace std; int n, tot;
int a0[N], a[N], pre[N], suc[N]; int ask(int sum[], int x)
{
int ans = ;
for(int i = x; i > ; i -= lb(i))
ans += sum[i];
return ans;
} void add(int sum[], int x, int v)
{
for(int i = x; i <= tot && i>;i += lb(i))
sum[i] += v;
} LL qsum(int sum[], int l, int r)
{
if(l > r) return ;
return ask(sum, r) - ask(sum, l - );
} int main()
{
while(~scanf("%d", &n))
{
for(int i = ; i <= n; i++)
{
scanf("%d", &a[i]);
a0[i] = a[i];
suc[i] = ;
pre[i] = ;
}
sort(a0 + , a0 + n + );
tot = ;
for(int i = ; i <= n; i++)
if(a0[i] != a0[i-]) a0[++tot] = a0[i];
for(int i = ; i <= n; i++)
{
a[i] = lower_bound(a0 + , a0 + tot + , a[i]) - a0;
add(suc, a[i], );
}
LL ans = , cnt1 = , cnt2 = ;
for(int i = ; i <= n; i++)
{
add(pre, a[i], );
add(suc, a[i], -);
LL pre_higher = qsum(pre, a[i] + , tot);
LL pre_lower = qsum(pre, , a[i] - );
LL suc_higher = qsum(suc, a[i] + , tot);
LL suc_lower = qsum(suc, , a[i] - );
cnt1 += suc_higher;
cnt2 += suc_lower;
ans -= pre_higher * suc_higher;
ans -= pre_lower * suc_lower;
ans -= suc_lower * suc_higher;
ans -= pre_lower * pre_higher;
}
ans += cnt1 * cnt2;
cout << ans << endl;
}
return ;
}
World is Exploding的更多相关文章
- HDU 5792---2016暑假多校联合---World is Exploding
2016暑假多校联合---World is Exploding Problem Description Given a sequence A with length n,count how many ...
- 2016 Multi-University Training Contest 5 World is Exploding
转载自:http://blog.csdn.net/queuelovestack/article/details/52096337 [题意]给你一个序列A,选出四个下标不同的元素,下标记为a,b,c,d ...
- HDU 5792 World is Exploding 树状数组+枚举
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Time Limit: 2000/1000 MS (Ja ...
- 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 5792 World is Exploding (树状数组)
World is Exploding 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...
- hdu_5792_World is Exploding(树状数组+逆序对)
题目链接:hdu_5792_World is Exploding 题意: 给你一个数列,让你找有多少个(a,b,c,d)满足a≠b≠c≠d,1≤a<b≤n,1≤c<d≤n,Aa<Ab ...
- hdu 5792 World is Exploding 树状数组
World is Exploding 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...
- 梯度消失(vanishing gradient)与梯度爆炸(exploding gradient)问题
(1)梯度不稳定问题: 什么是梯度不稳定问题:深度神经网络中的梯度不稳定性,前面层中的梯度或会消失,或会爆炸. 原因:前面层上的梯度是来自于后面层上梯度的乘乘积.当存在过多的层次时,就出现了内在本质上 ...
- This instability is a fundamental problem for gradient-based learning in deep neural networks. vanishing exploding gradient problem
The unstable gradient problem: The fundamental problem here isn't so much the vanishing gradient pro ...
- hdu-5792 World is Exploding(容斥+树状数组)
题目链接: World is Exploding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
随机推荐
- vs2012 MinGW 编译ffmeg 引用外部库libx264,librtmp
VS2012如何编译ffmpeg前面已经有文章讲过,本来主要讲述如何引用外部库libx264,librtmp, ffmpeg版本是3.0.2. 1. 下载x264源文件并编译 源码地址是http:// ...
- Andriod三步学会安卓自己定义视图及其属性
第一步:自己定义属性 第二步:自己定义控件解析属性 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcm5adW9adW8=/font/5a6L5L2T/fo ...
- Linux问题,磁盘分区打不开了
Metadata kept in Windows cache, refused to mount. chkdsk /f http://www.bubuko.com/infodetail-1184937 ...
- jave 金额科学记数法处理
金额 :amount amount.stripTrailingZeros().toPlainString();
- Data Matrix Font and Encoder条码控件可以以字体的形式来打印DataMatrix条形码
Data Matrix Font and Encoder条码控件使您能够以字体的形式来打印DataMatrix条形码. 本产品能够在不论什么支持Java类库..NET动态链接库或Windows COM ...
- LeetCode(125)题解--Valid Palindrome
https://leetcode.com/problems/valid-palindrome/ 题目: Given a string, determine if it is a palindrome, ...
- 未开启HugePages ORACLE session剧增时引起的一次悲剧
故障简单描写叙述一下:LINUX系统未开启HugePages,主机内存将近300G.SWAP是32G.ORACLE 的 SGA_MAX_SIZE设置是主机内存的将近80%,SGA_TARGET设置是主 ...
- #ZgotmplZ go web 开发 base64 图片显示
Go Web开发,用Base64作为图片URL时遇到#ZgotmplZ的问题 - 简书 https://www.jianshu.com/p/54fc25da7c4f // var imgBase64 ...
- user版本如何永久性开启adb 的root权限【转】
本文转载自:http://blog.csdn.net/o0daxu0o/article/details/52933926 [Solution]* adb 的root 权限是在system/core/a ...
- Codeforces Round #385 (Div. 2) Hongcow Builds A Nation —— 图论计数
题目链接:http://codeforces.com/contest/745/problem/C C. Hongcow Builds A Nation time limit per test 2 se ...