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. easyswoole报错:failed: Error during WebSocket handshake: Unexpected response code: 200

    WebSocket connection to 'ws://www.xxxx.com/xxx/xx' failed: Error during WebSocket handshake: Unexpec ...

  2. Python—None

    None是一个特殊的常量. None不是False. None不是0. None不是空字符串. None有自己的数据类型NoneType,并且是NoneType中唯一的值. None只是一个空值的对象 ...

  3. linux内核钩子--khook

    简介 本文介绍github上的一个项目khook,一个可以在内核中增加钩子函数的框架,支持x86.项目地址在这里:https://github.com/milabs/khook 本文先简单介绍钩子函数 ...

  4. springboot实现上传并解析Excel

    添加pom依赖 <!-- excel解析包 --> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> ...

  5. tomcat日志报Invalid message received with signature的解决办法

    这位大神说的是正解,大家可以参考一下这个链接:https://blog.csdn.net/bugdongwenlong/article/details/84261424

  6. java——包装类数据缓存 ==号详解

    Java对部分经常使用的数据采用缓存技术,即第一次使用该数据则创建该数据对象并对其进行缓存, 当再次使用等值对象时直接从缓存中获取,从而提高了程序执行性能.(只对常用数据进行缓存) Java中只是对部 ...

  7. O048、掌握 cinder-scheduler 调度逻辑

    参考https://www.cnblogs.com/CloudMan6/p/5589707.html   上一节我们详细讨论了cinder-api 和 cinder-volume ,今天讨论另一个重要 ...

  8. element-ui中点击菜单,改变当前菜单背景颜色

    需求: vue项目中,点击左侧菜单,tags页显示当前打开的菜单,并且高亮显示当前菜单 实现效果: 实现代码:在vuex里面定义tags存放所有打开的菜单,和当前打开的索引curtagsIndex:, ...

  9. Fescar锁和隔离级别的理解

    前几天夜里,我老大发我一篇文章说阿里的GTS开源了. 因为一直对分布式事务比较感兴趣,立马pull了代码,进行阅读.基本的原理,实现方案我就不一一细化了,详细见官方文档(写的很棒,点赞). 在fesc ...

  10. 使用docker compose部署服务

    谈到微服务的话题,技术上我们往往会涉及到多服务.多容器的部署与管理. Docker 有三个主要的作用:Build, Ship和Run.使用docker compose我们可以在Run的层面解决很多实际 ...