题目,要求找出有多少对这样的东西,四个数,并且满足num[a]<num[b] &&num[c]>num[d]

要做这题,首先要懂得用树状数组,我设,下面的小于和大于都是严格的小于和大于

dpL_min[i]:表示在第i个数往左,(不包括第i个),有多少个数是少于num[i]的

dpL_max[i]:表示在第i个数往左,(不包括第i个),有多少个数是大于num[i]的

dpR_min[i]:表示在第i个数往右,(不包括第i个),有多少个数是小于num[i]的

dpR_max[i]:表示在第i个数往右,(不包括第i个),有多少个数是大于num[i]的

首先我们能预处理出所有的sumab对数,表示在1--n中有多少对这样的ab对。

sumcd同理。然后默认的ans=sumab*sumcd了

但是有重复的呀。有四种情况是重复的,就是a==d || a==c || b==c || a==c

那么,我们枚举每一个i,表示当前是a==d=num[i],就是把a和d现在相同,且数字是num[i],那么要减去的值就是dpR_max[i]*dpL_max[i];  dpR_max[i]表示有多少个数能和num[a]组合,变成num[a]<num[b]的对数。同理dpL_max[i]

再来一个例子吧.。假如现在是a==c=num[i],那么ans -= dpR_max[i] * dpR_min[i];   dpR_max[i] 表示有多少个数能和num[a]组合,变成num[a]<num[b]的对数。dpR_min[i]表示有多少个数能和num[c]结合,变成num[c]>num[d]这样的对数。

这里和网上说的枚举i作为d值是不同的哦,网上的解释我感觉上是说不通的。反正我是想不明白。我这里的枚举每个i,作为他们相同的数子。

有没可能是a==c && b==d呢?可能的,矛盾了。

这里要注意的还有数字是严格大于,在离散的时候注意一下就可以了

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
int n;
const int maxn = + ;
struct data
{
int val,pos;
}book[maxn];
int a[maxn];
int c[maxn];//树状数组
int lowbit (int x)//得到x二进制末尾0的个数的2次方 2^num
{
return x&(-x);
}
void add (int pos,int val)//在第pos位加上val这个值
{
while (pos<=n) //n是元素的个数
{
c[pos] += val;
pos += lowbit(pos);
}
return ;
}
int get_sum (int pos) //求解:1--pos的总和
{
int ans = ;
while (pos)
{
ans += c[pos];
pos -= lowbit(pos);
}
return ans;
}
bool cmp (struct data a,struct data b)
{
return a.val < b.val;
}
int dpL_min[maxn];
int dpL_max[maxn];
int dpR_min[maxn];
int dpR_max[maxn];
void init ()
{
memset(c,,sizeof c);
memset(dpR_max,,sizeof dpR_max);
memset(dpR_min,,sizeof dpR_min);
memset(dpL_max,,sizeof dpL_max);
memset(dpL_min,,sizeof dpL_min);
}
void work ()
{
init();
for (int i=;i<=n;++i)
{
scanf("%d",&book[i].val);
book[i].pos = i;
}
sort(book+,book++n,cmp);
for (int i=;i<=n;++i)
{
if (i>= && book[i].val == book[i-].val) a[book[i].pos] = a[book[i-].pos];
else a[book[i].pos]=i; //从小到大离散
}
for (int i=;i<=n;++i)
{
dpL_min[i] = get_sum(a[i]-);
dpL_max[i] = get_sum(n)-get_sum(a[i]);
add(a[i],);
}
memset(c,,sizeof c);
for (int i=n;i>=;--i)
{
dpR_min[i] = get_sum(a[i]-);
dpR_max[i] = get_sum(n) - get_sum(a[i]);
add(a[i],);
} LL sumab = ;
LL sumcd = ;
for (int i=;i<=n;++i) sumab += dpL_min[i];
for (int i=;i<=n;++i) sumcd += dpL_max[i];
LL ans = sumab * sumcd; for (int i=;i<=n;++i)
{
ans -= dpL_max[i] * dpR_max[i]; //a==d
ans -= dpL_max[i] * dpL_min[i]; // b==d
ans -= dpR_max[i] * dpR_min[i]; // a==c;
ans -= dpL_min[i] * dpR_min[i]; //c==b
}
printf ("%I64d\n",ans);
return ;
}
int main()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
while(scanf("%d",&n)!=EOF && n) work();
return ;
}

HDU 5792 L - World is Exploding 。容斥原理 + 树状数组 + 离散化的更多相关文章

  1. HDU 5792:World is Exploding(树状数组求逆序对)

    http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Problem Description   Given a sequ ...

  2. HDU 5792 World is Exploding(树状数组+离散化)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5792 题意: 思路: lmin[i]:表示左边比第i个数小的个数. lmax[i]:表示左边比第i个 ...

  3. HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences             ...

  4. hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)

    hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...

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

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

  6. BZOJ_5055_膜法师_树状数组+离散化

    BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...

  7. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  8. HDU 5792 World is Exploding (树状数组)

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

  9. HDU 4777 Rabbit Kingdom --容斥原理+树状数组

    题意: 给一个数的序列,询问一些区间,问区间内与区间其他所有的数都互质的数有多少个. 解法: 直接搞有点难, 所谓正难则反,我们求区间内与其他随便某个数不互质的数有多少个,然后区间长度减去它就是答案了 ...

随机推荐

  1. bzoj 2535 & bzoj 2109 航空管制 —— 贪心+拓扑序

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2535 https://www.lydsy.com/JudgeOnline/problem.p ...

  2. Android 开发:开源库Speex支持arm64的动态库文件

    随着处理器制造工艺的不断进步,和Android系统的不断发展,最近出了arm64-v8a的架构,由于项目中用到了speex的第三方语音编解码的动态库,其他架构的处理器暂不用说,一切正常,唯独到arm6 ...

  3. 关于系统中:/dev/mem

    1)参考:https://blog.csdn.net/lsn946803746/article/details/52948036   博主:lsn946803746 2)参考:https://blog ...

  4. MAXOS安装FFMPEG

    安装brew 安装方法:命令行输入 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/ins ...

  5. Project Online JS 添加Ribbon按钮

    var Projects = Projects || {}; (function () { Projects.ribbonButtonClick = function (name) { var pro ...

  6. JasperReports项目中的应用

    转自:http://www.blogjava.net/vjame/archive/2013/10/12/404908.html . 2.业务处理 //返回报表查询结果 List<ReportEl ...

  7. Mysql 数据库时间更新字段

    关于时间更新: 创建时间: CURRENT_TIMESTAMP 更新时间: 勾选根据时间戳更新

  8. Tomcat 如何部署多个应用

    Tomcat 如何部署多个应用 https://blog.csdn.net/tdcqfyl/article/details/51966387

  9. IDEA如何启动debug?

    选择remote,然后,修改host和port: host是我们访问backstop的ip,端口是监听端口8787,点击ok即可. 打断点,调试,即可.

  10. JavaScript中的构造函数和工厂函数说明

    在cnblog上看到一篇文章,讲解JS中的构造函数和工厂函数,觉得讲的真好 JavaScript中的工厂函数和构造函数都能用来创建一个对象,我们可以来看看下面的例子 构造函数 function cre ...