HDU 5792 World is Exploding 树状数组+枚举
题目链接:
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)
#### 问题描述
>
#### 输入
> 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 4sample 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 树状数组+枚举的更多相关文章
- hdu 5792 World is Exploding 树状数组
World is Exploding 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...
- hdu 5792 World is Exploding 树状数组+离散化+容斥
World is Exploding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- 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]< ...
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- HDU 5862 Counting Intersections (树状数组)
Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...
- 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 ...
- HDU 1394 Minimum Inversion Number (树状数组求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...
随机推荐
- Oracle登录时提示错误,导致用户无法登录
Oracle登录时提示错误,导致用户无法登录,错误如下 ------------------------------------------------------------------------ ...
- 认识php钩子-转白俊遥的博客
认识php钩子-转载白俊遥的博客 我们先来回顾下原本的开发流程:产品汪搞出了一堆需求:当用户注册成功后需要发送短信.发送邮件等等:然后聪明机智勇敢的程序猿们就一扑而上:把这些需求转换成代码扔在 用户注 ...
- mysql 修改字段长度
mysql 修改字段长度 alter table news modify column title varchar(130); alter table 表名 modify column 字段名 类型 ...
- Oracle存储过程学习备忘
之前的项目使用存储过程很少,但在实际的项目中,存储过程的使用是必不可少的. 存储过程是一组为了完成特定功能的SQL 语句 集,经编译后存储在数据库中:存储过程创建后,一次编译在程序中可以多次调用,对安 ...
- PHP 学习笔记 01
例子: 为什么要学PHP 主观原因: 前段时间在学校处理了毕业的一些事情,回到上海后开始了找工作的旅程.意向工作是WPF开发或者ASP.NET 作为后端的WEB开发. 陆陆续续一直在面试,其中有一家公 ...
- jQuery的筛选选择器
基本筛选选择器 很多时候我们不能直接通过基本选择器与层级选择器找到我们想要的元素,为此jQuery提供了一系列的筛选选择器用来更快捷的找到所需的DOM元素.筛选选择器很多都不是CSS的规范,而是jQu ...
- ListView的动态刷新问题——用notifyDataSetChanged没作用
也许很多开发的朋友,尤其是Android初学者(笔者也是个初学者),在动态刷新ListView时,使用notifyDataSetChanged并没有起到作用.有时会被困扰得很痛苦. 其实,在使用not ...
- .NET Async/Await 最佳实践
.NET 异步编程Guildlines 名称 描述 例外 Avoid async void Prefer async Task methods over async void methods Even ...
- OpenStack:安装Neutron与provider network
1. 安装(1)Install Networking services on a dedicated network node# apt-get install neutron-server neut ...
- C# Hadoop学习笔记
记录一下学习地址 http://www.360doc.com/content/14/0607/22/3218170_384675141.shtml