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

1012 World is Exploding

题意:选四个数,满足a<b and A[a]<A[b]   c<d and A[c]>A[d] 问有几个这样的集合

思路:

树状数组+离线化

先处理出每个数左边比它小 大,右边比它大 小的数目,用cnt[][i]表示。最后统计一下减去重复的就可以

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long ll;
ll cnt[][];
ll cnt1[],cnt2[];
ll a[]; struct Node{
ll num,id;
}sub_a[]; ll maxn = ;
ll n;
ll tree[];
bool cmp(Node a, Node b) { //按照数字排序
return a.num < b.num;
}
void discrete() { //离散化
sort(sub_a+, sub_a++n, cmp);
a[sub_a[].id] = ;
maxn = ;
for(ll i = ; i <= n; i++) {
if(sub_a[i].num != sub_a[i-].num)
a[sub_a[i].id] = i;
else
a[sub_a[i].id] = a[sub_a[i-].id];
maxn = max(maxn,a[sub_a[i].id]);
}
}
void add(ll k){
while(k <= n){
tree[k] ++;
k += k & (- k);
}
}
ll read(ll k){
ll ans = ;
while(k){
ans += tree[k];
k -= k &(-k);
}
return ans;
} int main(){
while(scanf("%I64d",&n) != EOF)
{
memset(cnt1,,sizeof(cnt1));
memset(cnt2,,sizeof(cnt2));
for(ll i = ; i <= n; i ++){
scanf("%I64d",&sub_a[i].num);
sub_a[i].id = i;
}
discrete();
memset(tree,,sizeof(tree));
//在它右侧比它小的
for(ll i = n; i >= ; i--){ add(a[i]);
cnt[][i] = read(a[i] - );
}
//在它左侧比它小的
memset(tree,,sizeof(tree));
for(ll i = ; i <= n; i ++){
add(a[i]);
cnt[][i] = read(a[i] - );
a[i] = maxn + - a[i];
}
//在它右侧比它大的
memset(tree,,sizeof(tree));
for(ll i = n; i >= ; i --){ add(a[i]);
cnt[][i] = read(a[i] - );
}
//在它左侧比它大的
memset(tree,,sizeof(tree));
for(ll i = ; i <= n; i ++){
add(a[i]);
cnt[][i] = read(a[i] - );
a[i] = maxn + - a[i];
}
ll cnta = ,cntb = ;
for(ll i = ; i <= n; i ++){
cnt1[i] = cnt[][i] + cnt[][i];
cnta += cnt1[i];
cnt2[i] = cnt[][i] + cnt[][i];
cntb += cnt2[i];
}
cnta = cnta / ;
cntb = cntb / ;
long long ans = cnta * cntb;
for(ll i = ; i <= n; i ++){
ans -= cnt1[i] * cnt2[i];
}
printf("%I64d\n",ans);
}
return ;
}

2016 Multi-University Training Contest 5 1012 World is Exploding 树状数组+离线化的更多相关文章

  1. 2016 大连网赛---Different GCD Subarray Query(GCD离散+树状数组)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5869 Problem Description This is a simple probl ...

  2. 【BZOJ】1012: [JSOI2008]最大数maxnumber 树状数组求区间最值

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1012 题意:维护一个数列,开始时没有数值,之后会有两种操作, Q L :查询数列末 ...

  3. AtCoder Regular Contest 088 E - Papple Sort(树状数组+结论)

    结论:每次把字符丢到最外面最优,用树状数组统计答案,把字符放到最外边后可以当成消失了,直接在树状数组上删掉就好. 感性理解是把字符丢到中间会增加其他字符的移动次数,但是丢到外面不会,所以是正确的. # ...

  4. 2016 Al-Baath University Training Camp Contest-1

    2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...

  5. 2016-2017 ACM-ICPC Southwestern European Regional Programming Contest (SWERC 2016) F dfs序+树状数组

    Performance ReviewEmployee performance reviews are a necessary evil in any company. In a performance ...

  6. 【BZOJ】1012: [JSOI2008]最大数maxnumber(树状数组+rmq)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1012 树状数组原来我只懂得sum和add的操作,今天才知道可以有求区间最值的操作,我学习了一下写了个 ...

  7. Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) E. Little Artem and Time Machine 树状数组

    E. Little Artem and Time Machine 题目连接: http://www.codeforces.com/contest/669/problem/E Description L ...

  8. VK Cup 2016 - Round 1 (Div. 2 Edition) B. Bear and Displayed Friends 树状数组

    B. Bear and Displayed Friends 题目连接: http://www.codeforces.com/contest/658/problem/B Description Lima ...

  9. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) D. Factory Repairs 树状数组

    D. Factory Repairs 题目连接: http://www.codeforces.com/contest/635/problem/D Description A factory produ ...

