2016 Multi-University Training Contest 5 1012 World is Exploding 树状数组+离线化

时间:2022-09-08 20:18:41

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&lpar;GCD离散&plus;树状数组&rpar;

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

  2. 【BZOJ】1012&colon; &lbrack;JSOI2008&rsqb;最大数maxnumber 树状数组求区间最值

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

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

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

  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 &lpar;SWERC 2016&rpar; F dfs序&plus;树状数组

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

  6. 【BZOJ】1012&colon; &lbrack;JSOI2008&rsqb;最大数maxnumber(树状数组&plus;rmq)

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

  7. Codeforces Round &num;348 &lpar;VK Cup 2016 Round 2&comma; Div&period; 2 Edition&rpar; E&period; 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 &lpar;Div&period; 2 Edition&rpar; B&period; 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 &lpar;Div&period; 2 Edition&rpar; D&period; Factory Repairs 树状数组

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

随机推荐

  1. 数据处理之PostgreSQL过程语言学习

    前段时间,公司更换新的PostgreSQL数据集市的系统过程中,自己下载了postgresqlAPI的pdf文件研究了一下PostgreSQL数据集市.发现使用PostgreSQL过程语言可以大大加快 ...

  2. 用 windows GDI 实现软光栅化渲染器--gdi3d(开源)

    尝试用windows GDI实现了一个简单的软光栅化渲染器,把OpenGL渲染管线实现了一遍,还是挺有收获的,搞清了以前一些似是而非的疑惑. ----更新2015-10-16代码已上传.gihub地址 ...

  3. IE6中常见兼容性问题及浏览器显示难题

    1.双倍边距Bug 问题描述:假如有一个ul,里面有若干li,当li设置为左浮动时,此时设置li的margin-left为10px,会在最左侧呈现双倍情况.即20px 正常显示: IE6显示: 修正方 ...

  4. 微信JSSDK与录音相关的坑

    欢迎各位转载, 以让微信团队重视这些恼人的BUG. 请注明出处微信JSSDK与录音相关的坑 by lzl124631x 最近一直在做微信JSSDK与录音相关的功能开发, 遇到了各种奇尺大坑, 时不时冷 ...

  5. ASP&period;NET基础之HttpHandler学习

    ASP.NET基础之HttpHandler学习 经过前两篇[ASP.NET基础之HttpModule学习]和[ASP.NET基础之HttpContext学习]文章的学习我们对ASP.NET的基础内容有 ...

  6. ZOJ&Tab;1967 POJ 2570 Fiber Network

    枚举起点和公司,每次用DFS跑一遍图,预处理出所有的答案.询问的时候很快就能得到答案. #include<cstdio> #include<cmath> #include&lt ...

  7. 芝麻HTTP:Python爬虫进阶之Scrapy框架安装配置

    初级的爬虫我们利用urllib和urllib2库以及正则表达式就可以完成了,不过还有更加强大的工具,爬虫框架Scrapy,这安装过程也是煞费苦心哪,在此整理如下. Windows 平台: 我的系统是 ...

  8. &period;NET MD5 加密

    using System; using System.Security.Cryptography; using System.Text; namespace Md5Demo { /// <sum ...

  9. JQurey 添加和删除元素

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> ...

  10. 【Linux】linux正则表达式及通配符

    正则表达式就是用于匹配每行输入的一种模式,模式是指一串字符序列.拥有强大的字符搜索功能.也非常方便的搜索过滤出我们想要的内容. linux正则表达式分为基本正则表达式(Basic Regexp)和扩展 ...