题目链接:

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. dedecms后台上传图片附件返回302的问题

    在网上查了资料,验证可行后,在这儿做个备份! 自己解决了,貌似是swfupload在linux环境下session丢失的引起的 解决办法是:在include/userlogin.class.php文件 ...

  2. C#局域网桌面共享软件制作(二)

    链接C#局域网桌面共享软件制作(一) 如果你运行这个软件查看流量监控就会发现1~2M/s左右的上传下载,并且有时会报错“参数无效”,如果你将屏幕截图保存到本地的话每张图片大概4M(bmp).120KB ...

  3. mutex 简单介绍

    “mutex”是术语“互相排斥(mutually exclusive)”的简写形式,也就是互斥量. 当两个或更多线程需要同时访问一个共享资源时,系统需要使用同步机制来确保一次只有一个线程使用该资源.M ...

  4. Spark提交任务到集群

    提交Spark程序到集群与提交MapReduce程序到集群一样,首先要将写好的Spark程序打成jar包,再在Spark-submit下通过命令提交. Step1:打包程序 Intellij IDEA ...

  5. 10)Java Error and Exception

      1>异常继承类        Error类和Exception类都继续自Throwable类      Error表示系统级的错误情况,如内存错误这样程序无法通过自身的处理再继续执行下去的情 ...

  6. 重定向语句Response.Redirect()方法与Response.RedirectPermanent()对搜索引擎页面排名的影响

    在ASP.NET中,开发人员经常使用Response.Redirect()方法,用编程的手法,将对老的URL的请求转到新的URL上.但许多开发人员没有意识到的是,Response.Redirect() ...

  7. STM32F0xx_FLASH编程(片内)配置详细过程

    Ⅰ.概述 关于数据的储存,我觉得编程的人基本上都会使用到,只是看你储存在哪里.STM32的芯片内部FLASH都是可以进行编程的,也就是说可以拿来储存数据.但是,很多做一些小应用程序开发的人都没有利用好 ...

  8. Python-Day12 Python mysql and ORM

    一.Mysql数据库 1.什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,    每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据 ...

  9. Python学习基础教程(learning Python)--2.2.1 Python下的变量解析

    前文提及过变量代表内存里的某个数据,这个说法有根据么? 这里我们介绍一个python内建(built-in)函数id.我们先看看id函数的帮助文档吧.在python查某个函数的帮助文档很简单,只用he ...

  10. PHP 学习笔记 01

    例子: 为什么要学PHP 主观原因: 前段时间在学校处理了毕业的一些事情,回到上海后开始了找工作的旅程.意向工作是WPF开发或者ASP.NET 作为后端的WEB开发. 陆陆续续一直在面试,其中有一家公 ...