HDU 5792 World is Exploding
来源:互联网 发布:js 数组长度 编辑:程序博客网 时间:2024/05/21 01:52
假设,只求一对pair(A,B),就是一道简单的树状数组水题。只需要求出每个位置它右边有多少比它大的数,求一个前缀和就是答案。
但两对其实也一样,各求出两对的前缀和,相乘,然后再把a==c,a==d,b==c,b==d的不符情况减去。
先用树状数组预处理出以下四个数组:
lg[i],左边比a[i]大的;
ls[i],左边比a[i]小的;
rg[i],右边比a[i]大的;
rs[i],右边比a[i]小的;
怎么求不合要求的情况,可以这样考虑
假设a==c,A[b]在a的右边且A[b]>A[a],即rg[a],A[d]也在c的右边且A[d]<A[c],即rs[c];所以a==c的情况就是 rs[i]*rg[i];
其它类似。
【注意】a[i]的值可达1e9,所以要离散化,离散化的时候注意有重复的数字。
附上两份代码,另一份差不多是标程改过来的,其中树状数组的写法我搞不懂,问了某巨之后说是那样写树状数组的下标就可以从0开始了。
【代码1】
/* ***********************************************Author :angon************************************************ */#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <stack>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;#define mst(a,k) memset(a,k,sizeof(a))#define REP(i,k,n) for(int i=k;i<n;i++)#define REPP(i,k,n) for(int i=k;i<=n;i++)#define scan(d) scanf("%d",&d)#define scanl(d) scanf("%I64d",&d)#define scann(n,m) scanf("%d%d",&n,&m)#define scannl(n,m) scanf("%i64d%I64d",&n,&m)#define mst(a,k) memset(a,k,sizeof(a))#define LL long long#define N 50010#define mod 1000000007inline int read(){int s=0;char ch=getchar();for(; ch<'0'||ch>'9'; ch=getchar());for(; ch>='0'&&ch<='9'; ch=getchar())s=s*10+ch-'0';return s;}LL a[N],b[N],ls[N],lg[N],rs[N],rg[N];int n,m;int c[N];int lowbit(int x){ return x & (-x);}void modify (int x,int add) { //在x位置加上add,要修改所有祖先 while(x<=m) { c[x]+=add; x+=lowbit(x); }}LL get_sum(int x) //统计前x项的和{ int ret=0; while(x>0) { ret+=c[x]; x-=lowbit(x); } return ret;}int main(){ //freopen("1012.in","r",stdin); //freopen("out.txt","w",stdout); while(~scan(n)) { REPP(i,1,n) scanl(a[i]),b[i]=a[i]; sort(b+1,b+n+1); m = unique(b+1,b+n+1)-(b+1); REPP(i,1,n) a[i] = lower_bound(b+1,b+m+1,a[i])-b; LL sum1=0,sum2=0; mst(c,0); REPP(i,1,n) { ls[i] = get_sum(a[i]-1); lg[i] = get_sum(m) - get_sum(a[i]); // lg[i] = i - get_sum(a[i]); sum1+=ls[i]; sum2+=lg[i]; modify(a[i],1); } mst(c,0); for(int i=n;i>=1;i--) { rs[i] = get_sum(a[i]-1); //rg[i] = n - i - get_sum(a[i]) +1; rg[i] = get_sum(m) - get_sum(a[i]); modify(a[i],1); } LL ans = sum1*sum2; REPP(i,1,n) { ans -= rg[i]*rs[i]; ans -= rg[i]*lg[i]; ans -= ls[i]*rs[i]; ans -= ls[i]*lg[i]; } printf("%I64d\n",ans); } return 0;}
【代码2】
#include <bits/stdc++.h>#define LL long long#define ALL(v) (v).begin(),(v).end()#define showtime fprintf(stderr,"time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)const int N = 100000 + 5;int n,A[N],B[N],C[N];int ls[N],rs[N],lg[N],rg[N];int tot;void modify(int p,int dt) { //树状数组下标从0开始 for (int i = p; i < tot; i += ~i & i + 1) C[i] += dt;}int query(int p) { int ret = 0; for (int i = p; i >= 0; i -= ~i & i + 1) ret += C[i]; return ret;}int main() { //freopen("1012.in","r",stdin); //freopen("out.txt","w",stdout); while (scanf("%d",&n) == 1) { for (int i = 0; i < n; ++ i) { scanf("%d",A + i); B[i]=A[i]; } sort(B,B+n); tot = unique(B,B+n)-B; for (int i = 0; i < n; ++ i) { A[i] = lower_bound(B,B+tot,A[i]) - B ; } mst(C,0); for (int i = 0; i < n; ++ i) { ls[i] = query(A[i] - 1); lg[i] = i - query(A[i]); modify(A[i],1); } mst(C,0); for (int i = n - 1; i >= 0; -- i) { rs[i] = query(A[i] - 1); rg[i] = n - 1 - i - query(A[i]); modify(A[i],1); } LL result = 0,allg = accumulate(rs,rs + n,0ll); for (int i = 0; i < n; ++ i) { result += rg[i] * 1ll * (allg - lg[i] - rs[i]) - (lg[i] + rs[i]) * 1ll * ls[i]; } printf("%I64d\n",result); } return 0;}
0 0
- HDU 5792 World is Exploding
- HDU 5792 World is Exploding
- HDU 5792 World is Exploding
- HDU 5792 World is Exploding
- HDU 5792 World is Exploding
- Hdu 5792 World is Exploding
- World is Exploding HDU
- hdu 5792 World is Exploding 树状数组
- HDU-5792-World is Exploding-树状数组
- HDU 5792 World is Exploding(BIT)
- HDOJ 5792 World is Exploding
- HDU 5792 World is Exploding (树状数组逆序对)
- HDU-5792-World is Exploding(树状数组+离散化)
- Hdu-5792 World is Exploding(简单容斥)
- hdu 5792 World is Exploding 离散化+树状数组
- hdu 5792 World is Exploding (树状数组)
- hdu 5792 World is Exploding 2016 Multi-University 5
- HDU 5792 World is Exploding(树状数组)
- mysql数据库文件和innodb存储引擎文件学习笔记
- 用Log Parser Studio分析IIS日志
- 【Codeforces】-702B-Powers of Two(二分)
- How Many Maos Does the Guanxi Worth
- (二)、变量的声明
- HDU 5792 World is Exploding
- win7 64位下安装和配置python2.7.6及其库管理工具
- Muduo库的框架剖析及总结(一)
- Emacs用Magit管理git项目
- C++运算符的重载
- HDU 1007 Quoit Design 最小点对基础模板
- tomcat目录结构及配置文件说明
- 汉字转拼音
- java网络通讯(UDP与TCP)