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 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...
随机推荐
- jQuery bind and unbind (绑定和解除)
测试:页面代码: <body> <input type="button" name="aaa" value="点击我"&g ...
- POJ C程序设计进阶 编程题#2: 配对碱基链
编程题#2: 配对碱基链 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 脱 ...
- Ajax与Json的一些总结
Ajax与Json AJAX=异步javaScript 和XML AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新. 这意味着可以在不 ...
- mac里边配置android开发环境,intellij开发工具:
1 在android的官网下载 android sdk的mac版 http://developer.android.com/sdk/index.html 选择mac的版本 下载后打开sdk-mana ...
- Php中正则小结(一)
一.概念 语法模式类似perl.表达式必须用分隔符闭合,比如一个正斜杠(/). 分隔符可以是任意非字母非数字,除反斜杠(\)和空字节之外的非空白ascii字符 如果分隔符 在表达式中使用,需要使用反斜 ...
- delphi构造&析构调用顺序
_ClassCreate ->Create ->AfterConstruction(->DoCreate / OnCreate) BeforeDestruction(->DoD ...
- FileUpload控件「批次上传 / 多档案同时上传」的范例--以「流水号」产生「变量名称」
原文出處 http://www.dotblogs.com.tw/mis2000lab/archive/2013/08/19/multiple_fileupload_asp_net_20130819. ...
- Oracle中Clob类型处理解析:ORA-01461:仅可以插入LONG列的LONG值赋值
感谢原作者:破剑冰-Oracle中Clob类型处理解析 上一篇分析:ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值 最近为Clob字段在插入数据时发现当字符的字节数(一个半角字符一 ...
- 在openSUSE13.2上gem install rails -v 4.1成功,但是之后不存在rails命令解决
解决方案为,不要用sudo gem install就好了,卧槽
- <转载>编程珠玑-位排序(bitsort)
转载:http://www.cnblogs.com/shuaiwhu/archive/2011/05/29/2065039.html 维护版权 在<编程珠玑>一书上,有一题是将一堆不 ...