图腾参考的题解: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);
}