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. Java并发:Callable、Future和FutureTask

    Java并发编程:Callable.Future和FutureTask 在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一 ...

  2. Visual StudioTools for Unity 使用技巧2

    在之前的博客介绍了 Visual Studio Tools for Unity的安装和使用. http://www.cnblogs.com/petto/p/3886811.html 其实这个工具还提供 ...

  3. CString, QString, char*之间的转换(包括VC编译开关)

    传给未分配内存的const char* (LPCTSTR)指针. CString cstr(asdd); const char* ch = (LPCTSTR)cstr; ch指向的地址和cstr相同. ...

  4. Android:PopupWindow简单弹窗

    两布局,一个当前布局页面和一个点击展示布局页面,主程序代码: public class MainActivity extends Activity { private PopupWindow pop; ...

  5. linux shell 命令学习(1) du- estimate file space usage

    du - estimate file space usage , 计算文件的磁盘大小 语法格式: du [OPTION] ... [FILE] 描述: 汇总每个文件的磁盘大小, 递归汇总目录的大小, ...

  6. 机器学习 —— 概率图模型(Homework: Factors)

    Talk is cheap, I show you the code 第一章的作业主要是关于PGM的因子操作.实际上,因子是整个概率图的核心.对于有向图而言,因子对应的是CPD(条件分布):对无向图而 ...

  7. BZOJ 2004 Bus 公交线路(矩阵)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2004 题意:小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依 ...

  8. javadoc 和 javadoc注释规范

    javadoc是Sun公司提供的一个技术,它从程序源代码中抽取类.方法.成员等注释形成一个和源代码配套的API帮助文档. javadoc命令是用来生成自己API文档的,使用方式:在dos中在目标文件所 ...

  9. [POJ2377]Bad Cowtractors(最大生成树,Kruskal)

    题目链接:http://poj.org/problem?id=2377 于是就找了一道最大生成树的AC了一下,注意不连通的情况啊,WA了一次. /* ━━━━━┒ギリギリ♂ eye! ┓┏┓┏┓┃キリ ...

  10. Web内容管理系统 Magnolia 启程-挖掘优良的架构(3)

    Author and Public instances 第一个关键观念:instance-实例.每一个项目都必须至少有一个Author实例和至少一个Public实例.下面将告诉你为什么: 基本概念:J ...