HDU-5792 World is Exploding(树状数组)
题目大意:给一个整数序列,统计四元组(a,b,c,d)的个数,满足条件1:a<>b<>c<>d;条件2:<a,b>组成一个顺序对,<c,d>组成一个逆序对。(a、b、c、d均为下标)
代码如下:从所有四元组中减去不满足条件的四元组。用顺序对数乘以逆序对数得到只满足条件2的四元组数目sum,从sum减去不满足条件1的四元组数目便是答案。sum中只有四种不满足条件1的情况,即:a=c、a=d、b=c、b=d。四种情况的含义分别为顺序对的左端点与逆序对的左端点相同、顺序对的左端点与逆序对的右端点相同、顺序对的右端点与逆序对的左端点相同、顺序对的右端点与逆序对的右端点相同。维护四个数组A、B、C、D,数组中的位置 i 分别表示以 i 为右端点的顺序、逆序以及以 i 为左端点的顺序、逆序对数,便可计算出相应情况下多余的四元组数目。
这四个数组是通过维护树状数组获得的,但是通过维护线段树却超时啦。。。
代码如下:
# include<iostream>
# include<cstdio>
# include<map>
# include<vector>
# include<cstring>
# include<algorithm>
using namespace std;
# define LL long long const int N=50000; int a[N+5];
map<int,int>mp;
vector<int>v;
int vis[N+5];
int cnt[N+5];
int A[N+5];//以i为右端点的顺序对个数
int B[N+5];//以i为右端点的逆序对个数
int C[N+5];//以i为左端点的顺序对个数
int D[N+5];//以i为左端点的逆序对个数 int lowbit(int x)
{
return x&(-x);
} int query(int x)
{
int res=0;
while(x>=1){
res+=cnt[x];
x-=lowbit(x);
}
return res;
} void update(int x,int n)
{
while(x<=n){
++cnt[x];
x+=lowbit(x);
}
} int main()
{
int n;
while(~scanf("%d",&n))
{
v.clear();
mp.clear();
for(int i=1;i<=n;++i){
scanf("%d",a+i);
v.push_back(a[i]);
}
sort(v.begin(),v.end());
int m=unique(v.begin(),v.end())-v.begin();
for(int i=0;i<m;++i) mp[v[i]]=i+1; memset(cnt,0,sizeof(cnt));
memset(vis,0,sizeof(vis));
LL sum1=0,sum2=0;
for(int i=1;i<=n;++i){
A[i]=query(mp[a[i]]-1);
B[i]=i-1-A[i]-vis[mp[a[i]]];
update(mp[a[i]],m+1);
++vis[mp[a[i]]];
sum1+=A[i];
sum2+=B[i];
} memset(cnt,0,sizeof(cnt));
memset(vis,0,sizeof(vis));
for(int i=n;i>=1;--i){
D[i]=query(mp[a[i]]-1);
C[i]=n-i-D[i]-vis[mp[a[i]]];
update(mp[a[i]],m+1);
++vis[mp[a[i]]];
} LL sum3=0;
for(int i=1;i<=n;++i){
sum3+=(LL)C[i]*(LL)D[i];
sum3+=(LL)C[i]*(LL)B[i];
sum3+=(LL)A[i]*(LL)D[i];
sum3+=(LL)A[i]*(LL)B[i];
}
printf("%lld\n",sum1*sum2-sum3);
}
return 0;
}
线段树的超时代码:
# include<iostream>
# include<cstdio>
# include<map>
# include<vector>
# include<cstring>
# include<algorithm>
using namespace std;
# define LL long long
# define mid (l+(r-l)/2) const int N=50000; int a[N+5];
vector<int>v;
int A[N+5];//以i为右端点的顺序对个数
int B[N+5];//以i为右端点的逆序对个数
int C[N+5];//以i为左端点的顺序对个数
int D[N+5];//以i为左端点的逆序对个数
int tr[N*4+5];
map<int,int>mp; void pushUp(int rt)
{
tr[rt]=tr[rt<<1]+tr[rt<<1|1];
} void build(int rt,int l,int r)
{
tr[rt]=0;
if(l==r) return ;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
} void update(int rt,int l,int r,int pos)
{
if(l==r)
++tr[rt];
else{
if(pos<=mid) update(rt<<1,l,mid,pos);
else update(rt<<1|1,mid+1,r,pos);
pushUp(rt);
}
} int query(int rt,int l,int r,int L,int R)
{
if(L<=l&&r<=R) return tr[rt];
int res=0;
if(L<=mid) res+=query(rt<<1,l,mid,L,R);
if(R>mid) res+=query(rt<<1|1,mid+1,r,L,R);
return res;
} int main()
{
int n;
while(~scanf("%d",&n))
{
mp.clear();
v.clear();
for(int i=1;i<=n;++i){
scanf("%d",a+i);
v.push_back(a[i]);
}
sort(v.begin(),v.end());
int len=unique(v.begin(),v.end())-v.begin();
for(int i=0;i<len;++i)
mp[v[i]]=i+1;
build(1,0,len+1);
LL sum1=0,sum2=0;
for(int i=1;i<=n;++i){
A[i]=query(1,0,len+1,0,mp[a[i]]-1);
B[i]=query(1,0,len+1,mp[a[i]]+1,len+1);
update(1,0,len+1,mp[a[i]]);
sum1+=A[i];
sum2+=B[i];
}
build(1,0,len+1);
for(int i=n;i>=1;--i){
C[i]=query(1,0,len+1,mp[a[i]]+1,len+1);
D[i]=query(1,0,len+1,0,mp[a[i]]-1);
update(1,0,len+1,mp[a[i]]);
}
LL sum3=0;
for(int i=1;i<=n;++i){
sum3+=(LL)C[i]*(LL)D[i];
sum3+=(LL)C[i]*(LL)B[i];
sum3+=(LL)A[i]*(LL)D[i];
sum3+=(LL)A[i]*(LL)B[i];
}
printf("%lld\n",sum1*sum2-sum3);
}
return 0;
}
HDU-5792 World is Exploding(树状数组)的更多相关文章
- 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 5792 World is Exploding 树状数组+离散化+容斥
World is Exploding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- 2016 Multi-University Training Contest 5 1012 World is Exploding 树状数组+离线化
http://acm.hdu.edu.cn/showproblem.php?pid=5792 1012 World is Exploding 题意:选四个数,满足a<b and A[a]< ...
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- HDU 5862 Counting Intersections (树状数组)
Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...
- hdu 5592 ZYB's Game 树状数组
ZYB's Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=55 ...
- HDU 1394 Minimum Inversion Number (树状数组求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...
随机推荐
- C# Async/Await异步函数原理
原理 与同步函数相比,CLR在执行异步函数时有几个不同的特点: 1. 并非一次完成,而且分多次完成 2. 并非由同一个线程完成,而是线程池每次动态分配一个线程来处理: 结合 ...
- 启用jboss热部署
Please make sure to add <configuration> <jsp-configuration deve ...
- powershell玩转xml之20问
powershell玩转xml之20问 powershell 传教士 原创文章 2014-01-30,2015-10-27改 允许转载,但必须保留名字和出处,否则追究法律责任 问:xml文件编码情况如 ...
- 将table导出为Excel的标准无乱码写法
导出为Excel有很多种写法,对于一些复杂的格式,笔者喜欢在后台先拼成一个<table>,再使用Response输出. 如果数据中包含中文或者一些特殊字符,可很多不规范的写法都会导致页面乱 ...
- c# access插入null值
c# 插入access数据库 提示错误: Parameter @DeviceLocation has no default value. 参数@DeviceLocation 的有没有默认值. Stri ...
- Volatile vs VolatileRead/Write?
You should never use Thread.VolatileRead/Write(). It was a design mistake in .NET 1.1, it uses a ful ...
- iOS 网络判定
由于流量精灵需要在 蜂窝数据或者3G 环境下进行流量监控因此需要判定3G 环境 将 SystemConfiguration.framework 添加进工程: 引入头文件 #import <Sys ...
- iOS: 枚举类型 enum,NS_ENUM,NS_OPTIONS
一般情况下,我们采用C风格的enum关键字可以定义枚举类型. enum{ UIViewAnimationTransitionNone, UIViewAnimationTransitionFlipFro ...
- python3中输出不换行
python2中输出默认是换行的,为了抑制换行,是这么做的: print x, 到了python3中,print变成一个函数,这种语法便行不通了.用2to3工具转换了下,变成这样了: print(x, ...
- javascript笔记5-BOM
Javascript应用的平台很多,不仅仅针对Web.在Web中使用Javascript,BOM(browser object model,浏览器对象模型)是核心. BOM提供了很多对象,用于访问浏览 ...