HDU 5792 World is Exploding(树状数组+离散化)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5792
题意:
思路:
lmin[i]:表示左边比第i个数小的个数。
lmax[i]:表示左边比第i个数大的个数。
rmin[i]:表示右边比第i个数小的个数。
rmax[i]:表示右边比第i个数大的个数。
这些都是可以用树状数组计算出来的,把所有的lmin加起来就是所有(a,b)对的个数,所有lmax加起来就是所有(c,d)对的个数,两者相乘就是所有情况之和了。但是需要注意的是,在这些情况中还存在a=c,a=d,b=c,b=d这四种不符合题意的,需要把这些给减掉。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = +; int n;
int a[maxn],b[maxn],c[maxn];
int lmin[maxn],lmax[maxn],rmin[maxn],rmax[maxn]; int lowbit(int x)
{
return x&-x;
} int get_sum(int x)
{
int ret = ;
while(x>)
{
ret+=c[x];
x-=lowbit(x);
}
return ret;
} void add(int x)
{
while(x<=n)
{
c[x]+=;
x+=lowbit(x);
}
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b+n+);
int num=unique(b+,b+n+)-(b+);
for(int i=;i<=n;i++)
a[i]=lower_bound(b+,b+num+,a[i])-(b+)+; ll suml=,sumr=;
memset(c,,sizeof(c));
for(int i=;i<=n;i++)
{
lmin[i]=get_sum(a[i]-);
lmax[i]=get_sum(n)-get_sum(a[i]);
add(a[i]);
suml+=lmin[i];
sumr+=lmax[i];
}
memset(c,,sizeof(c));
for(int i=n;i>=;i--)
{
rmin[i]=get_sum(a[i]-);
rmax[i]=get_sum(n)-get_sum(a[i]);
add(a[i]);
} ll ans=suml*sumr; for(int i=;i<=n;i++)
{
ans-=(ll)rmin[i]*rmax[i];//a==c==a[i]
ans-=(ll)lmin[i]*lmax[i];//b==d==a[i]
ans-=(ll)lmin[i]*rmin[i];//b==c==a[i]
ans-=(ll)lmax[i]*rmax[i];//a==d==a[i]
}
printf("%lld\n",ans);
}
return ;
}
HDU 5792 World is Exploding(树状数组+离散化)的更多相关文章
- hdu 5792 World is Exploding 树状数组+离散化+容斥
World is Exploding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 5792 World is Exploding 树状数组+枚举
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Time Limit: 2000/1000 MS (Ja ...
- hdu 5792 World is Exploding 树状数组
World is Exploding 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...
- HDU 5256 - 序列变换 ,树状数组+离散化 ,二分法
Problem Description 我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个元素都必须是整数.请输出最少需要修改多少 ...
- 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≤ ...
- hdu 4911 Inversion and poj2299 [树状数组+离散化]
题目 题意: 给你一串数字,然后给你最多进行k次交换(只能交换相邻的)问交换后的最小逆序对个数是多少. 给你一个序列,每次只能交换相邻的位置,把他交换成一个递增序列所需要的最少步数 等于 整个序列的 ...
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- hdu4605 树状数组+离散化+dfs
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- BZOJ_5055_膜法师_树状数组+离散化
BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
随机推荐
- Codeforce 513A - Game
Two players play a simple game. Each player is provided with a box with balls. First player's box co ...
- netperf 网络性能测试
Netperf是一种网络性能的测量工具,主要针对基于TCP或UDP的传输.Netperf根据应用的不同,可以进行不同模式的网络性能测试,即批量数据传输(bulk data transfer)模式和请求 ...
- 大数据学习路线分享-Hbase shell的基本操作完整流程
HBase的命令行工具,最简单的接口,适合HBase管理使用,可以使用shell命令来查询HBase中数据的详细情况.安装完HBase之后,启动hadoop集群(利用hdfs存储),启动zookeep ...
- 在Linux 中如何从进程相关的文件描述中恢复数据
在Linux中误删除了某个文件,但是 ps-ef|grep 文件名 发现某个进程还在使用该文件,那么可以通 过以下方式恢复文件. 例如:创建一个简单文件/tmp/test.txt, 随便向里面写点内容 ...
- Q_DECL_OVERRIDE
Q_DECL_OVERRIDE也就是c++的override # define Q_DECL_OVERRIDE override 在重写虚函数时会用到, 作用是防止写错虚函数: void keyPre ...
- python 3.7 方向键乱码
原因是缺少安装包libreadline-dev 第一步安装libreadline-dev包:sudo apt-get install libreadline-dev(centos安装:yum -y i ...
- How to Get Text inside a Canvas using Webdriver or Protractor
https://stackoverflow.com/questions/43609429/how-to-get-text-inside-a-canvas-using-webdriver-or-prot ...
- Introduction to the Standard Directory Layout
Having a common directory layout would allow for users familiar with one Maven project to immediatel ...
- fjwc2019 D2T1 直径 (构造)
#181. 「2019冬令营提高组」直径 (这题构造题是我考场上唯一一A的题........) 先看这个特殊的Subtask4 Subtask 4(20pts):$\sqrt{1+8k}$ 为整 ...
- bzoj4861 / P3715 [BJOI2017]魔法咒语
P3715 [BJOI2017]魔法咒语 AC自动机+dp+矩阵乘法 常规思路是按基本串建立AC自动机 然鹅这题是按禁忌串建立AC自动机 对后缀是禁忌的点以及它的失配点做上标记$(a[i].ed)$, ...