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 == ...
随机推荐
- Win 8.1 安装后要做的优化
①无光驱恢复电脑或重置系统(无需插入介质,本来老提示插入介质) 1.准备工作:win8iso,解压到任意非系统盘,解压后的路径假如是:H:\OS\win8 2.接着以管理员身份启动命令提示符,输入 ...
- IE8 "开发人员工具" 无法使用,无法显示
经常使用IE8开发工具的开发人员可能会遇到这么一种去情况:按F12时任务栏里出现开发人员工具的任务,但是开发人员工具窗体不弹出,也不出现在IE8里,重装IE88后还是存在此问题. 解决办法其实非常简单 ...
- 利用xlrd模块实现Python读取Excel文档
# -*- coding: cp936 -*- #python读取excel import xlrd def main(): xls=xlrd.open_workbook("d:\\11.x ...
- List中remove元素的理解
今天写了个简单的list中remove元素的方法,结果报错... List<String> ll = Arrays.asList("1","2",& ...
- JavaWeb、J2-SE开发框架——Spring
相关博客: 2.spring官网 1.使用Spring的jdbcTemplate进一步简化JDBC操作
- 将web工程部署到tomcat
http://blog.csdn.net/lucklq/article/details/7621807 http://jingyan.baidu.com/article/466506582f2f4af ...
- ASP.NET RemoteAttribute远程验证更新问题
create时使用remote特性没有任何问题, update时,问题就大了,验证唯一性时需要排除自身,如果使用这个特性将无法正确的验证. 改进思路:将自动生成的标签属性改为手写,,并在url上面加上 ...
- 苹果开发之COCOA编程(第三版)下半部分
第十八章:Image和鼠标事件 1.NSResponderNSView继承自NSResponder类.所有的事件处理方法都定义在NSResponder类中.NSResponder申明了如下方法:- ( ...
- GS给客户单发包以及m_queGcWait(所有GC共享)
GS给客户单发包以及m_queGcWait(所有GC共享) send_stat BaseChannel::SendCmd(int nCmd, void* pData, int nLen) { Prot ...
- vue-cli与后台数据交互增删改查
1. 安装vue-resource npm install vue-resource --save 2.访问后台地址,在vue中会出现跨域的问题,以下为解决方案 在config下的index.js 中 ...