(多校第五场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
原创粉丝点击