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. 大数据和hadoop有什么关系?

    本文资料来自百度文库相关文档 Hadoop,Spark和Storm是目前最重要的三大分布式计算系统,Hadoop常用于离线的复杂的大数据处理,Spark常用于离线的快速的大数据处理,而Storm常用于 ...

  2. usdt钱包开发,比特币协议 Omni 层协议 USDT

    usdt钱包开发 比特币协议 -> Omni 层协议 -> USDT USDT是基于比特币omni协议的一种代币: https://omniexplorer.info/asset/31 I ...

  3. 常见的原生javascript DOM操作

    1.创建元素 创建元素:document.createElement() 使用document.createElement()可以创建新元素.这个方法只接受一个参数,即要创建元素的标签名.这个标签名在 ...

  4. <转>jmeter(十六)配置元件之计数器

    本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...

  5. scss简单用法

  6. JavaUtil smtp 邮件发送

    需要用到的jar包:javax.mail.jar package com.lee.util; import java.io.UnsupportedEncodingException; import j ...

  7. SpringMVC中参数接收

    /** *  * SpringMVC中参数接收 * 1.接收简单类型 int String * 2.可以使用对象pojo接收 * 3.可以使用集合数据接收参数 * 页面: name="ids ...

  8. 怎样从外网访问内网WebSphere?

    本地安装了一个WebSphere,只能在局域网内访问,怎样从外网也能访问到本地的WebSphere呢?本文将介绍具体的实现步骤. 准备工作 安装并启动WebSphere 默认安装的WebSphere端 ...

  9. Makefile依赖关系中的竖线“|”

    网上搜索无果,于是自己查看了一下makefile的info文件,其中解释如下: [java] view plain copy print? target : prerequisites   [TAB] ...

  10. 最简单的uwsgi+nginx配置多个django站点

    1. nginx.conf http{ server { listen       80; server_name  www.web1.com ....... location / { uwsgi_p ...