标签:spl names bit turn ace signed void 树状数组 描述
康托展开公式:
\[ans=1+(\sum_{i=1}^{n}{a_i})\times(n-i)!\]
用树状数组维护一下\(\sum\)里面的东西,前缀积维护后面的东西。
#include<bits/stdc++.h>
using namespace std;
template <typename Tp>
void read(Tp &x){
x=0;char ch=1;int fh;
while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();
if(ch=='-') ch=getchar(),fh=-1;
else fh=1;
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
x*=fh;
}
#define int long long
const int maxn=1000007;
const int mod=998244353;
int n;
int times[maxn],a[maxn];
int c[maxn];
void add(int x,int k){
while(x<=n){c[x]+=k;x+=((x)&(-x));}
}
int query(int x){
int res=0;
while(x){res+=c[x];x-=(x&(-x));}
return res;
}
int ans;
signed main(){
read(n);times[0]=1;
for(int i=1;i<=n;i++){
add(i,1);times[i]=times[i-1]*i%mod;
read(a[i]);
}
for(int i=1;i<=n;i++){
ans=(ans+(query(a[i])-1)*times[n-i]%mod)%mod;
add(a[i],-1);
}
ans=(ans+1)%mod;
printf("%lld\n",ans);
return 0;
}
标签:spl names bit turn ace signed void 树状数组 描述
原文地址:https://www.cnblogs.com/liubainian/p/11609526.html