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(树状数组+离散化)的更多相关文章

  1. hdu 5792 World is Exploding 树状数组+离散化+容斥

    World is Exploding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

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

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

  3. hdu 5792 World is Exploding 树状数组

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

  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. hdu 4911 Inversion and poj2299 [树状数组+离散化]

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

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

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

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

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

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

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

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

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

随机推荐

  1. locust 的使用

    Contents Locust这一款开源性能测试工具.然而,当前在网络上针对Locust的教程极少,不管是中文还是英文,基本都是介绍安装方法和简单的测试案例演示,但对于较复杂测试场景的案例演示却基本没 ...

  2. 利用sqoop将hive数据导入导出数据到mysql

    一.导入导出数据库常用命令语句 1)列出mysql数据库中的所有数据库命令  #  sqoop list-databases --connect jdbc:mysql://localhost:3306 ...

  3. c# 共享事件处理程序

    使用同一个方法来处理多个Button实例的Click事件. 1.全选所有的Button,在事件添加中的Click点击事件中添加处理函数. 2.假如一个label控件用于显示按钮按下输出文本 3.处理函 ...

  4. SpringMVC中的自定义参数绑定案例

    由于日期数据有很多种格式,所以springmvc没办法把字符串转换成日期类型.所以需要自定义参数绑定.前端控制器接收到请求后,找到注解形式的处理器适配器,对RequestMapping标记的方法进行适 ...

  5. 苹果手机显示分享链接的方法html页面

    function onBridgeReady(){ WeixinJSBridge.call('showOptionMenu'); } if (typeof WeixinJSBridge == &quo ...

  6. 学写网页 #06# table

    A B E C D <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> & ...

  7. ELK学习笔记之F5利用EELK进行应用数据挖掘系列(2)-DNS

    0x00 概述 很多客户使用GTM/DNS为企业业务提供动态智能解析,解决应用就近性访问.优选问题.对于已经实施多数据中心双活的客户,则会使用GSLB提供双活流量调度.DNS作为企业业务访问的指路者, ...

  8. python的paramiko模块-远程登录linux主机并操作

    paramiko是一个用于做远程控制的模块,使用该模块可以对远程服务器进行命令或文件操作. 如果python服务器对被远程控制机器开启了免密验证,即在python服务器上可通过ssh 用户名@被控制机 ...

  9. Python3 离线安装TensorFlow包

    Python3 离线安装TensorFlow包 1,下载包 官网地址:https://pypi.org/project/tensorflow/1.1.0rc2/#files 清华镜像:https:// ...

  10. oracle 11g AUTO_SAMPLE_SIZE动态采用工作机制

    Note that if you're interested in learning about Oracle Database 12c, there's an updated version of ...