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. Socket称"套接字"

    Socket又称"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求. 二.利用Socket建立网络连接的步骤 建立Socket连接至少需要一对 ...

  2. Java多线程2:实现多线程的两种方式

    原文:http://www.cnblogs.com/skywang12345/p/3479063.html 常见的实现多线程的方式有2种,一是继承Thread类,二是实现 Runnable接口,还可以 ...

  3. 253. Meeting Rooms II

    题目: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] ...

  4. Android 在Intent中传递接口

    总结:在Activity中不能用intent传递匿名接口,原因如下:Activity A中生成了匿名接口M, 这个接口的引用就在组Activity A中,Activity A会禁止接口M 序列化.因为 ...

  5. TagHighlight

    :UpdateTypesFile 命令会生成相应的上色高亮文件(生成的着色文件可以叫types_c.taghl)还有相应的tags文件,寻找tags或taghl_config.txt确定project ...

  6. IS_ERR、PTR_ERR、ERR_PTR

    最近在使用filp_open打开文件时遇到到一个问题,当打开一个并不存在的文件时,filp_open返回值值为0xfffffffe,而并不是0(NULL),这是因为内核对返回指针的函数做了特殊处理.内 ...

  7. 三个Timer

    System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer 关于这三者的区别,可以参见msdn     https:// ...

  8. 百度HTTPS加密搜索有什么用?

    前段时间,我曾提到百度支持移动端HTTPS SSL加密搜索,用以保护用户隐私.最近,百度开始支持PC端HTTPS SSL加密搜索,现在可以启用 https://www.baidu.com 搜索.我很少 ...

  9. Ajax的“dataType”乱用的陷阱

    $.doAjax({ url : "areaAction_synchronizeArea.do", data : { 'vrvRangeUrl' : synAreaHTTP ,'v ...

  10. LA 4636 (贪心) Cubist Artwok

    题意: 一个由若干小正方体组成的图形,给出它的正视图和侧视图,求满足条件的最少小正方体的个数. 分析: 虽说是一道简单的贪心,可一直没有太好的思路. 又一次可耻地看了别人的题解. http://blo ...