小奇遐想 + 【bzoj1145】【CTSC2008】【图腾】【组合数学+树状数组】

图腾参考的题解:https://blog.csdn.net/sunshinezff/article/details/48286769

小奇遐想参考的题解:http://hzwer.com/5416.html

 n^3复杂度的暴力写法:

#include<bits/stdc++.h>
using namespace std;
const int mod=16777216;
const int N=200050;
template <class T>
inline void read(T &x){
    x=0;int f=1;char c=getchar();
    for( ; !isdigit(c) ;c=getchar() ){if(c=='-')f=-1;}
    for( ;  isdigit(c) ;c=getchar() ){x=x*10+c-'0';}x*=f;
}
int n,a[N],L[N],ans;
int main()
{
    read(n);
    for(int i=0;i<n;i++) read(a[i]);

    for(int i=0;i<n;i++){
        for(int j=0;j<i;j++){
            if(a[i]>a[j]){
                L[i]++;
            }
        }
    }
    for(int a2=0;a2<n;a2++){
        for(int a3=a2+1;a3<n;a3++){
            if(a[a3]>a[a2]+1){
                for(int a4=a3+1;a4<n;a4++){
                    if(a[a4]<a[a3]&&a[a4]>a[a2]){
                        ans=(ans+L[a2])&(mod-1);
                    }
                }
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}

正解应该是:树状数组:

/*
    if you ask me how much i love you
    the moonlight stand for my heart

    if you ask me how much i love you
    the moonlight stand for my heart

    if you ask me how much i love you
    the moonlight stand for my heart
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=200005;
const int mod=16777216;
ll c[N],a[N],L[N],R[N],n;
int lowbit(int x){return x&(-x);}
void update(int x,int val){for( ;x<=n;x+=lowbit(x))c[x]+=val;}
ll getsum(int x){ll res=0;for(;x>0;x-=lowbit(x))res+=c[x];return res;}
template <class T>
void read(T &x){
    x=0;int f=1;char c=getchar();
    for( ; !isdigit(c) ; c=getchar() ){}
    for( ;  isdigit(c) ; c=getchar() ){x=x*10+c-'0';}
}
void init(){
    for(int i=1;i<=n;i++){
        L[i]=getsum(a[i]);
        R[i]=a[i]-L[i]-1;
        update(a[i],1);
    }
}
int res1(){
    int res=0;
    for(int i=1;i<=n;i++){
        res=(res+L[i]*((n-i-R[i])*(n-i-R[i]-1))/2)&(mod-1);
    }return res;
}
int res2(){
    int res=0;
    memset(c,0,sizeof(c));
    for(int i=1;i<=n;i++){
        res=(res+getsum(a[i])*(n-i-R[i]))&(mod-1);
        update(a[i],L[i]);
    }
    return res;
}
int main()
{
    read(n);
    //printf("%d\n",n);
    for(int i=1;i<=n;i++) read(a[i]);
    init();
    int ans=(res1()-res2())&(mod-1);
    printf("%d\n",ans);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值