Number sequence

Given a number sequence which has N element(s), please calculate the number of different collocation for three number Ai, Aj, Ak, which satisfy that Ai < Aj > Ak and i < j < k.

Input

The first line is an integer N (N <= 50000). The second line contains N integer(s): A1, A2, ..., An(0 <= Ai <= 32768).

Output

There is only one number, which is the the number of different collocation.

Sample Input

5
1 2 3 4 1

Sample Output

6

这题能够用树状数组做,开两个一维的树状数组分别记录当前点前面的比这点小的个数和后面比这点大的个数。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 50005
#define ll long long
int b1[maxn],b2[maxn],a[maxn];
int lowbit(int x){
return x&(-x);
}
void update1(int pos,int num)
{
while(pos<=maxn){
b1[pos]+=num;pos+=lowbit(pos);
}
}
int getsum1(int pos)
{
int num=0;
while(pos>0){
num+=b1[pos];pos-=lowbit(pos);
}
return num;
} void update2(int pos,int num)
{
while(pos<=maxn){
b2[pos]+=num;pos+=lowbit(pos);
}
}
int getsum2(int pos)
{
int num=0;
while(pos>0){
num+=b2[pos];pos-=lowbit(pos);
}
return num;
} int main()
{
int n,m,i,j;
ll num=0;
while(scanf("%d",&n)!=EOF)
{
memset(b1,0,sizeof(b1));
memset(b2,0,sizeof(b2));
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
a[i]++;
if(i==1){
update1(a[i],1);continue;
}
update2(a[i],1);
}
num=0;
for(i=2;i<=n-1;i++){
num+=getsum1(a[i]-1)*getsum2(a[i]-1);
update1(a[i],1);
update2(a[i],-1);
}
printf("%lld\n",num);
}
return 0;
}

最新文章

  1. mongoDB 使用手册
  2. img 在层内 居中显示
  3. LTIB常用命令3
  4. MySql数据库1【概念】
  5. 谈谈android 布局 的优化
  6. you can Solve a Geometry Problem too(hdoj1086)
  7. python导入模块的方法
  8. IOS基础开发二(iphone计算器)
  9. 利用StringBuffer向字符串特定的重复子字符串插入数据
  10. Linux内存布局
  11. AmpLab Tachyon and Shark update
  12. PL/SQL 三个小例子
  13. UITableViewBase&amp;nbsp;UI_09
  14. XML配置spring session jdbc实现session共享
  15. 往服务器部署thinkphp5代码时要注意 pathinfo的问题
  16. SSH总结
  17. MYSQL 常用函数大全
  18. HTTP中的Get与Post
  19. 克隆后没有IP
  20. SharePoint 2013 Step by Step—— 为终端用户提供故障恢复的解决方案 Part I

热门文章

  1. poj 3623 Best Cow Line, Gold
  2. Swift - 按钮(UIButton)的用法
  3. MySQL 关闭FOREIGN_KEY_CHECKS检查
  4. C#写的客户端连接 php的服务器端的小例子
  5. SilkTest Q&amp;A 3
  6. Android资源管理框架(Asset Manager)简介和学习计划
  7. codeforces 598B Queries on a String
  8. win7下怎样设置putty免用户名密码登陆
  9. Android 异步链式调用设计
  10. ACM-计算几何之Quoit Design——hdu1007 zoj2107