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>Ada≠b≠c≠d,1≤a<b≤n,1≤c<d≤n,Aa<Ab,Ac>Ad.

InputThe 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⋯AnA1,A2⋯An. 
1≤n≤500001≤n≤50000 
0≤Ai≤1e90≤Ai≤1e9OutputFor 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 因为只考虑相对大小关系,所以先将数据离散化,然后用树状数组记录前后比i大或者比i小的元素,求出所有个数,分类讨论重合情况,减去重合的元素
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<fstream>
#include<set>
#include<memory>
#include<bitset>
#include<string>
#include<functional>
using namespace std;
typedef long long LL;
#define MAXN 500009
LL pre_max[MAXN], pre_min[MAXN], post_max[MAXN], post_min[MAXN];
LL a[MAXN], tmp[MAXN];
LL T[MAXN], n;
LL lowbit(LL x)
{
return x&(-x);
}
void update(LL x)
{
while (x <= MAXN)
{
T[x] += ;
x += lowbit(x);
}
}
LL getsum(LL x)
{
LL sum = ;
while (x > )
{
sum += T[x];
x -= lowbit(x);
}
return sum;
}
int main()
{
while (scanf("%lld", &n) != EOF)
{
memset(T, , sizeof(T));
for (LL i = ; i <= n; i++)
scanf("%lld", &a[i]);
memcpy(tmp, a, sizeof(a));
sort(tmp + , tmp + n + );
LL len = unique(tmp + , tmp + n + ) - tmp;
for (LL i = ; i <= n; i++)
a[i] = lower_bound(tmp + , tmp + len + , a[i]) - tmp;
for (LL i = ; i <= n; i++)
{
update(a[i]);
pre_min[i] = getsum(a[i] - );
pre_max[i] = i - getsum(a[i]);
}
LL sum1, sum2;
sum1 = sum2 = ;
for (int i = ; i <= n; i++)
{
post_min[i] = getsum(a[i] - ) - pre_min[i];
post_max[i] = n - getsum(a[i]) - pre_max[i];
sum1 += post_min[i];
sum2 += post_max[i];
}
LL ans = sum1*sum2;
for (int i = ; i <= n; i++)
{
ans -= pre_min[i] * pre_max[i];
ans -= pre_min[i] * post_min[i];
ans -= post_max[i] * pre_max[i];
ans -= post_max[i] * post_min[i];
}
printf("%lld\n", ans);
}
}

World is Exploding 树状数组+离散化的更多相关文章

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

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

  2. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. BZOJ_5055_膜法师_树状数组+离散化

    BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...

  4. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  5. HDU 5792 World is Exploding(树状数组+离散化)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5792 题意: 思路: lmin[i]:表示左边比第i个数小的个数. lmax[i]:表示左边比第i个 ...

  6. HDU 5792 L - World is Exploding 。容斥原理 + 树状数组 + 离散化

    题目,要求找出有多少对这样的东西,四个数,并且满足num[a]<num[b] &&num[c]>num[d] 要做这题,首先要懂得用树状数组,我设,下面的小于和大于都是严格 ...

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

  8. BZOJ-1227 虔诚的墓主人 树状数组+离散化+组合数学

    1227: [SDOI2009]虔诚的墓主人 Time Limit: 5 Sec Memory Limit: 259 MB Submit: 914 Solved: 431 [Submit][Statu ...

  9. POJ 2299 树状数组+离散化求逆序对

    给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...

随机推荐

  1. 块级元素的text-align对行内元素和果冻元素(inline-block)的作用

    块级元素社设置了text-align:center以后,对其直接行内元素/果冻元素.继承行内元素/果冻元素都会产生“居中效应”. <style> .test4{ text-align: c ...

  2. 手机端左右滑动,不用写js(只有页面切换到移动端可以看)

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. greendao3.2.3配置时遇到的问题

    这两天我一直在研究greendao这个框架,我在GitHub下载了 greendao3.2.2:https://github.com/greenrobot/greenDAO,照着网址里面来配置: // ...

  4. java设计模式之代理模式 ,以及和java 回调机制的区别

    java 代理模式就是: 将自己要做的事交给别人去做(这个别人就是代理者,自己就是被代理者),为什么自己能做的要交给别人去做了?假如一个小学生小明,现在要写作业,但是又想玩游戏,他更想玩游戏,并且不想 ...

  5. JData 整合ArtTemplate的前端框架

    因为项目需要和自己的兴趣,几个月前结合模板解析神速的ArtTemplate,自己写了个框架取名JData,多多指教啊---因为一直没时间写文档,为了能够更方便地使用和避免我把代码忘了,今天抽空把文档写 ...

  6. java.math.BigDecimal类multiply的使用

    java.math.BigInteger.multiply(BigInteger val) 返回一个BigInteger,其值是 (this * val).声明 以下是java.math.BigInt ...

  7. AutoIt简单使用

    以上是自己在公司做培训的PPT首页,其实在线的中文参考文档很全面,很值得学习的.

  8. 从mysql全库备份中恢复指定库和指定表

    需求:开发要求导入某天某个表的数据,而我们的数据是全库备份 例如:  从newbei_2017-08-31_402793782.tar.bz2中恢复表:bei_table 的数据 一.备份策略 备份全 ...

  9. CAD交互绘制矩形框(com接口)

    主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE dY1 直线的开始点y坐标 DOUBLE ...

  10. asp.net中js和jquery调用ashx的不同方法分享,需要的朋友可以参考一下

    asp.net中js和jquery调用ashx的不同方法分享,需要的朋友可以参考一下     =============js================ 复制代码代码如下: var xhr = n ...