题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5792

World is Exploding

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
#### 问题描述
>![](http://images2015.cnblogs.com/blog/809202/201608/809202-20160807150517450-1373867798.png)
#### 输入
> 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

输出

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

题解

数据给的50000,枚举两个点是不可能了,但题目只是要四元组的个数,并没有问每个四元组长什么样,那么我们可以考虑预处理统计一些值出来,枚举一个点尝试一下。

我们预处理出四个数组:ls[i],lg[i],rs[i],rg[i]。分别表示i左边比它小的,比它大的;i右边比它小的,比它大的个数。然后我们每次枚举左下角的点去做,再扣去算出来是三个点(一个点算重了,注意:不可能有两个点都算重)的情况,就可以了。具体的统计公式看代码。

代码

#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define pb(v) push_back(v)
#define sz() size()
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++) using namespace std; typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8; //start---------------------------------------------------------------------- const int maxn = 5e4+10;
const int mod=21092013; int n; int ls[maxn],lg[maxn],rs[maxn],rg[maxn];
int arr[maxn],arr2[maxn];
VI ha;
int sumv[maxn];
void add(int x,int v){
while(x<=n){
sumv[x]+=v;
x+=x&(-x);
}
} int sum(int x){
int ret=0;
while(x>0){
ret+=sumv[x];
x-=x&(-x);
}
return ret;
} void init(){
ha.clear();
clr(ls,0);
clr(lg,0);
clr(rs,0);
clr(rg,0);
clr(sumv,0);
} int main() {
while(scanf("%d",&n)==1&&n){
init();
rep(i,0,n){
scanf("%d",&arr[i]);
arr2[i]=arr[i];
ha.pb(arr[i]);
}
sort(arr2,arr2+n);
sort(all(ha));
ha.erase(unique(all(ha)),ha.end());
LL sums=0;
rep(i,0,n){
int id=lower_bound(all(ha),arr[i])-ha.begin()+1;
ls[i]=sum(id-1); lg[i]=sum(n)-sum(id);
int p1=lower_bound(arr2,arr2+n,arr[i])-arr2;
int p2=upper_bound(arr2,arr2+n,arr[i])-arr2;
rs[i]=p1-ls[i]; rg[i]=n-p2-lg[i];
sums+=rs[i];
add(id,1);
}
LL ans=0;
rep(i,0,n){
ans+=rg[i]*(sums-rs[i]-lg[i]);
}
rep(i,0,n){
ans-=ls[i]*(rs[i]+lg[i]);
}
printf("%lld\n",ans);
}
return 0;
} //end-----------------------------------------------------------------------

HDU 5792 World is Exploding 树状数组+枚举的更多相关文章

  1. hdu 5792 World is Exploding 树状数组

    World is Exploding 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...

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

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

  3. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  4. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

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

  6. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  7. HDU 5862 Counting Intersections (树状数组)

    Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...

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

  9. HDU 1394 Minimum Inversion Number (树状数组求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...

随机推荐

  1. 安卓手机的touchend事件不触发问题

    问题描述 $(document).on("touchstart touchmove",".btn-highlight",function(event){ $(t ...

  2. Spark RDD的依赖解读

    在Spark中, RDD是有依赖关系的,这种依赖关系有两种类型 窄依赖(Narrow Dependency) 宽依赖(Wide Dependency) 以下图说明RDD的窄依赖和宽依赖 窄依赖 窄依赖 ...

  3. 用户View,五大布局

    1.LinearLayout 线性布局 android:orientation="horizontal" 制定线性布局的排列方式 水平 horizontal 垂直 vertical ...

  4. 第七章 管理类型(In .net4.5) 之 使用类型

    1. 概述 本章介绍 值类型的装箱拆箱.类型转换 以及 C#4.0新推出的 dynamic 关键字. 2. 主要内容 2.1 装箱和拆箱 2.2 类型转换 有四种方式可以实现类型转换: ① 隐式转换: ...

  5. PHPStorm配置支持友好的Laravel代码自动提示

    在项目的composer.json "barryvdh/laravel-ide-helper":"dev-master" 项目config/app.php Ba ...

  6. windows下python+flask环境配置详细图文教程

    本帖是本人在安装配置python和flask环境时所用到的资源下载及相关的教程进行了整理罗列,来方便后面的人员,省去搜索的时间.如果你在安装配置是存在问题可留言给我. 首先罗列一下python+fla ...

  7. [第四版]用getaddrinfo设置tcp基本连接属性

    getaddrinfo getaddrinfo的一个重要功能, 很方便的构造struct sockaddr_in对象, 把繁琐的构造过程隐藏起来 getaddrinfo兼有gethostbyname和 ...

  8. Stream,Reader/Writer,Buffered的区别(1)

    Stream: 是字节流形式,exe文件,图片,视频等.支持8位的字符,用于 ASCII 字符和二进制数据. Reader/Writer: 是字符流,文本文件,XML,txt等,用于16位字符,也就是 ...

  9. 九度oj 1523 从上往下打印二叉树

    原题链接:http://ac.jobdu.com/problem.php?pid=1523 建树,再层次遍历bfs.为了找根方便些,加了father指针... 如下: #include<algo ...

  10. ajax & jsonp & img

    ajax 是一种请求服务器的方式,核心是XMLHttpRequest对象: 优点是无需刷新页面, 缺点是不能跨域请求. /* * Ajax direacted by Zakas * * Ajax.ge ...