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 ...
随机推荐
- window下安装cross-env解决NODE_ENV ts-node 不是内部或外部命令,也不是可运行的程序 或批处理文件 问题
window下安装cross-env解决NODE_ENV ts-node 不是内部或外部命令,也不是可运行的程序 或批处理文件 问题 在git bash上启动无法进行调试,采用cross-env后可以 ...
- 51Nod 2020 排序相减
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=2020 思路:排序 水水 #include<iostre ...
- .NET 常用ORM之SubSonic
一.SubSonic简单介绍 SubSonic是一个类似Rails的开源.NET项目.你可以把它看作是一把瑞士军刀,它可以用来构建Website和通过ORM方式来访问数据.Rob Conery和Eri ...
- OSI七层详解
OSI 七层模型通过七个层次化的结构模型使不同的系统不同的网络之间实现可靠的通讯,因此其最主要的功能就是帮助不同类型的主机实现数据传输 . 完成中继功能的节点通常称为中继系统.在OSI七层模型中,处于 ...
- 金九银十跳槽季,程序员面试点解析之Java专场
前言 近年来Java工程师这个岗位炙手可热,市场需求大,学习Java的人也越来越多,所以IT企业与求职者的选择都比较多,那么IT企业在面试时都会提哪些问题呢.下面为大家分享 Java高级工程师面试阿里 ...
- SSM整合Mybatis-Spring
mybatis -Spring 整合 cn.kitty.bean public class Book { private int bookid; private String bookname; pr ...
- show processlist命令与kill 线程
show [full] processlist show processlist显示正在运行的线程.如果有process权限,则可以查看所有正在运行的线程.否则,只能看到自己的线程.如果不使用full ...
- collectd 检测cpu使用率
环境配置 1. install epel https://www.cyberciti.biz/faq/installing-rhel-epel-repo-on-centos-redhat-7-x/ 2 ...
- MS11-050安全漏洞
IE浏览器渗透攻击--MS11050安全漏洞 实验前准备 1.两台虚拟机,其中一台为kali,一台为windows xp sp3(包含IE7). 2.设置虚拟机网络为NAT模式,保证两台虚拟机可以相互 ...
- 主动攻击:利用ms08_067_netapi进行攻击
利用ms09_053_wins进行攻击 ms08_067漏洞 如果用户在受影响的系统上收到特制的 RPC 请求,则该漏洞可能允许远程执行代码. 在 Microsoft Windows 2000.Win ...