World is Exploding

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 727    Accepted Submission(s): 348

Problem Description
Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a<b≤n,1≤c<d≤n,Aa<Ab,Ac>Ad.
 
Input
The input consists of multiple test cases. 
Each test case begin with an integer n in a single line.

The next line contains n integers A1,A2⋯An.
1≤n≤50000
0≤Ai≤1e9

 
Output
For each test case,output a line contains an integer.
 
Sample Input
4
2 4 1 3
4
1 2 3 4
 
Sample Output
1
0
 
Author
ZSTU
 
Source
 题意:给你n个数字,要求从中找到下标i<j,a[i]<a[j];p<q,a[p]>a[q]且i,j,p,q互不相等的组数;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x7f7f7f7f
#define FOR(i,n) for(int i=1;i<=n;i++)
#define CT continue;
#define PF printf
#define SC scanf
const int mod=1000000007;
const int N=1e6+10;
int tmp[N],a[N],n,m,c[N],pos[N];
ll lmin[N],rmin[N],lmax[N],rmax[N]; int lowbit(int i)
{
return i&(-i);
} void add(int x)
{
while(x<=m)
{
c[x]+=1;
x+=lowbit(x);
}
} int query(int x)
{
int res=0;
while(x>0)
{
res+=c[x];
x-=lowbit(x);
}
return res;
} int main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&tmp[i]);
a[i]=tmp[i];
} sort(tmp+1,tmp+n+1);
m=unique(tmp+1,tmp+n+1)-tmp-1; for(int i=1;i<=n;i++)
pos[i]=lower_bound(tmp+1,tmp+m+1,a[i])-tmp; MM(c,0);
for(int i=1;i<=n;i++)
{
lmin[i]=query(pos[i]-1);
lmax[i]=query(m)-query(pos[i]);
add(pos[i]);
} MM(c,0);
for(int i=n;i>=1;i--)
{
rmin[i]=query(pos[i]-1);
rmax[i]=query(m)-query(pos[i]);
add(pos[i]);
} ll ans=0,l=0,r=0;
for(int i=1;i<=n;i++) {r+=rmax[i],l+=lmax[i];};
ans=l*r;
for(int i=1;i<=n;i++)
{
ans-=lmin[i]*rmin[i];
ans-=lmax[i]*rmax[i];
ans-=lmax[i]*lmin[i];
ans-=rmax[i]*rmin[i];
}
printf("%lld\n",ans);
}
return 0;
}

  分析:比赛的时候又是只想到暴力枚举,结果发现肯定超时,复杂度又不会降下来,就跪了。。。

后来看了下题解,结果发现跟以前做的一道题很像。是以前做过的题的综合。

解决:既然超时了的话,就想想通过什么办法把复杂度降下来,,于是BIT,BIT可以在logn的时间内

求出每个点两侧>或<的数的个数;

BIT:因为只关心数据的相对大小,数据范围又很大,所以需要离散化

离散化:sort排好序后,用unique去重,然后对于原数组的每个数,利用lower_bound();找到其在去重

之后数组中的位置,记录下来,作为在BIT中的位置。

按原来的顺序枚举每个数,对于query(pos[i]-1)为查找在1-i中小于其的数的个数。query(m)-query(pos[i])为在1-i中>其的个数,因为是从小到大枚举所以都是l;然后将其在BIT中的对应位置的值+1,

然后从大到小枚举一下

hdu 5792 World is Exploding 树状数组+离散化+容斥的更多相关文章

  1. HDU 5792 World is Exploding 树状数组+枚举

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Time Limit: 2000/1000 MS (Ja ...

  2. hdu 5792 World is Exploding 树状数组

    World is Exploding 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...

  3. hdu 5792(树状数组,容斥) World is Exploding

    hdu 5792 要找的无非就是一个上升的仅有两个的序列和一个下降的仅有两个的序列,按照容斥的思想,肯定就是所有的上升的乘以所有的下降的,然后再减去重复的情况. 先用树状数组求出lx[i](在第 i ...

  4. HDU 5256 - 序列变换 ,树状数组+离散化 ,二分法

    Problem Description 我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个元素都必须是整数.请输出最少需要修改多少 ...

  5. World is Exploding 树状数组+离散化

    Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a<b≤n,1≤ ...

  6. Luogu4528 CTSC2008 图腾 树状数组、容斥

    传送门 设$f_i$表示$i$排列的数量,其中$x$表示不确定 那么$$ans=f_{1324}-f_{1432}-f_{1243}=(f_{1x2x}-f_{1423})-(f_{14xx}-f_{ ...

  7. hdu 4911 Inversion and poj2299 [树状数组+离散化]

    题目 题意:  给你一串数字,然后给你最多进行k次交换(只能交换相邻的)问交换后的最小逆序对个数是多少. 给你一个序列,每次只能交换相邻的位置,把他交换成一个递增序列所需要的最少步数 等于 整个序列的 ...

  8. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  9. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

随机推荐

  1. 怎样通过id属性快速从HTMLCollection对象中获取到目标元素节点

    方法1: 直接使用id或name属性: 比如我想获取 id 为 img1 的图片元素节点, 则可以这样写: document.images.img1 如果没有对应节点, 则返回undefined; 方 ...

  2. 【js】clientY pageY screenY layerY offsetY的区别

    clientY 指的是距离可视页面左上角的距离 pageY 指的是距离可视页面左上角的距离(不受页面滚动影响) screenY 指的是距离屏幕左上角的距离 layerY 指的是找到它或它父级元素中最近 ...

  3. QT编译Mysql驱动问题及解决方案

    默认情况下,qt 并没有自带mysql的数据库插件,需要自己编译先安装mysql server ,运行setup.exe时选择自定义安装,安装目录设为"D:\mysqldev"不要 ...

  4. ie8 下的半透明 background:rgba 与opacity失效 兼容办法

    fliter: filter: progid:DXImageTransform.Microsoft.Alpha(opacity=70);

  5. python 文本相似度计算

    参考:python文本相似度计算 原始语料格式:一个文件,一篇文章. #!/usr/bin/env python # -*- coding: UTF-8 -*- import jieba from g ...

  6. Linux系统目录结构和文件基本属性

    一.Linux系统目录结构 二.Linux 文件基本属性 三.touch stat tar 命令 一.Linux系统目录结构 不同颜色文件的含义: inux 文件颜色的含义,蓝色代表目录,绿色代表可执 ...

  7. ubuntu16.04环境LNMP实现PHP5.6和PHP7.2

    最近因为公司突然间说要升级php7,所以做个记录 PPA 方式安装 php7.2 : sudo apt-get install software-properties-common 添加 php7 的 ...

  8. Bmake

    Bmake is a common makefile framework. Both support native build and cross build. Easy for use, modif ...

  9. 第十一章·Filebeat-使用Filebeat收集日志

    Filebeat介绍及部署 Filebeat介绍 Filebeat附带预构建的模块,这些模块包含收集.解析.充实和可视化各种日志文件格式数据所需的配置,每个Filebeat模块由一个或多个文件集组成, ...

  10. 【转】草根老师的 linux字符设备驱动详解

    Linux 驱动 之 模块化编程 Linux 驱动之模块参数和符号导出 Linux 设备驱动之字符设备(一) Linux 设备驱动之字符设备(二) Linux 设备驱动之字符设备(三)