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

最新文章

  1. google play iap 常见问题
  2. AJAX JSONP源码实现(原理解析)
  3. MySql安装与MySQL添加用户、删除用户与授权
  4. SQL in与exists
  5. Smart210学习-----lcd驱动
  6. CSS控制div宽度最大宽度/高度和最小宽度/高度
  7. 设置outlook自动回复
  8. [2016-03-15]rabbitmq notes
  9. Dijkstra and Floyd算法
  10. xml的xPath解析规则
  11. 策略模式(Stratety)
  12. Android BLE与终端通信(三)——客户端与服务端通信过程以及实现数据通信
  13. [译]Ocelot - Tracing
  14. SAP S/4 HANA 1709 Fully Activated Appliance
  15. R语言-简单模型画图
  16. Linux修改hostname时/etc/hosts、/etc/sysconfig/network ,hostname,三者的区别和联系
  17. php接口 接受ios或android端图片; php接收NSData数据
  18. Android.mk (1) 函数
  19. ReactNative For Android 项目实战总结
  20. python+selenium之简单介绍继承

热门文章

  1. 用unsigned char 表示字节
  2. 【PostgreSQL-9.6.3】进程及体系结构
  3. JS的type类型为 text/template
  4. HTTP的工作原理
  5. 面向UI编程思想
  6. mysql允许远程连接的命令
  7. getHiddenProp() 浏览器状态切换改变
  8. Java格式化CST时间(mysql date类型)
  9. [BZOJ] 1037 [ZJOI2008]生日聚会
  10. 我能考虑到的数组(老)方法就这些了(es5)