随机推荐

  1. iOS视频录制、压缩导出、取帧

    概述 花了点时间研究了一下常用的视频获取.录制.压缩.取帧图功能,分享给大家了!相信阅读完本篇文章,会对你有很大的帮助的! 本篇文章研究几下以个功能: 视频录制 从相册选择视频 保持视频到相册 获取视 ...

  2. 360云盘、百度云、微云……为什么不出 OS X(Mac 端)应用呢?(用户少,开发成本高)(百度网盘Mac版2016.10.18横空出世)

    已经说的很好了,现有的云盘所谓的 OS X 版只有云同步功能,不过 115 是个例外,不过 115 的现状……不言自明.接下来说点和本题答案无关的,其实在官方客户端流氓 + 限速的大背景下 OS X ...

  3. jQuery编程基础精华01(jQuery简介,顶级对象$,jQuery对象、Dom对象,链式编程,选择器)

    jQuery简介 什么是jQuery? jQuery就是一个JavaScript函数库,没什么特别的.(开源)联想SQLHelper类 jQuery能做什么?jQuery是做什么的? jQuery本身 ...

  4. linux驱动学习之tasklet分析

    tasklet是中断处理下半部分最常用的一种方法,驱动程序一般先申请中断,在中断处理函数内完成中断上半部分的工作后调用tasklet.tasklet有如下特点: 1.tasklet只可以在一个CPU上 ...

  5. ActiveMQ 集群(1)

    Queue consumer clusters(消费者集群): 简介: 同一个queue,如果一个消费者失效, 那么任何未经确认的消息将会被发送给queue上的其它消费者.如果一个消费者比其它消费者执 ...

  6. C++STL 之排列

    固然我们可以自己使用递归编写全排列程序,但是既然STL里面已将有了这个功能为什么不直接用呢,下面就写一下直接使用C++ STL生成全排序的程序 函数名:next_permutation 包含头文件:a ...

  7. 带你走进EJB--那些跟EJB容器相关的那些Java概念

    最近在对EJB的相关内容进行总结,在总结的过程中发现对容器的概念并不是很理解,因为EJB本身就是一个容器,但是容器到底是用来做什么的?它跟我们之前所了解的组件,框架,包,类等都有什么关系?接下来主要是 ...

  8. oracle索引,索引的建立、修改、删除

    索引,索引的建立.修改.删除 2007-10-05 13:29 来源: 作者: 网友评论 0 条 浏览次数 2986 索引索引是关系数据库中用于存放每一条记录的一种对象,主要目的是加快数据的读取速度和 ...

  9. poj 2777 Count Color(线段树 区间更新)

    题目:http://poj.org/problem?id=2777 区间更新,比点更新多一点内容, 详见注释,  参考了一下别人的博客.... 参考博客:http://www.2cto.com/kf/ ...

  10. tornado中使用torndb,连接数过高的问题

    问题背景 最近新的产品开发中,使用了到了Tornado和mysql数据库.但在基本框架完成之后,我在开发时候发现了一个很奇怪的现象,我在测试时,发现数据库返回不了结果,于是我在mysql中输入show ...