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. React-用ImmutableJS提高性能

    一.需求 1.子组件有更新时,只重新渲染有变化的子组件,而不是全部 二.ImmutableJS原理 三.代码 1.CheckboxWithLabel.jsx var React = require(' ...

  2. NPOI操作EXCEL----------NPOI基础01

    来源地址:http://www.cnblogs.com/csqb-511612371/p/4878059.html 先来介绍一下NPOI基本的东西: 1.下载地址:http://npoi.codepl ...

  3. C++:派生类的构造函数和析构函数

    4.2 派生类的构造函数和析构函数4.2.1 派生类构造函数和析构函数的执行顺序 通常情况下,当创建派生类对象时,首先执行基类的构造函数,随后再执行派生类的构造函数:当撤销派生类对象时,则先执行派生类 ...

  4. [HDOJ2818]Building Block(带权并查集,路径压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2818 题意:有N个块,每次有两个操作: M x y表示把x所在的那一堆全部移到y所在的那一堆的下方. ...

  5. [51NOD1105]第k大的数(二分答案)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1105 先排序,二分上下界分别是最小的两个数和最大的两个数的乘积 ...

  6. jquery在线教程

    http://www.runoob.com/jquery/jquery-slide.htmlhttp://www.w3school.com.cn/jquery/http://www.phpstudy. ...

  7. MyEclipse中使用JUnit进行单元测试

    1. 下载JUnit的jar文件,下载地址在这里 2. 在MyEclipse中新建一个要测试的项目HelloJUnit 3. 添加一个要测试的类HelloJUnit,代码如下,注意需要先建packag ...

  8. poj 3101 Astronomy (java 分数的最小公倍数 gcd)

    题目链接 要用大数,看了别人的博客,用java写的. 题意:求n个运动周期不完全相同的天体在一条直线上的周期. 分析:两个星球周期为a,b.则相差半周的长度为a*b/(2*abs(a-b)),对于n个 ...

  9. Android远程图片获取和本地缓存

    对于客户端——服务器端应用,从远程获取图片算是经常要用的一个功能,而图片资源往往会消耗比较大的流量,对 应用来说,如果处理不好这个问题,那会让用户很崩溃,不知不觉手机流量就用完了,等用户发现是你的应用 ...

  10. HDU 4893 线段树

    比赛时太大意,斐波拉契数列开小了. 题目大意:1个序列,3种操作,改变序列某个数大小,将序列中连续的一段每个数都变成其最近的斐波拉契数,以及查询序列中某一段的数之和. 解题思路:维护add[]数组表示 ...