hdu-5792 World is Exploding(容斥+树状数组)
题目链接:
World is Exploding
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Each test case begin with an integer n in a single line.
The next line contains n integers A1,A2⋯An.
1≤n≤50000
0≤Ai≤1e9
/************************************************
┆ ┏┓ ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃ ┃ ┆
┆┃ ━ ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃ ┃ ┆
┆┃ ┻ ┃ ┆
┆┗━┓ ┏━┛ ┆
┆ ┃ ┃ ┆
┆ ┃ ┗━━━┓ ┆
┆ ┃ AC代马 ┣┓┆
┆ ┃ ┏┛┆
┆ ┗┓┓┏━┳┓┏┛ ┆
┆ ┃┫┫ ┃┫┫ ┆
┆ ┗┻┛ ┗┻┛ ┆
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=5e4+10;
const int maxn=1e3+14;
const double eps=1e-8; int n,fa[N],pres[N],preb[N],nexs[N],nexb[N],sum[N];
struct node
{
int a,id;
}po[N];
int cmp(node x,node y)
{
if(x.a==y.a)return x.id<y.id;
return x.a<y.a;
}
int cmp1(node x,node y)
{
if(x.a==y.a)return x.id<y.id;
return x.a>y.a;
} int lowbit(int x){return x&(-x);} inline void update(int x)
{
while(x<=n)
{
sum[x]++;
x+=lowbit(x);
}
}
int query(int x)
{
int s=0;
while(x)
{
s+=sum[x];
x-=lowbit(x);
}
return s;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
For(i,1,n)read(po[i].a),po[i].id=i;
sort(po+1,po+n+1,cmp);
po[0].a=-1;
mst(sum,0);
For(i,1,n)
{
if(po[i].a==po[i-1].a)fa[i]=fa[i-1];
else fa[i]=i;
pres[po[i].id]=query(po[i].id)-(i-fa[i]);
nexs[po[i].id]=fa[i]-1-pres[po[i].id];
update(po[i].id);
}
mst(sum,0);
sort(po+1,po+n+1,cmp1);
For(i,1,n)
{
if(po[i].a==po[i-1].a)fa[i]=fa[i-1];
else fa[i]=i;
preb[po[i].id]=query(po[i].id)-(i-fa[i]);
nexb[po[i].id]=fa[i]-1-preb[po[i].id];
update(po[i].id);
}
sort(po+1,po+n+1,cmp1);
LL ans1=0,ans2=0,ans;
For(i,1,n)
{
ans1=ans1+pres[i];
ans2=ans2+preb[i];
}
ans=ans1*ans2;
For(i,1,n)
{
ans=ans-nexs[i]*nexb[i];//a==c
ans=ans-preb[i]*nexb[i];//a==d
ans=ans-pres[i]*nexs[i];//b==c
ans=ans-pres[i]*preb[i];//b==d
}
cout<<ans<<endl;
}
return 0;
}
hdu-5792 World is Exploding(容斥+树状数组)的更多相关文章
- bzoj4361:isn(dp+容斥+树状数组)
题面 darkbzoj 题解 \(g[i]\)表示长度为\(i\)的非降序列的个数 那么, \[ ans = \sum_{i=1}^{n}g[i]*(n-i)!-g[i+1]*(n-i-1)!*(i+ ...
- hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)
hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...
- HDU 5792 L - World is Exploding 。容斥原理 + 树状数组 + 离散化
题目,要求找出有多少对这样的东西,四个数,并且满足num[a]<num[b] &&num[c]>num[d] 要做这题,首先要懂得用树状数组,我设,下面的小于和大于都是严格 ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- HDU 5542 - The Battle of Chibi - [离散化+树状数组优化DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5542 Problem DescriptionCao Cao made up a big army an ...
- HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- Hdu 5458 Stability (LCA + 并查集 + 树状数组 + 缩点)
题目链接: Hdu 5458 Stability 题目描述: 给出一个还有环和重边的图G,对图G有两种操作: 1 u v, 删除u与v之间的一天边 (保证这个边一定存在) 2 u v, 查询u到v的路 ...
- HDU 5869 Different GCD Subarray Query 离线+树状数组
Different GCD Subarray Query Problem Description This is a simple problem. The teacher gives Bob a ...
- HDU 4267 A Simple Problem with Integers --树状数组
题意:给一个序列,操作1:给区间[a,b]中(i-a)%k==0的位置 i 的值都加上val 操作2:查询 i 位置的值 解法:树状数组记录更新值. 由 (i-a)%k == 0 得知 i%k == ...
随机推荐
- Visual Studio 2015官方下载 秘钥破解
微软刚刚为开发人员奉上了最新大礼Visual Studio 2015正式版.如果你是MSDN订阅用户,现在就可以去下载丰富的相关资源.如果你指向体验一把尝尝鲜,微软也是很慷慨的. Visual Stu ...
- 基于Repository模式设计项目架构—你可以参考的项目架构设计
关于Repository模式,直接百度查就可以了,其来源是<企业应用架构模式>.我们新建一个Infrastructure文件夹,这里就是基础设施部分,EF Core的上下文类以及Repos ...
- 17:不重复整数提取NoRepeatNum
题目描述 输入一个int型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数. 输入描述:输入一个int型整数 输出描述:按照从右向左的阅读顺序,返回一个不含重复数字的新的整数 输入例子: ...
- 双向数据绑定---AngularJS的基本原理学习
Angular JS (Angular.JS) 是一组用来开发Web页面的框架.模板以及数据绑定和丰富UI组件.它支持整个开发进程,提供web应用的架构,无需进行手工DOM操作. AngularJS非 ...
- SQL注入基础入门
一般的WEB架构 SQL注入成因: 用户开启浏览器并连接http://www.xxx.com.位于逻辑层的Web服务器从文件系统中加载脚本将其传递给脚本引擎,脚本引擎负责解析并执行脚本. 脚本使用数据 ...
- Effective Java - [3. 对象通用方法]
Item 10. 若覆盖equals方法,需要遵守规则
- 地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?
// test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- JavaScript toString() 方法
注意:在JavaScript中,数字后面的"."操作符是的意义是不确定.因为它既可能是一个浮点数的标志,又可能是取一个对象的属性的运算符.但是JavaScript的解释器把他当做了 ...
- linux 经常使用命令
帮助信息 ./configure -help|grep mysql 安装php ./configure --prefix=/usr/local/fastphp --with-mysql=mysqlnd ...
- centOS7 安装nginx+php+mysql
nginx安装 本文是介绍使用源码编译安装,包括具体的编译参数信息. 正式开始前,编译环境gcc g++ 开发库之类的需要提前装好. 安装make: yum -y install gcc automa ...