康托展开 洛谷 P5367

#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize(2)
#define  endl '\n'
#define lowbit(x) ((x)&-(x))
const int N=2e6+10;
const int mod =998244353;
typedef long long ll;
ll ans=0,n1,m1;
ll t=0,s1=0,s2=0,s3=0,s4=0,max1=0,max2=0,w,min1=100000000,sum=0,n,m,i,j,k,v,l,r;

inline int read() {
	bool sym=0;
	int res=0;
	char ch=getchar();
	while(!isdigit(ch))sym |=(ch =='-'),ch=getchar();
	while(isdigit(ch)) res =(res<<3)+(res<<1)+(ch^48),ch=getchar();
	return sym ? -res : res;
}
void print(int x) {
	if(!x)return;
	print(x/10);
	putchar(x%10+'0');
}
int isPrime(int n)
{	
	float n_sqrt;
    if(n==1) return 0;
	if(n==2 || n==3) return 1;
	if(n%6!=1 && n%6!=5) return 0;
	n_sqrt=floor(sqrt((float)n));
	for(int i=5;i<=n_sqrt;i+=6)
	{
	    if(n%(i)==0 | n%(i+2)==0) return 0;
	}
        return 1;
        
}
ll a[1000007]={1,1};
ll jj[1400000];
ll tree[1000007];
void update(ll x,ll d){
	for(ll i=x;i<=n;i+=lowbit(i)){
		tree[i]+=d;
	}
}

ll summ(ll x){
	ll ans=0;
	for(ll i=x;i>0;i-=lowbit(i))
	ans+=tree[i];
	return ans;
}
int main() {


	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(i=1;i<=n;i++)
	{
		a[i]=(a[i-1]*i)%mod;
		update(i,1);
	}
	 for(i=1;i<=n;i++){
	 	cin>>jj[i];
	 	ans=(ans+((summ(jj[i])-1)*a[n-i])%mod)%mod;
	 	update(jj[i],-1);
	 }
cout<<ans+1;
	return 0;
}

//mio lover

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值