(多校第五场1012)HDU5792 World is Exploding(树状数组)
来源:互联网 发布:苹果系统mac系统下载 编辑:程序博客网 时间:2024/04/30 01:17
题意:给出一列数,问有多少个四元组满足a<b && c>d,并且四个数两两不相等,a的位置在b的前面,c的位置在d的其那面。
可以先忽略四个数两两不相等的条件,那就是(,逆序对个数)乘上(顺序对个数),例如{2,4,1,3},逆序对就是{(2,1),(4,1),(4,3)} ,顺序对就是{(2,4),(2,3),(1,3)},这样3*3=9,一共九个符合a<b && c>d的四元组,而其中像{2,1,3}这样的都是不符合最终条件的,2<1 && 1>3 但是不满足两两不相等。
现在要计算的就是这样不满足两两不相等的情况的个数,我们枚举相等的那个数,对于t这个数,这种情况的个数就是 (关于t的逆序对个数×关于t的顺序对个数),一一减去就是结果了。
#include <algorithm>#include <iostream>#include <numeric>#include <cstring>#include <iomanip>#include <string>#include <vector>#include <cstdio>#include <queue>#include <stack>#include <cmath>#include <map>#include <set>#define LL long longusing namespace std;#define maxn 51000int arr[maxn];int tree[maxn];int temp[maxn];LL sma[maxn];LL lar[maxn];//bool vis[maxn];int lowbit(int x){ return x&(-x);}void update(int pos,int num,int n){ while(pos<=n) { tree[pos]+=num; pos+=lowbit(pos); } return ;}int getsum(int en){ int sum=0; while(en>0) { sum+=tree[en]; en-=lowbit(en); } return sum;}int main(){ int n; while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;++i) { scanf("%d",&arr[i]); temp[i]=arr[i]; } sort(temp+1,temp+1+n); memset(sma,0,sizeof(sma)); memset(lar,0,sizeof(lar)); memset(tree,0,sizeof(tree)); LL a,b; a=b=0; for(int i=n;i>=1;--i) { int pos=lower_bound(temp+1,temp+1+n,arr[i])-temp;update(pos,1,n); sma[i]+=getsum(n)-getsum(pos); lar[i]+=getsum(pos-1); a+=sma[i]; b+=lar[i]; } memset(tree,0,sizeof(tree)); for(int i=1;i<=n;++i) { int pos=lower_bound(temp+1,temp+1+n,arr[i])-temp; update(pos,1,n); lar[i]+=getsum(n)-getsum(pos); sma[i]+=getsum(pos-1); }// for(int i=1;i<=n;++i)// cout<<lar[i]<<" ";// cout<<endl;// for(int i=1;i<=n;++i)// cout<<sma[i]<<" ";// cout<<endl; LL sub=0; for(int i=1;i<=n;++i) { sub+=lar[i]*sma[i]; } printf("%lld\n",a*b-sub); } return 0;}
0 0
- (多校第五场1012)HDU5792 World is Exploding(树状数组)
- HDU5792(2016多校第五场)——World is Exploding(树状数组,离散化)
- hdu5792 World is Exploding(多校第五场)树状数组求逆序对 离散化
- 2016 MUT 5 1012 World is Exploding(hdu5792) 【树状数组】
- HDU5792 World is Exploding(树状数组)2016 Multi-University Training Contest 5
- hdu5792 World is Exploding
- 2016多校训练Contest5: 1012 World is Exploding hdu5792
- HDU 5792 World is Exploding 2016多校赛第五场 树状数组+容斥原理
- hdu5792 多校5 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 树状数组
- HDU-5792-World is Exploding-树状数组
- (HDU 5792)World is Exploding <树状数组+去重> 多校训练5
- HDU5792 World is Exploding (线段树&&逆序数)
- C++ Primer读书笔记:6.3~6.7
- assests文件下读取csv文件和图片
- poj-Flip Game
- hdoj1541 stars 树状数组
- js笔记
- (多校第五场1012)HDU5792 World is Exploding(树状数组)
- 【杭电1863】畅通工程
- HDU 5792 World is Exploding 2016多校赛第五场 树状数组+容斥原理
- bzoj1070
- Mac scrapy环境搭建
- 如何把hdfs上的多个目录下的文件合并为一个文件
- HDU 2504 又见GCD
- Android OkHttp(1)
- 杭电 ACM 1035 简单模拟题